rhai/src/func/hashing.rs

197 lines
5.1 KiB
Rust
Raw Normal View History

2021-06-16 12:36:33 +02:00
//! Module containing utilities to hash functions and function calls.
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
any::TypeId,
hash::{BuildHasher, Hash, Hasher},
iter::empty,
};
2021-12-06 03:34:13 +01:00
/// Dummy hash value to map zeros to. This value can be anything.
///
/// # Notes
///
/// Hashes are `u64`, and they can be zero (although extremely unlikely).
/// It is possible to hijack the zero value to indicate non-existence,
/// like [`None`] in [`Option<u64>`].
///
/// When a hash is calculated to be zero, it gets mapped to this alternate hash value.
/// This has the effect of releasing the zero value at the expense of causing the probability of
/// this value to double, which has minor impacts.
pub const ALT_ZERO_HASH: u64 = 42;
2021-12-05 10:05:19 +01:00
/// A hasher that only takes one single [`u64`] and returns it as a non-zero hash key.
///
/// # Zeros
///
2021-12-06 03:34:13 +01:00
/// If the value is zero, it is mapped to `ALT_ZERO_HASH`.
2021-06-16 12:36:33 +02:00
///
/// # Panics
///
/// Panics when hashing any data type other than a [`u64`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
2021-11-16 05:26:37 +01:00
struct StraightHasher(u64);
2021-06-16 12:36:33 +02:00
impl Hasher for StraightHasher {
#[inline(always)]
fn finish(&self) -> u64 {
self.0
}
#[inline]
2021-06-16 12:36:33 +02:00
fn write(&mut self, bytes: &[u8]) {
assert_eq!(bytes.len(), 8, "StraightHasher can only hash u64 values");
let mut key = [0_u8; 8];
key.copy_from_slice(bytes);
self.0 = u64::from_ne_bytes(key);
2021-12-05 10:05:19 +01:00
if self.0 == 0 {
2021-12-06 03:34:13 +01:00
self.0 = ALT_ZERO_HASH
2021-12-05 10:05:19 +01:00
}
2021-06-16 12:36:33 +02:00
}
}
/// A hash builder for `StraightHasher`.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
2021-11-16 05:26:37 +01:00
struct StraightHasherBuilder;
2021-06-16 12:36:33 +02:00
impl BuildHasher for StraightHasherBuilder {
type Hasher = StraightHasher;
#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
2021-12-06 03:34:13 +01:00
StraightHasher(ALT_ZERO_HASH)
2021-06-16 12:36:33 +02:00
}
}
/// Create an instance of the default hasher.
#[inline(always)]
#[must_use]
pub fn get_hasher() -> ahash::AHasher {
2021-11-27 07:24:06 +01:00
ahash::AHasher::default()
2021-06-16 12:36:33 +02:00
}
2021-12-05 10:05:19 +01:00
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified variable name.
2021-06-30 10:28:37 +02:00
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
2021-12-05 10:05:19 +01:00
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
///
2021-06-30 10:28:37 +02:00
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
2021-11-28 05:41:20 +01:00
pub fn calc_qualified_var_hash<'a>(
modules: impl Iterator<Item = impl AsRef<str> + 'a>,
var_name: impl AsRef<str>,
) -> u64 {
2021-06-30 10:28:37 +02:00
let s = &mut get_hasher();
// We always skip the first module
let mut len = 0;
modules
.inspect(|_| len += 1)
.skip(1)
2021-11-28 05:41:20 +01:00
.for_each(|m| m.as_ref().hash(s));
2021-06-30 10:28:37 +02:00
len.hash(s);
2021-11-28 05:41:20 +01:00
var_name.as_ref().hash(s);
2021-12-05 10:05:19 +01:00
match s.finish() {
2021-12-06 03:34:13 +01:00
0 => ALT_ZERO_HASH,
2021-12-05 10:05:19 +01:00
r => r,
}
2021-06-30 10:28:37 +02:00
}
2021-12-05 10:05:19 +01:00
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name
2021-06-16 12:36:33 +02:00
/// and the number of parameters, but no parameter types.
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
2021-12-05 10:05:19 +01:00
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
///
2021-06-16 12:36:33 +02:00
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
2021-11-28 05:41:20 +01:00
pub fn calc_qualified_fn_hash(
modules: impl Iterator<Item = impl AsRef<str>>,
2021-11-27 16:04:45 +01:00
fn_name: impl AsRef<str>,
2021-06-16 12:36:33 +02:00
num: usize,
) -> u64 {
let s = &mut get_hasher();
// We always skip the first module
let mut len = 0;
modules
.inspect(|_| len += 1)
.skip(1)
2021-11-28 05:41:20 +01:00
.for_each(|m| m.as_ref().hash(s));
2021-06-16 12:36:33 +02:00
len.hash(s);
2021-11-27 16:04:45 +01:00
fn_name.as_ref().hash(s);
2021-06-16 12:36:33 +02:00
num.hash(s);
2021-12-05 10:05:19 +01:00
match s.finish() {
2021-12-06 03:34:13 +01:00
0 => ALT_ZERO_HASH,
2021-12-05 10:05:19 +01:00
r => r,
}
2021-06-16 12:36:33 +02:00
}
2021-12-05 10:05:19 +01:00
/// Calculate a non-zero [`u64`] hash key from a non-namespace-qualified function name
2021-06-16 12:36:33 +02:00
/// and the number of parameters, but no parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
2021-12-05 10:05:19 +01:00
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
2021-06-16 12:36:33 +02:00
#[inline(always)]
#[must_use]
2021-11-27 16:04:45 +01:00
pub fn calc_fn_hash(fn_name: impl AsRef<str>, num: usize) -> u64 {
2021-11-28 05:41:20 +01:00
calc_qualified_fn_hash(empty::<&str>(), fn_name, num)
2021-06-16 12:36:33 +02:00
}
2021-12-05 10:05:19 +01:00
/// Calculate a non-zero [`u64`] hash key from a list of parameter types.
2021-06-16 12:36:33 +02:00
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
2021-12-05 10:05:19 +01:00
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
2021-06-16 12:36:33 +02:00
#[inline]
#[must_use]
pub fn calc_fn_params_hash(params: impl Iterator<Item = TypeId>) -> u64 {
let s = &mut get_hasher();
let mut len = 0;
2021-11-16 06:15:43 +01:00
params.inspect(|_| len += 1).for_each(|t| t.hash(s));
2021-06-16 12:36:33 +02:00
len.hash(s);
2021-12-05 10:05:19 +01:00
match s.finish() {
2021-12-06 03:34:13 +01:00
0 => ALT_ZERO_HASH,
2021-12-05 10:05:19 +01:00
r => r,
}
2021-06-16 12:36:33 +02:00
}
/// Combine two [`u64`] hashes by taking the XOR of them.
2021-12-05 10:05:19 +01:00
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
2021-06-16 12:36:33 +02:00
#[inline(always)]
#[must_use]
2021-11-16 05:26:37 +01:00
pub const fn combine_hashes(a: u64, b: u64) -> u64 {
2021-12-05 10:05:19 +01:00
match a ^ b {
2021-12-06 03:34:13 +01:00
0 => ALT_ZERO_HASH,
2021-12-05 10:05:19 +01:00
r => r,
}
2021-06-16 12:36:33 +02:00
}