feat(common): Align stable_hasher to latest rustc (#3895)

This commit is contained in:
magic-akari 2022-03-07 18:53:54 +08:00 committed by GitHub
parent e7e2fe148a
commit 372f298f5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 134 additions and 93 deletions

View File

@ -1,6 +1,5 @@
use std::{ use std::{
hash::{BuildHasher, Hash, Hasher}, hash::{BuildHasher, Hash, Hasher},
marker::PhantomData,
mem, mem,
}; };
@ -13,57 +12,56 @@ use siphasher::sip128::{Hash128, Hasher128, SipHasher24};
/// To that end we always convert integers to little-endian format before /// To that end we always convert integers to little-endian format before
/// hashing and the architecture dependent `isize` and `usize` types are /// hashing and the architecture dependent `isize` and `usize` types are
/// extended to 64 bits if needed. /// extended to 64 bits if needed.
pub struct StableHasher<W> { pub struct StableHasher {
state: SipHasher24, state: SipHasher24,
bytes_hashed: u64,
width: PhantomData<W>,
} }
impl<W: StableHasherResult> ::std::fmt::Debug for StableHasher<W> { impl ::std::fmt::Debug for StableHasher {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{:?}", self.state) write!(f, "{:?}", self.state)
} }
} }
pub trait StableHasherResult: Sized { pub trait StableHasherResult: Sized {
fn finish(hasher: StableHasher<Self>) -> Self; fn finish(hasher: StableHasher) -> Self;
} }
impl<W: StableHasherResult> StableHasher<W> { impl StableHasher {
#[inline]
pub fn new() -> Self { pub fn new() -> Self {
StableHasher { StableHasher {
state: SipHasher24::new_with_keys(0, 0), state: SipHasher24::new_with_keys(0, 0),
bytes_hashed: 0,
width: PhantomData,
} }
} }
pub fn finish(self) -> W { #[inline]
pub fn finish<W: StableHasherResult>(self) -> W {
W::finish(self) W::finish(self)
} }
} }
impl StableHasherResult for u128 { impl StableHasherResult for u128 {
fn finish(hasher: StableHasher<Self>) -> Self { #[inline]
let (_0, _1) = hasher.finalize().as_u64(); fn finish(hasher: StableHasher) -> Self {
(_0 as u128) | ((_1 as u128) << 64) hasher.finalize().as_u128()
} }
} }
impl StableHasherResult for u64 { impl StableHasherResult for u64 {
fn finish(hasher: StableHasher<Self>) -> Self { #[inline]
fn finish(hasher: StableHasher) -> Self {
hasher.finalize().h1 hasher.finalize().h1
} }
} }
impl<W> StableHasher<W> { impl StableHasher {
#[inline] #[inline]
pub fn finalize(self) -> Hash128 { pub fn finalize(self) -> Hash128 {
self.state.finish128() self.state.finish128()
} }
} }
impl<W> Hasher for StableHasher<W> { impl Hasher for StableHasher {
fn finish(&self) -> u64 { fn finish(&self) -> u64 {
panic!("use StableHasher::finalize instead"); panic!("use StableHasher::finalize instead");
} }
@ -71,37 +69,31 @@ impl<W> Hasher for StableHasher<W> {
#[inline] #[inline]
fn write(&mut self, bytes: &[u8]) { fn write(&mut self, bytes: &[u8]) {
self.state.write(bytes); self.state.write(bytes);
self.bytes_hashed += bytes.len() as u64;
} }
#[inline] #[inline]
fn write_u8(&mut self, i: u8) { fn write_u8(&mut self, i: u8) {
self.state.write_u8(i); self.state.write_u8(i);
self.bytes_hashed += 1;
} }
#[inline] #[inline]
fn write_u16(&mut self, i: u16) { fn write_u16(&mut self, i: u16) {
self.state.write_u16(i.to_le()); self.state.write_u16(i.to_le());
self.bytes_hashed += 2;
} }
#[inline] #[inline]
fn write_u32(&mut self, i: u32) { fn write_u32(&mut self, i: u32) {
self.state.write_u32(i.to_le()); self.state.write_u32(i.to_le());
self.bytes_hashed += 4;
} }
#[inline] #[inline]
fn write_u64(&mut self, i: u64) { fn write_u64(&mut self, i: u64) {
self.state.write_u64(i.to_le()); self.state.write_u64(i.to_le());
self.bytes_hashed += 8;
} }
#[inline] #[inline]
fn write_u128(&mut self, i: u128) { fn write_u128(&mut self, i: u128) {
self.state.write_u128(i.to_le()); self.state.write_u128(i.to_le());
self.bytes_hashed += 16;
} }
#[inline] #[inline]
@ -110,53 +102,77 @@ impl<W> Hasher for StableHasher<W> {
// platforms. This is important for symbol hashes when cross compiling, // platforms. This is important for symbol hashes when cross compiling,
// for example. // for example.
self.state.write_u64((i as u64).to_le()); self.state.write_u64((i as u64).to_le());
self.bytes_hashed += 8;
} }
#[inline] #[inline]
fn write_i8(&mut self, i: i8) { fn write_i8(&mut self, i: i8) {
self.state.write_i8(i); self.state.write_i8(i);
self.bytes_hashed += 1;
} }
#[inline] #[inline]
fn write_i16(&mut self, i: i16) { fn write_i16(&mut self, i: i16) {
self.state.write_i16(i.to_le()); self.state.write_i16(i.to_le());
self.bytes_hashed += 2;
} }
#[inline] #[inline]
fn write_i32(&mut self, i: i32) { fn write_i32(&mut self, i: i32) {
self.state.write_i32(i.to_le()); self.state.write_i32(i.to_le());
self.bytes_hashed += 4;
} }
#[inline] #[inline]
fn write_i64(&mut self, i: i64) { fn write_i64(&mut self, i: i64) {
self.state.write_i64(i.to_le()); self.state.write_i64(i.to_le());
self.bytes_hashed += 8;
} }
#[inline] #[inline]
fn write_i128(&mut self, i: i128) { fn write_i128(&mut self, i: i128) {
self.state.write_i128(i.to_le()); self.state.write_i128(i.to_le());
self.bytes_hashed += 16;
} }
#[inline] #[inline]
fn write_isize(&mut self, i: isize) { fn write_isize(&mut self, i: isize) {
// Always treat isize as i64 so we get the same results on 32 and 64 bit // Always treat isize as a 64-bit number so we get the same results on 32 and 64
// platforms. This is important for symbol hashes when cross compiling, // bit platforms. This is important for symbol hashes when cross
// for example. // compiling, for example. Sign extending here is preferable as it means
self.state.write_i64((i as i64).to_le()); // that the same negative number hashes the same on both 32 and 64 bit
self.bytes_hashed += 8; // platforms.
let value = i as u64;
// Cold path
#[cold]
#[inline(never)]
fn hash_value(state: &mut SipHasher24, value: u64) {
state.write_u8(0xff);
state.write_u64(value.to_le());
}
// `isize` values often seem to have a small (positive) numeric value in
// practice. To exploit this, if the value is small, we will hash a
// smaller amount of bytes. However, we cannot just skip the leading
// zero bytes, as that would produce the same hash e.g. if you hash two
// values that have the same bit pattern when they are swapped. See https://github.com/rust-lang/rust/pull/93014 for context.
//
// Therefore, we employ the following strategy:
// 1) When we encounter a value that fits within a single byte (the most common
// case), we hash just that byte. This is the most common case that is
// being optimized. However, we do not do this for the value 0xFF, as
// that is a reserved prefix (a bit like in UTF-8). 2) When we encounter
// a larger value, we hash a "marker" 0xFF and then the corresponding
// 8 bytes. Since this prefix cannot occur when we hash a single byte, when we
// hash two `isize`s that fit within a different amount of bytes, they
// should always produce a different byte stream for the hasher.
if value < 0xff {
self.state.write_u8(value as u8);
} else {
hash_value(&mut self.state, value);
}
} }
} }
/// Something that implements `HashStable<CTX>` can be hashed in a way that is /// Something that implements `HashStable<CTX>` can be hashed in a way that is
/// stable across multiple compilation sessions. /// stable across multiple compilation sessions.
pub trait HashStable<CTX> { pub trait HashStable<CTX> {
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut CTX, hasher: &mut StableHasher<W>); fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher);
} }
/// Implement this for types that can be turned into stable keys like, for /// Implement this for types that can be turned into stable keys like, for
@ -174,10 +190,10 @@ macro_rules! impl_stable_hash_via_hash {
($t:ty) => { ($t:ty) => {
impl<CTX> $crate::rustc_data_structures::stable_hasher::HashStable<CTX> for $t { impl<CTX> $crate::rustc_data_structures::stable_hasher::HashStable<CTX> for $t {
#[inline] #[inline]
fn hash_stable<W: $crate::rustc_data_structures::stable_hasher::StableHasherResult>( fn hash_stable(
&self, &self,
_: &mut CTX, _: &mut CTX,
hasher: &mut $crate::rustc_data_structures::stable_hasher::StableHasher<W>, hasher: &mut $crate::rustc_data_structures::stable_hasher::StableHasher,
) { ) {
::std::hash::Hash::hash(self, hasher); ::std::hash::Hash::hash(self, hasher);
} }
@ -204,40 +220,40 @@ impl_stable_hash_via_hash!(char);
impl_stable_hash_via_hash!(()); impl_stable_hash_via_hash!(());
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 { impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher) self.get().hash_stable(ctx, hasher)
} }
} }
impl<CTX> HashStable<CTX> for f32 { impl<CTX> HashStable<CTX> for f32 {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let val: u32 = unsafe { ::std::mem::transmute(*self) }; let val: u32 = unsafe { ::std::mem::transmute(*self) };
val.hash_stable(ctx, hasher); val.hash_stable(ctx, hasher);
} }
} }
impl<CTX> HashStable<CTX> for f64 { impl<CTX> HashStable<CTX> for f64 {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let val: u64 = unsafe { ::std::mem::transmute(*self) }; let val: u64 = unsafe { ::std::mem::transmute(*self) };
val.hash_stable(ctx, hasher); val.hash_stable(ctx, hasher);
} }
} }
impl<CTX> HashStable<CTX> for ::std::cmp::Ordering { impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(*self as i8).hash_stable(ctx, hasher); (*self as i8).hash_stable(ctx, hasher);
} }
} }
impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) { impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0,) = *self; let (ref _0,) = *self;
_0.hash_stable(ctx, hasher); _0.hash_stable(ctx, hasher);
} }
} }
impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) { impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0, ref _1) = *self; let (ref _0, ref _1) = *self;
_0.hash_stable(ctx, hasher); _0.hash_stable(ctx, hasher);
_1.hash_stable(ctx, hasher); _1.hash_stable(ctx, hasher);
@ -250,7 +266,7 @@ where
T2: HashStable<CTX>, T2: HashStable<CTX>,
T3: HashStable<CTX>, T3: HashStable<CTX>,
{ {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0, ref _1, ref _2) = *self; let (ref _0, ref _1, ref _2) = *self;
_0.hash_stable(ctx, hasher); _0.hash_stable(ctx, hasher);
_1.hash_stable(ctx, hasher); _1.hash_stable(ctx, hasher);
@ -265,7 +281,7 @@ where
T3: HashStable<CTX>, T3: HashStable<CTX>,
T4: HashStable<CTX>, T4: HashStable<CTX>,
{ {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0, ref _1, ref _2, ref _3) = *self; let (ref _0, ref _1, ref _2, ref _3) = *self;
_0.hash_stable(ctx, hasher); _0.hash_stable(ctx, hasher);
_1.hash_stable(ctx, hasher); _1.hash_stable(ctx, hasher);
@ -275,7 +291,7 @@ where
} }
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] { impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.len().hash_stable(ctx, hasher); self.len().hash_stable(ctx, hasher);
for item in self { for item in self {
item.hash_stable(ctx, hasher); item.hash_stable(ctx, hasher);
@ -285,35 +301,35 @@ impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> { impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(&self[..]).hash_stable(ctx, hasher); (&self[..]).hash_stable(ctx, hasher);
} }
} }
impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> { impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(**self).hash_stable(ctx, hasher); (**self).hash_stable(ctx, hasher);
} }
} }
impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> { impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(**self).hash_stable(ctx, hasher); (**self).hash_stable(ctx, hasher);
} }
} }
impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> { impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(**self).hash_stable(ctx, hasher); (**self).hash_stable(ctx, hasher);
} }
} }
impl<CTX> HashStable<CTX> for str { impl<CTX> HashStable<CTX> for str {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, _: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) {
self.len().hash(hasher); self.len().hash(hasher);
self.as_bytes().hash(hasher); self.as_bytes().hash(hasher);
} }
@ -321,7 +337,7 @@ impl<CTX> HashStable<CTX> for str {
impl<CTX> HashStable<CTX> for String { impl<CTX> HashStable<CTX> for String {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
(&self[..]).hash_stable(hcx, hasher); (&self[..]).hash_stable(hcx, hasher);
} }
} }
@ -337,7 +353,7 @@ impl<HCX> ToStableHashKey<HCX> for String {
impl<CTX> HashStable<CTX> for bool { impl<CTX> HashStable<CTX> for bool {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher); (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher);
} }
} }
@ -347,7 +363,7 @@ where
T: HashStable<CTX>, T: HashStable<CTX>,
{ {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
if let Some(ref value) = *self { if let Some(ref value) = *self {
1u8.hash_stable(ctx, hasher); 1u8.hash_stable(ctx, hasher);
value.hash_stable(ctx, hasher); value.hash_stable(ctx, hasher);
@ -363,7 +379,7 @@ where
T2: HashStable<CTX>, T2: HashStable<CTX>,
{ {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(ctx, hasher); mem::discriminant(self).hash_stable(ctx, hasher);
match *self { match *self {
Ok(ref x) => x.hash_stable(ctx, hasher), Ok(ref x) => x.hash_stable(ctx, hasher),
@ -377,14 +393,14 @@ where
T: HashStable<CTX> + ?Sized, T: HashStable<CTX> + ?Sized,
{ {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, ctx: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(**self).hash_stable(ctx, hasher); (**self).hash_stable(ctx, hasher);
} }
} }
impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> { impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, _: &mut CTX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) {
::std::hash::Hash::hash(self, hasher); ::std::hash::Hash::hash(self, hasher);
} }
} }
@ -412,25 +428,36 @@ impl_stable_hash_via_hash!(::std::path::PathBuf);
impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R> impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
where where
K: ToStableHashKey<HCX> + Eq + Hash, K: ToStableHashKey<HCX> + Eq,
V: HashStable<HCX>, V: HashStable<HCX>,
R: BuildHasher, R: BuildHasher,
{ {
#[inline] #[inline]
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut HCX, hasher: &mut StableHasher<W>) { 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);
},
);
} }
} }
impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R> impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
where where
K: ToStableHashKey<HCX> + Eq + Hash, K: ToStableHashKey<HCX> + Eq,
R: BuildHasher, R: BuildHasher,
{ {
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut HCX, hasher: &mut StableHasher<W>) { 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(); stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
keys.sort_unstable(); let key = key.to_stable_hash_key(hcx);
keys.hash_stable(hcx, hasher); key.hash_stable(hcx, hasher);
});
} }
} }
@ -439,13 +466,18 @@ where
K: ToStableHashKey<HCX>, K: ToStableHashKey<HCX>,
V: HashStable<HCX>, V: HashStable<HCX>,
{ {
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut HCX, hasher: &mut StableHasher<W>) { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
let mut entries: Vec<_> = self stable_hash_reduce(
.iter() hcx,
.map(|(k, v)| (k.to_stable_hash_key(hcx), v)) hasher,
.collect(); self.iter(),
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2)); self.len(),
entries.hash_stable(hcx, hasher); |hasher, hcx, (key, value)| {
let key = key.to_stable_hash_key(hcx);
key.hash_stable(hcx, hasher);
value.hash_stable(hcx, hasher);
},
);
} }
} }
@ -453,30 +485,39 @@ impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
where where
K: ToStableHashKey<HCX>, K: ToStableHashKey<HCX>,
{ {
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut HCX, hasher: &mut StableHasher<W>) { 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(); stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
keys.sort_unstable(); let key = key.to_stable_hash_key(hcx);
keys.hash_stable(hcx, hasher); key.hash_stable(hcx, hasher);
});
} }
} }
pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>( fn stable_hash_reduce<HCX, I, C, F>(
hcx: &mut HCX, hcx: &mut HCX,
hasher: &mut StableHasher<W>, hasher: &mut StableHasher,
map: &::std::collections::HashMap<K, V, R>, mut collection: C,
to_stable_hash_key: F, length: usize,
hash_function: F,
) where ) where
K: Eq + Hash, C: Iterator<Item = I>,
V: HashStable<HCX>, F: Fn(&mut StableHasher, &mut HCX, I),
R: BuildHasher,
SK: HashStable<HCX> + Ord + Clone,
F: Fn(&K, &HCX) -> SK,
W: StableHasherResult,
{ {
let mut entries: Vec<_> = map length.hash_stable(hcx, hasher);
.iter()
.map(|(k, v)| (to_stable_hash_key(k, hcx), v)) match length {
.collect(); 1 => {
entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2)); hash_function(hasher, hcx, collection.next().unwrap());
entries.hash_stable(hcx, hasher); }
_ => {
let hash = collection
.map(|value| {
let mut hasher = StableHasher::new();
hash_function(&mut hasher, hcx, value);
hasher.finish::<u128>()
})
.reduce(|accum, value| accum.wrapping_add(value));
hash.hash_stable(hcx, hasher);
}
}
} }

View File

@ -678,12 +678,12 @@ impl SourceFile {
remove_bom(&mut src); remove_bom(&mut src);
let src_hash = { let src_hash = {
let mut hasher: StableHasher<u128> = StableHasher::new(); let mut hasher: StableHasher = StableHasher::new();
hasher.write(src.as_bytes()); hasher.write(src.as_bytes());
hasher.finish() hasher.finish()
}; };
let name_hash = { let name_hash = {
let mut hasher: StableHasher<u128> = StableHasher::new(); let mut hasher: StableHasher = StableHasher::new();
name.hash(&mut hasher); name.hash(&mut hasher);
hasher.finish() hasher.finish()
}; };