From 67c84e05e72931587e5f81a297dee95bc149bcd8 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Wed, 13 Sep 2017 18:20:27 +0200 Subject: incr.comp.: Use StableHash impls instead of functions for hashing most maps. --- src/librustc_data_structures/stable_hasher.rs | 50 ++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'src/librustc_data_structures') diff --git a/src/librustc_data_structures/stable_hasher.rs b/src/librustc_data_structures/stable_hasher.rs index 1b2d09bef93..e18c1969a91 100644 --- a/src/librustc_data_structures/stable_hasher.rs +++ b/src/librustc_data_structures/stable_hasher.rs @@ -289,7 +289,8 @@ impl, CTX> HashStable for (T1,) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - self.0.hash_stable(ctx, hasher); + let (ref _0,) = *self; + _0.hash_stable(ctx, hasher); } } @@ -297,8 +298,24 @@ impl, T2: HashStable, CTX> HashStable for (T1, T2) fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - self.0.hash_stable(ctx, hasher); - self.1.hash_stable(ctx, hasher); + let (ref _0, ref _1) = *self; + _0.hash_stable(ctx, hasher); + _1.hash_stable(ctx, hasher); + } +} + +impl HashStable for (T1, T2, T3) + where T1: HashStable, + T2: HashStable, + T3: HashStable, +{ + fn hash_stable(&self, + ctx: &mut CTX, + hasher: &mut StableHasher) { + let (ref _0, ref _1, ref _2) = *self; + _0.hash_stable(ctx, hasher); + _1.hash_stable(ctx, hasher); + _2.hash_stable(ctx, hasher); } } @@ -462,14 +479,11 @@ impl HashStable for ::std::collections::HashMap V: HashStable, R: BuildHasher, { + #[inline] 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); + hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key); } } @@ -516,3 +530,23 @@ impl HashStable for ::std::collections::BTreeSet keys.hash_stable(hcx, hasher); } } + +pub fn hash_stable_hashmap( + hcx: &mut HCX, + hasher: &mut StableHasher, + map: &::std::collections::HashMap, + to_stable_hash_key: F) + where K: Eq + Hash, + V: HashStable, + R: BuildHasher, + SK: HashStable + Ord + Clone, + F: Fn(&K, &HCX) -> SK, + W: StableHasherResult, +{ + let mut entries: Vec<_> = 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); +} + -- cgit 1.4.1-3-g733a5