Skip to content

Commit

Permalink
Auto merge of rust-lang#89404 - Kobzol:hash-stable-sort, r=Mark-Simul…
Browse files Browse the repository at this point in the history
…acrum

Slightly optimize hash map stable hashing

I was profiling some of the `rustc-perf` benchmarks locally and noticed that quite some time is spent inside the stable hash of hashmaps. I tried to use a `SmallVec` instead of a `Vec` there, which helped very slightly.

Then I tried to remove the sorting, which was a bottleneck, and replaced it with insertion into a binary heap. Locally, it yielded nice improvements in instruction counts and RSS in several benchmarks for incremental builds. The implementation could probably be much nicer and possibly extended to other stable hashes, but first I wanted to test the perf impact properly.

Can I ask someone to do a perf run? Thank you!
  • Loading branch information
bors committed Dec 12, 2021
2 parents e70e4d4 + e4b4d18 commit 58457bb
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
SK: HashStable<HCX> + Ord,
F: Fn(&K, &HCX) -> SK,
{
let mut entries: Vec<_> = map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
let mut entries: SmallVec<[_; 3]> =
map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
entries.hash_stable(hcx, hasher);
}

0 comments on commit 58457bb

Please sign in to comment.