Refine hashing.

This commit is contained in:
Stephen Chung 2023-01-31 16:37:50 +08:00
parent 08f428def2
commit faa48f78c3
2 changed files with 28 additions and 26 deletions

View File

@ -98,7 +98,7 @@ pub struct Engine {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub(crate) module_resolver: Box<dyn crate::ModuleResolver>, pub(crate) module_resolver: Box<dyn crate::ModuleResolver>,
/// An empty [`ImmutableString`] for cloning purposes. /// Strings interner.
pub(crate) interned_strings: Locked<StringsInterner>, pub(crate) interned_strings: Locked<StringsInterner>,
/// A set of symbols to disable. /// A set of symbols to disable.

View File

@ -51,11 +51,7 @@ impl Hasher for StraightHasher {
} }
#[inline(always)] #[inline(always)]
fn write_u64(&mut self, i: u64) { fn write_u64(&mut self, i: u64) {
if i == 0 { self.0 = i;
self.0 = ALT_ZERO_HASH;
} else {
self.0 = i;
}
} }
} }
@ -69,7 +65,7 @@ impl BuildHasher for StraightHasherBuilder {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
fn build_hasher(&self) -> Self::Hasher { fn build_hasher(&self) -> Self::Hasher {
StraightHasher(ALT_ZERO_HASH) StraightHasher(0)
} }
} }
@ -99,16 +95,19 @@ pub fn get_hasher() -> ahash::AHasher {
/// The first module name is skipped. Hashing starts from the _second_ module in the chain. /// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_var_hash<'a>( pub fn calc_var_hash<'a>(namespace: impl IntoIterator<Item = &'a str>, var_name: &str) -> u64 {
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
var_name: &str,
) -> u64 {
let s = &mut get_hasher(); let s = &mut get_hasher();
let mut count = 0;
// We always skip the first module // We always skip the first module
let iter = modules.into_iter(); namespace.into_iter().for_each(|m| {
iter.len().hash(s); // We always skip the first module
iter.skip(1).for_each(|m| m.hash(s)); if count > 0 {
m.hash(s);
}
count += 1;
});
count.hash(s);
var_name.hash(s); var_name.hash(s);
match s.finish() { match s.finish() {
@ -135,16 +134,21 @@ pub fn calc_var_hash<'a>(
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_fn_hash<'a>( pub fn calc_fn_hash<'a>(
namespace: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>, namespace: impl IntoIterator<Item = &'a str>,
fn_name: &str, fn_name: &str,
num: usize, num: usize,
) -> u64 { ) -> u64 {
let s = &mut get_hasher(); let s = &mut get_hasher();
let mut count = 0;
// We always skip the first module namespace.into_iter().for_each(|m| {
let iter = namespace.into_iter(); // We always skip the first module
iter.len().hash(s); if count > 0 {
iter.skip(1).for_each(|m| m.hash(s)); m.hash(s);
}
count += 1;
});
count.hash(s);
fn_name.hash(s); fn_name.hash(s);
num.hash(s); num.hash(s);
@ -163,17 +167,15 @@ pub fn calc_fn_hash<'a>(
/// If the hash happens to be zero, it is mapped to `ALT_ZERO_HASH`. /// If the hash happens to be zero, it is mapped to `ALT_ZERO_HASH`.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_fn_hash_full( pub fn calc_fn_hash_full(base: u64, params: impl IntoIterator<Item = TypeId>) -> u64 {
base: u64,
params: impl IntoIterator<Item = TypeId, IntoIter = impl ExactSizeIterator<Item = TypeId>>,
) -> u64 {
let s = &mut get_hasher(); let s = &mut get_hasher();
base.hash(s); base.hash(s);
let iter = params.into_iter(); let mut count = 0;
iter.len().hash(s); params.into_iter().for_each(|t| {
iter.for_each(|t| {
t.hash(s); t.hash(s);
count += 1;
}); });
count.hash(s);
match s.finish() { match s.finish() {
0 => ALT_ZERO_HASH, 0 => ALT_ZERO_HASH,