Revise function hashing.
This commit is contained in:
94
src/utils.rs
94
src/utils.rs
@@ -10,8 +10,7 @@ use crate::stdlib::{
|
||||
fmt,
|
||||
fmt::{Debug, Display},
|
||||
hash::{BuildHasher, Hash, Hasher},
|
||||
iter::{empty, FromIterator},
|
||||
num::NonZeroU64,
|
||||
iter::FromIterator,
|
||||
ops::{Add, AddAssign, Deref, DerefMut, Sub, SubAssign},
|
||||
str::FromStr,
|
||||
string::{String, ToString},
|
||||
@@ -19,18 +18,18 @@ use crate::stdlib::{
|
||||
};
|
||||
use crate::Shared;
|
||||
|
||||
/// A hasher that only takes one single [`NonZeroU64`] and returns it as a hash key.
|
||||
/// A hasher that only takes one single [`u64`] and returns it as a hash key.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics when hashing any data type other than a [`NonZeroU64`].
|
||||
/// Panics when hashing any data type other than a [`u64`].
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct StraightHasher(NonZeroU64);
|
||||
pub struct StraightHasher(u64);
|
||||
|
||||
impl Hasher for StraightHasher {
|
||||
#[inline(always)]
|
||||
fn finish(&self) -> u64 {
|
||||
self.0.get()
|
||||
self.0
|
||||
}
|
||||
#[inline(always)]
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
@@ -39,9 +38,7 @@ impl Hasher for StraightHasher {
|
||||
let mut key = [0_u8; 8];
|
||||
key.copy_from_slice(bytes);
|
||||
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
self.0 = NonZeroU64::new(u64::from_ne_bytes(key))
|
||||
.unwrap_or_else(|| NonZeroU64::new(42).unwrap());
|
||||
self.0 = u64::from_ne_bytes(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +51,7 @@ impl BuildHasher for StraightHasherBuilder {
|
||||
|
||||
#[inline(always)]
|
||||
fn build_hasher(&self) -> Self::Hasher {
|
||||
StraightHasher(NonZeroU64::new(42).unwrap())
|
||||
StraightHasher(42)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,26 +61,7 @@ pub fn get_hasher() -> ahash::AHasher {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// _(INTERNALS)_ Calculate a [`NonZeroU64`] hash key from a namespace-qualified function name and
|
||||
/// parameter types.
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
/// Module names are passed in via `&str` references from an iterator.
|
||||
/// Parameter types are passed in via [`TypeId`] values from an iterator.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
|
||||
#[inline(always)]
|
||||
pub fn calc_native_fn_hash<'a>(
|
||||
modules: impl Iterator<Item = &'a str>,
|
||||
fn_name: &str,
|
||||
params: impl Iterator<Item = TypeId>,
|
||||
) -> Option<NonZeroU64> {
|
||||
calc_fn_hash(modules, fn_name, None, params)
|
||||
}
|
||||
|
||||
/// _(INTERNALS)_ Calculate a [`NonZeroU64`] hash key from a namespace-qualified function name
|
||||
/// _(INTERNALS)_ Calculate a [`u64`] hash key from a namespace-qualified function name
|
||||
/// and the number of parameters, but no parameter types.
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
@@ -94,29 +72,11 @@ pub fn calc_native_fn_hash<'a>(
|
||||
///
|
||||
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
|
||||
#[inline(always)]
|
||||
pub fn calc_script_fn_hash<'a>(
|
||||
modules: impl Iterator<Item = &'a str>,
|
||||
fn_name: &str,
|
||||
num: usize,
|
||||
) -> Option<NonZeroU64> {
|
||||
calc_fn_hash(modules, fn_name, Some(num), empty())
|
||||
}
|
||||
|
||||
/// Calculate a [`NonZeroU64`] hash key from a namespace-qualified function name and parameter types.
|
||||
///
|
||||
/// Module names are passed in via `&str` references from an iterator.
|
||||
/// Parameter types are passed in via [`TypeId`] values from an iterator.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
|
||||
#[inline(always)]
|
||||
fn calc_fn_hash<'a>(
|
||||
pub fn calc_fn_hash<'a>(
|
||||
mut modules: impl Iterator<Item = &'a str>,
|
||||
fn_name: &str,
|
||||
num: Option<usize>,
|
||||
params: impl Iterator<Item = TypeId>,
|
||||
) -> Option<NonZeroU64> {
|
||||
num: usize,
|
||||
) -> u64 {
|
||||
let s = &mut get_hasher();
|
||||
|
||||
// Hash a boolean indicating whether the hash is namespace-qualified.
|
||||
@@ -124,20 +84,30 @@ fn calc_fn_hash<'a>(
|
||||
// We always skip the first module
|
||||
modules.for_each(|m| m.hash(s));
|
||||
fn_name.hash(s);
|
||||
if let Some(num) = num {
|
||||
num.hash(s);
|
||||
} else {
|
||||
params.for_each(|t| t.hash(s));
|
||||
}
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
NonZeroU64::new(s.finish()).or_else(|| NonZeroU64::new(42))
|
||||
num.hash(s);
|
||||
s.finish()
|
||||
}
|
||||
|
||||
/// Combine two [`NonZeroU64`] hashes by taking the XOR of them.
|
||||
/// _(INTERNALS)_ Calculate a [`u64`] hash key from a list of parameter types.
|
||||
/// Exported under the `internals` feature only.
|
||||
///
|
||||
/// Parameter types are passed in via [`TypeId`] values from an iterator.
|
||||
#[inline(always)]
|
||||
pub(crate) fn combine_hashes(a: NonZeroU64, b: NonZeroU64) -> NonZeroU64 {
|
||||
// HACK - If it so happens to hash directly to zero (OMG!) then change it to 42...
|
||||
NonZeroU64::new(a.get() ^ b.get()).unwrap_or_else(|| NonZeroU64::new(42).unwrap())
|
||||
pub fn calc_fn_params_hash(params: impl Iterator<Item = TypeId>) -> u64 {
|
||||
let s = &mut get_hasher();
|
||||
let mut len = 0;
|
||||
params.for_each(|t| {
|
||||
t.hash(s);
|
||||
len += 1;
|
||||
});
|
||||
len.hash(s);
|
||||
s.finish()
|
||||
}
|
||||
|
||||
/// Combine two [`u64`] hashes by taking the XOR of them.
|
||||
#[inline(always)]
|
||||
pub(crate) fn combine_hashes(a: u64, b: u64) -> u64 {
|
||||
a ^ b
|
||||
}
|
||||
|
||||
/// _(INTERNALS)_ A type that wraps a [`HashMap`] and implements [`Hash`].
|
||||
|
Reference in New Issue
Block a user