From ac08f13948303491ed4a00f0578bd64f0327769c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 13 Dec 2021 09:19:59 +0100 Subject: [PATCH] Remove sort from hashing hashset, treeset and treemap --- .../src/stable_hasher.rs | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index 150c835c236..f37cf76c32b 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -503,13 +503,17 @@ impl_stable_hash_via_hash!(::std::path::PathBuf); impl HashStable for ::std::collections::HashMap where - K: HashStable + ToStableHashKey + Eq, + K: ToStableHashKey + Eq, V: HashStable, R: BuildHasher, { #[inline] fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key); + stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| { + let key = key.to_stable_hash_key(hcx); + key.hash_stable(hcx, hasher); + value.hash_stable(hcx, hasher); + }); } } @@ -519,9 +523,10 @@ where R: BuildHasher, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect(); - keys.sort_unstable(); - keys.hash_stable(hcx, hasher); + stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| { + let key = key.to_stable_hash_key(hcx); + key.hash_stable(hcx, hasher); + }); } } @@ -531,10 +536,11 @@ where V: HashStable, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut entries: Vec<_> = - self.iter().map(|(k, v)| (k.to_stable_hash_key(hcx), v)).collect(); - entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2)); - entries.hash_stable(hcx, hasher); + stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, (key, value)| { + let key = key.to_stable_hash_key(hcx); + key.hash_stable(hcx, hasher); + value.hash_stable(hcx, hasher); + }); } } @@ -543,34 +549,30 @@ where K: ToStableHashKey, { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { - let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect(); - keys.sort_unstable(); - keys.hash_stable(hcx, hasher); + stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| { + let key = key.to_stable_hash_key(hcx); + key.hash_stable(hcx, hasher); + }); } } -pub fn hash_stable_hashmap( +fn stable_hash_reduce( hcx: &mut HCX, hasher: &mut StableHasher, - map: &::std::collections::HashMap, - to_stable_hash_key: F, + collection: C, + length: usize, + hash_function: F, ) where - K: Eq + HashStable, - V: HashStable, - R: BuildHasher, - SK: HashStable + Ord, - F: Fn(&K, &HCX) -> SK, + C: Iterator, + F: Fn(&mut StableHasher, &mut HCX, I), { - let hash = map - .iter() - .map(|(key, value)| { - let key = to_stable_hash_key(key, hcx); + let hash = collection + .map(|value| { let mut hasher = StableHasher::new(); - key.hash_stable(hcx, &mut hasher); - value.hash_stable(hcx, &mut hasher); + hash_function(&mut hasher, hcx, value); hasher.finish::() }) .reduce(|accum, value| accum.wrapping_add(value)); - map.len().hash_stable(hcx, hasher); + length.hash_stable(hcx, hasher); hash.hash_stable(hcx, hasher); }