Skip to content

Commit a5f5f6b

Browse files
committed
Avoid sorting in hash map stable hashing
1 parent 58457bb commit a5f5f6b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ impl StableHasher {
4242
}
4343

4444
impl StableHasherResult for u128 {
45+
#[inline]
4546
fn finish(hasher: StableHasher) -> Self {
4647
let (_0, _1) = hasher.finalize();
4748
u128::from(_0) | (u128::from(_1) << 64)
4849
}
4950
}
5051

5152
impl StableHasherResult for u64 {
53+
#[inline]
5254
fn finish(hasher: StableHasher) -> Self {
5355
hasher.finalize().0
5456
}
@@ -559,8 +561,16 @@ pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
559561
SK: HashStable<HCX> + Ord,
560562
F: Fn(&K, &HCX) -> SK,
561563
{
562-
let mut entries: SmallVec<[_; 3]> =
563-
map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
564-
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
565-
entries.hash_stable(hcx, hasher);
564+
let hash = map
565+
.iter()
566+
.map(|(key, value)| {
567+
let key = to_stable_hash_key(key, hcx);
568+
let mut hasher = StableHasher::new();
569+
key.hash_stable(hcx, &mut hasher);
570+
value.hash_stable(hcx, &mut hasher);
571+
hasher.finish::<u128>()
572+
})
573+
.reduce(|accum, value| accum.wrapping_mul(value));
574+
map.len().hash_stable(hcx, hasher);
575+
hash.hash_stable(hcx, hasher);
566576
}

0 commit comments

Comments
 (0)