rhai/src/func/hashing.rs

198 lines
5.3 KiB
Rust
Raw Normal View History

2021-06-16 18:36:33 +08:00
//! Module containing utilities to hash functions and function calls.
use crate::config;
2021-06-16 18:36:33 +08:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
any::TypeId,
hash::{BuildHasher, Hash, Hasher},
};
2022-09-05 19:22:30 +09:00
#[cfg(feature = "no_std")]
2022-09-12 23:08:38 +08:00
pub type StraightHashMap<V> = hashbrown::HashMap<u64, V, StraightHasherBuilder>;
2022-09-05 19:22:30 +09:00
#[cfg(not(feature = "no_std"))]
2022-09-12 23:08:38 +08:00
pub type StraightHashMap<V> = std::collections::HashMap<u64, V, StraightHasherBuilder>;
2021-12-06 10:34:13 +08: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 17:05:19 +08:00
/// A hasher that only takes one single [`u64`] and returns it as a non-zero hash key.
///
/// # Zeros
///
2021-12-06 10:34:13 +08:00
/// If the value is zero, it is mapped to `ALT_ZERO_HASH`.
2021-06-16 18:36:33 +08:00
///
/// # Panics
///
/// Panics when hashing any data type other than a [`u64`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct StraightHasher(u64);
2021-06-16 18:36:33 +08:00
impl Hasher for StraightHasher {
#[inline(always)]
2022-09-28 12:06:22 +08:00
#[must_use]
2021-06-16 18:36:33 +08:00
fn finish(&self) -> u64 {
self.0
}
2022-09-05 21:17:07 +08:00
#[inline(always)]
fn write(&mut self, _bytes: &[u8]) {
panic!("StraightHasher can only hash u64 values");
}
2022-09-05 21:17:07 +08:00
#[inline(always)]
fn write_u64(&mut self, i: u64) {
2022-09-05 19:22:30 +09:00
if i == 0 {
self.0 = ALT_ZERO_HASH;
} else {
self.0 = i;
}
2021-06-16 18:36:33 +08:00
}
}
/// A hash builder for `StraightHasher`.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct StraightHasherBuilder;
2021-06-16 18:36:33 +08:00
impl BuildHasher for StraightHasherBuilder {
type Hasher = StraightHasher;
#[inline(always)]
2022-09-28 12:06:22 +08:00
#[must_use]
2021-06-16 18:36:33 +08:00
fn build_hasher(&self) -> Self::Hasher {
2021-12-06 10:34:13 +08:00
StraightHasher(ALT_ZERO_HASH)
2021-06-16 18:36:33 +08:00
}
}
/// Create an instance of the default hasher.
#[inline(always)]
#[must_use]
pub fn get_hasher() -> ahash::AHasher {
2022-10-10 16:46:35 +08:00
match config::AHASH_SEED {
Some([seed1, seed2, seed3, seed4]) if seed1 | seed2 | seed3 | seed4 != 0 => {
ahash::RandomState::with_seeds(seed1, seed2, seed3, seed4).build_hasher()
}
2022-10-10 16:46:35 +08:00
_ => ahash::AHasher::default(),
2022-09-26 23:45:50 +08:00
}
2021-06-16 18:36:33 +08:00
}
2021-12-05 17:05:19 +08:00
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified variable name.
2021-06-30 16:28:37 +08: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 17:05:19 +08:00
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
///
2021-06-30 16:28:37 +08:00
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
2022-09-21 11:46:23 +08:00
pub fn calc_var_hash<'a>(
2022-09-06 14:16:15 +09:00
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
2022-06-11 16:01:15 +08:00
var_name: &str,
) -> u64 {
2021-06-30 16:28:37 +08:00
let s = &mut get_hasher();
// We always skip the first module
2022-09-06 14:16:15 +09:00
let iter = modules.into_iter();
let len = iter.len();
iter.skip(1).for_each(|m| m.hash(s));
2021-06-30 16:28:37 +08:00
len.hash(s);
2022-01-04 15:22:48 +08:00
var_name.hash(s);
2021-12-05 17:05:19 +08:00
match s.finish() {
2021-12-06 10:34:13 +08:00
0 => ALT_ZERO_HASH,
2021-12-05 17:05:19 +08:00
r => r,
}
2021-06-30 16:28:37 +08:00
}
2021-12-05 17:05:19 +08:00
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name
2021-06-16 18:36:33 +08:00
/// and the number of parameters, but no parameter types.
///
2022-09-21 11:46:23 +08:00
/// Module names making up the namespace are passed in via `&str` references from an iterator.
2021-06-16 18:36:33 +08:00
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
2022-09-21 11:46:23 +08:00
/// If the function is not namespace-qualified, pass [`None`] as the namespace.
///
2021-12-05 17:05:19 +08:00
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
///
2021-06-16 18:36:33 +08:00
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
2022-09-21 11:46:23 +08:00
pub fn calc_fn_hash<'a>(
namespace: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
2022-01-04 15:22:48 +08:00
fn_name: &str,
2021-06-16 18:36:33 +08:00
num: usize,
) -> u64 {
let s = &mut get_hasher();
// We always skip the first module
2022-09-21 11:46:23 +08:00
let iter = namespace.into_iter();
2022-09-06 14:16:15 +09:00
let len = iter.len();
iter.skip(1).for_each(|m| m.hash(s));
2021-06-16 18:36:33 +08:00
len.hash(s);
2022-01-04 15:22:48 +08:00
fn_name.hash(s);
2021-06-16 18:36:33 +08:00
num.hash(s);
2021-12-05 17:05:19 +08:00
match s.finish() {
2021-12-06 10:34:13 +08:00
0 => ALT_ZERO_HASH,
2021-12-05 17:05:19 +08:00
r => r,
}
2021-06-16 18:36:33 +08:00
}
2021-12-05 17:05:19 +08:00
/// Calculate a non-zero [`u64`] hash key from a list of parameter types.
2021-06-16 18:36:33 +08:00
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
2021-12-05 17:05:19 +08:00
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
2021-06-16 18:36:33 +08:00
#[inline]
#[must_use]
2022-09-06 14:16:15 +09:00
pub fn calc_fn_params_hash(
params: impl IntoIterator<Item = TypeId, IntoIter = impl ExactSizeIterator<Item = TypeId>>,
) -> u64 {
2021-06-16 18:36:33 +08:00
let s = &mut get_hasher();
2022-09-06 14:16:15 +09:00
let iter = params.into_iter();
let len = iter.len();
iter.for_each(|t| {
2022-07-27 18:04:59 +08:00
t.hash(s);
2022-07-27 16:04:24 +08:00
});
2021-06-16 18:36:33 +08:00
len.hash(s);
2021-12-05 17:05:19 +08:00
match s.finish() {
2021-12-06 10:34:13 +08:00
0 => ALT_ZERO_HASH,
2021-12-05 17:05:19 +08:00
r => r,
}
2021-06-16 18:36:33 +08:00
}
/// Combine two [`u64`] hashes by taking the XOR of them.
2021-12-05 17:05:19 +08:00
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
2021-06-16 18:36:33 +08:00
#[inline(always)]
#[must_use]
2021-11-16 12:26:37 +08:00
pub const fn combine_hashes(a: u64, b: u64) -> u64 {
2021-12-05 17:05:19 +08:00
match a ^ b {
2021-12-06 10:34:13 +08:00
0 => ALT_ZERO_HASH,
2021-12-05 17:05:19 +08:00
r => r,
}
2021-06-16 18:36:33 +08:00
}