Skip to content

Commit 58457bb

Browse files
committed
Auto merge of #89404 - Kobzol:hash-stable-sort, r=Mark-Simulacrum
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!
2 parents e70e4d4 + e4b4d18 commit 58457bb

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
559559
SK: HashStable<HCX> + Ord,
560560
F: Fn(&K, &HCX) -> SK,
561561
{
562-
let mut entries: Vec<_> = map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
562+
let mut entries: SmallVec<[_; 3]> =
563+
map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
563564
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
564565
entries.hash_stable(hcx, hasher);
565566
}

0 commit comments

Comments
 (0)