Skip to content

Commit

Permalink
Use Blake2 for the type-id hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Aug 16, 2024
1 parent e0d6581 commit 612b60a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::RefCell;

use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stable_hasher::{HashStable, StableBlake2sHasher256};
use rustc_macros::HashStable;
use rustc_middle::bug;
use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
///
/// Right now this takes the form of a hex-encoded opaque hash value.
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
let mut hasher = StableHasher::new();
let mut hasher = StableBlake2sHasher256::new();
tcx.with_stable_hashing_context(|mut hcx| {
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
});
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_data_structures/src/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::hash::{Hash, Hasher};

use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_stable_hash::hashers::Blake2s256Hash;

use crate::stable_hasher::{
impl_stable_traits_for_trivial_type, FromStableHash, Hash64, StableHasherHash,
Expand Down Expand Up @@ -164,6 +165,20 @@ impl FromStableHash<StableHasherHash> for Fingerprint {
}
}

impl FromStableHash<Blake2s256Hash> for Fingerprint {
#[inline]
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
}
}

impl_stable_traits_for_trivial_type!(Fingerprint);

impl<E: Encoder> Encodable<E> for Fingerprint {
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_data_structures/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::fmt;
use std::ops::BitXorAssign;

use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_stable_hash::hashers::Blake2s256Hash;

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

Expand Down Expand Up @@ -130,6 +131,18 @@ impl FromStableHash<StableHasherHash> for Hash128 {
}
}

impl FromStableHash<Blake2s256Hash> for Hash128 {
#[inline]
fn from(Blake2s256Hash(bytes): Blake2s256Hash) -> Self {
let p0 = u128::from_le_bytes(bytes[0..16].try_into().unwrap());
let p1 = u128::from_le_bytes(bytes[16..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
Self { inner: p0.wrapping_mul(3).wrapping_add(p1) }
}
}

impl fmt::Debug for Hash128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use smallvec::SmallVec;
#[cfg(test)]
mod tests;

pub use rustc_stable_hash::hashers::StableBlake2sHasher256;
pub use rustc_stable_hash::{
FromStableHash, IntoStableHash, SipHasher128Hash as StableHasherHash,
StableHasher as GenericStableHasher, StableSipHasher128 as StableHasher,
Expand All @@ -21,6 +22,7 @@ pub trait ExtendedHasher:
}

impl ExtendedHasher for rustc_stable_hash::hashers::SipHasher128 {}
impl ExtendedHasher for rustc_stable_hash::hashers::Blake2sHasher256 {}

use crate::fingerprint::Fingerprint;
pub use crate::hashes::{Hash128, Hash64};
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{fmt, iter};

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

self.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
let mut hasher = StableBlake2sHasher256::new();
hcx.while_hashing_spans(false, |hcx| ty.hash_stable(hcx, &mut hasher));
hasher.finish()
})
Expand Down

0 comments on commit 612b60a

Please sign in to comment.