1
Fork 0
rust/compiler/rustc_data_structures/src
bors f6fa358a18 Auto merge of #127226 - mat-1:optimize-siphash-round, r=nnethercote
Optimize SipHash by reordering compress instructions

This PR optimizes hashing by changing the order of instructions in the sip.rs `compress` macro so the CPU can parallelize it better. The new order is taken directly from Fig 2.1 in [the SipHash paper](https://eprint.iacr.org/2012/351.pdf) (but with the xors moved which makes it a little faster). I attempted to optimize it some more after this, but I think this might be the optimal instruction order. Note that this shouldn't change the behavior of hashing at all, only statements that don't depend on each other were reordered.

It appears like the current order hasn't changed since its [original implementation from 2012](fada46c421 (diff-b751133c229259d7099bbbc7835324e5504b91ab1aded9464f0c48cd22e5e420R35)) which doesn't look like it was written with data dependencies in mind.

Running `./x bench library/core --stage 0 --test-args hash` before and after this change shows the following results:

Before:
```
benchmarks:
    hash::sip::bench_bytes_4             7.20/iter +/- 0.70
    hash::sip::bench_bytes_7             9.01/iter +/- 0.35
    hash::sip::bench_bytes_8             8.12/iter +/- 0.10
    hash::sip::bench_bytes_a_16         10.07/iter +/- 0.44
    hash::sip::bench_bytes_b_32         13.46/iter +/- 0.71
    hash::sip::bench_bytes_c_128        37.75/iter +/- 0.48
    hash::sip::bench_long_str          121.18/iter +/- 3.01
    hash::sip::bench_str_of_8_bytes     11.20/iter +/- 0.25
    hash::sip::bench_str_over_8_bytes   11.20/iter +/- 0.26
    hash::sip::bench_str_under_8_bytes   9.89/iter +/- 0.59
    hash::sip::bench_u32                 9.57/iter +/- 0.44
    hash::sip::bench_u32_keyed           6.97/iter +/- 0.10
    hash::sip::bench_u64                 8.63/iter +/- 0.07
```
After:
```
benchmarks:
    hash::sip::bench_bytes_4             6.64/iter +/- 0.14
    hash::sip::bench_bytes_7             8.19/iter +/- 0.07
    hash::sip::bench_bytes_8             8.59/iter +/- 0.68
    hash::sip::bench_bytes_a_16          9.73/iter +/- 0.49
    hash::sip::bench_bytes_b_32         12.70/iter +/- 0.06
    hash::sip::bench_bytes_c_128        32.38/iter +/- 0.20
    hash::sip::bench_long_str          102.99/iter +/- 0.82
    hash::sip::bench_str_of_8_bytes     10.71/iter +/- 0.21
    hash::sip::bench_str_over_8_bytes   11.73/iter +/- 0.17
    hash::sip::bench_str_under_8_bytes  10.33/iter +/- 0.41
    hash::sip::bench_u32                10.41/iter +/- 0.29
    hash::sip::bench_u32_keyed           9.50/iter +/- 0.30
    hash::sip::bench_u64                 8.44/iter +/- 1.09
```
I ran this on my computer so there's some noise, but you can tell at least `bench_long_str` is significantly faster (~18%).

Also, I noticed the same compress function from the library is used in the compiler as well, so I took the liberty of copy-pasting this change to there as well.

Thanks `@semisol` for porting SipHash for another project which led me to notice this issue in Rust, and for helping investigate. <3
2024-07-04 04:03:45 +00:00
..
base_n Stabilize the size of incr comp object file names 2024-04-22 10:50:07 -04:00
binary_search_util Remove invariant comments 2023-11-05 17:35:37 -06:00
fingerprint remove redundant imports 2023-12-10 10:56:22 +08:00
flock Remove extern crate tracing. 2024-05-08 12:52:31 +10:00
graph Address code review comments on the comments 2024-06-12 15:48:34 +02:00
intern remove redundant imports 2023-12-10 10:56:22 +08:00
obligation_forest Require any function with a tait in its signature to actually constrain a hidden type 2024-06-12 08:53:59 +00:00
owned_slice Don't leak the function that is called on drop 2023-05-23 14:53:36 +00:00
profiling Don't use serde_json to serialize a simple JSON object 2023-04-16 15:00:06 +02:00
sip128 remove redundant imports 2023-12-10 10:56:22 +08:00
small_c_str Fix SmallCStr conversion from CStr 2024-02-14 18:40:53 -08:00
snapshot_map
sorted_map Split {Idx, IndexVec, IndexSlice} into their own modules 2023-04-24 13:53:35 +00:00
sso rustc_data_structures: use either instead of itertools 2024-01-24 15:36:57 -08:00
stable_hasher Store hashes in special types so they aren't accidentally encoded as numbers 2023-04-18 10:52:47 -04:00
sync Apply x clippy --fix and x fmt 2024-05-30 09:51:27 +08:00
tagged_ptr Add support for NonNull in ambiguous_wide_ptr_comparisions 2024-03-29 22:02:07 +01:00
transitive_relation get rid of RefCell in TransitiveRelation 2022-08-22 18:08:46 +08:00
aligned.rs fix broken intradoclinks 2023-04-14 13:04:58 +00:00
atomic_ref.rs
base_n.rs Convert some module-level // and /// comments to //!. 2024-06-20 09:23:18 +10:00
captures.rs
fingerprint.rs Remove macro_use from stable_hasher. 2024-05-07 10:19:12 +10:00
flat_map_in_place.rs Rename MapInPlace as FlatMapInPlace. 2023-03-08 15:53:56 +11:00
flock.rs Use Linux file locking on Redox 2024-06-16 12:56:50 +00:00
frozen.rs Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
fx.rs rustc_mir_transform: Make DestinationPropagation stable for queries 2024-01-05 20:55:32 +01:00
hashes.rs Use UnhashMap for a few more maps 2024-01-17 17:09:55 -05:00
intern.rs Don't print Interned or PrivateZst 2023-06-09 00:20:37 +00:00
jobserver.rs Update jobserver-rs to 0.1.28 2024-02-09 19:13:07 +03:00
lib.rs RFC 2383: Stabilize lint_reasons 🎉 2024-06-25 17:22:22 +02:00
marker.rs rustc_data_structures: Explicitly check for 64-bit atomics support 2024-06-28 10:26:45 +02:00
memmap.rs Issue 122262: MAP_PRIVATE for more reliability on virtualised filesystems. 2024-03-15 18:31:07 -04:00
owned_slice.rs By tracking import use types to check whether it is scope uses or the other situations like module-relative uses, we can do more accurate redundant import checking. 2024-02-18 16:38:11 +08:00
packed.rs Document Pu128. 2024-05-09 08:13:24 +10:00
profiling.rs Apply x clippy --fix and x fmt 2024-05-30 09:51:27 +08:00
sharded.rs rustc_data_structures: use either instead of itertools 2024-01-24 15:36:57 -08:00
sip128.rs Optimize SipHash by reordering compress instructions 2024-07-01 22:36:40 +00:00
small_c_str.rs Fix SmallCStr conversion from CStr 2024-02-14 18:40:53 -08:00
sorted_map.rs Apply x clippy --fix and x fmt 2024-05-30 09:51:27 +08:00
stable_hasher.rs Ensure careful consideration is given by impls 2024-06-22 07:17:02 +01:00
stack.rs Fix some clippy::complexity 2023-04-09 23:22:14 +02:00
steal.rs Harden the pre-tyctxt query system against accidental recomputation 2023-01-12 09:26:28 +00:00
svh.rs Remove extern crate rustc_macros from numerous crates. 2024-04-29 10:21:54 +10:00
sync.rs rustc_data_structures: fix wrong markdown syntax 2024-07-01 07:21:02 -07:00
tagged_ptr.rs Simplify bits_for_tags impl 2023-04-20 19:54:10 +00:00
temp_dir.rs Update dependencies with reported vulnerabilities 2023-06-02 12:34:01 -05:00
transitive_relation.rs Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
unhash.rs
unord.rs Add UnordMap::try_insert 2024-05-02 03:49:46 +02:00
work_queue.rs Split {Idx, IndexVec, IndexSlice} into their own modules 2023-04-24 13:53:35 +00:00