Skip to content

Commit 612b60a

Browse files
committed
Use Blake2 for the type-id hash
1 parent e0d6581 commit 612b60a

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::RefCell;
22

33
use rustc_data_structures::fingerprint::Fingerprint;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5+
use rustc_data_structures::stable_hasher::{HashStable, StableBlake2sHasher256};
66
use rustc_macros::HashStable;
77
use rustc_middle::bug;
88
use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
@@ -94,7 +94,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
9494
///
9595
/// Right now this takes the form of a hex-encoded opaque hash value.
9696
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
97-
let mut hasher = StableHasher::new();
97+
let mut hasher = StableBlake2sHasher256::new();
9898
tcx.with_stable_hashing_context(|mut hcx| {
9999
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
100100
});

compiler/rustc_data_structures/src/fingerprint.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::hash::{Hash, Hasher};
22

33
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4+
use rustc_stable_hash::hashers::Blake2s256Hash;
45

56
use crate::stable_hasher::{
67
impl_stable_traits_for_trivial_type, FromStableHash, Hash64, StableHasherHash,
@@ -164,6 +165,20 @@ impl FromStableHash<StableHasherHash> for Fingerprint {
164165
}
165166
}
166167

168+
impl FromStableHash<Blake2s256Hash> for Fingerprint {
169+
#[inline]
170+
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
171+
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
172+
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
173+
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
174+
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());
175+
176+
// See https://stackoverflow.com/a/27952689 on why this function is
177+
// implemented this way.
178+
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
179+
}
180+
}
181+
167182
impl_stable_traits_for_trivial_type!(Fingerprint);
168183

169184
impl<E: Encoder> Encodable<E> for Fingerprint {

compiler/rustc_data_structures/src/hashes.rs

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fmt;
1515
use std::ops::BitXorAssign;
1616

1717
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
18+
use rustc_stable_hash::hashers::Blake2s256Hash;
1819

1920
use crate::stable_hasher::{FromStableHash, StableHasherHash};
2021

@@ -130,6 +131,18 @@ impl FromStableHash<StableHasherHash> for Hash128 {
130131
}
131132
}
132133

134+
impl FromStableHash<Blake2s256Hash> for Hash128 {
135+
#[inline]
136+
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
137+
let p0 = u128::from_le_bytes(bytes[0..16].try_into().unwrap());
138+
let p1 = u128::from_le_bytes(bytes[16..32].try_into().unwrap());
139+
140+
// See https://stackoverflow.com/a/27952689 on why this function is
141+
// implemented this way.
142+
Self { inner: p0.wrapping_mul(3).wrapping_add(p1) }
143+
}
144+
}
145+
133146
impl fmt::Debug for Hash128 {
134147
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135148
self.inner.fmt(f)

compiler/rustc_data_structures/src/stable_hasher.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use smallvec::SmallVec;
1010
#[cfg(test)]
1111
mod tests;
1212

13+
pub use rustc_stable_hash::hashers::StableBlake2sHasher256;
1314
pub use rustc_stable_hash::{
1415
FromStableHash, IntoStableHash, SipHasher128Hash as StableHasherHash,
1516
StableHasher as GenericStableHasher, StableSipHasher128 as StableHasher,
@@ -21,6 +22,7 @@ pub trait ExtendedHasher:
2122
}
2223

2324
impl ExtendedHasher for rustc_stable_hash::hashers::SipHasher128 {}
25+
impl ExtendedHasher for rustc_stable_hash::hashers::Blake2sHasher256 {}
2426

2527
use crate::fingerprint::Fingerprint;
2628
pub use crate::hashes::{Hash128, Hash64};

compiler/rustc_middle/src/ty/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{fmt, iter};
44

55
use rustc_apfloat::Float as _;
66
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7-
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
7+
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableBlake2sHasher256};
88
use rustc_data_structures::stack::ensure_sufficient_stack;
99
use rustc_errors::ErrorGuaranteed;
1010
use rustc_hir as hir;
@@ -134,7 +134,7 @@ impl<'tcx> TyCtxt<'tcx> {
134134
let ty = self.erase_regions(ty);
135135

136136
self.with_stable_hashing_context(|mut hcx| {
137-
let mut hasher = StableHasher::new();
137+
let mut hasher = StableBlake2sHasher256::new();
138138
hcx.while_hashing_spans(false, |hcx| ty.hash_stable(hcx, &mut hasher));
139139
hasher.finish()
140140
})

0 commit comments

Comments
 (0)