Remove hashing hack.

This commit is contained in:
Stephen Chung 2023-02-11 11:56:20 +08:00
parent 75718a5a8b
commit e177a5648a
4 changed files with 26 additions and 72 deletions

View File

@ -2,7 +2,6 @@
use super::{ASTFlags, ASTNode, Ident, Namespace, Stmt, StmtBlock};
use crate::engine::{KEYWORD_FN_PTR, OP_EXCLUSIVE_RANGE, OP_INCLUSIVE_RANGE};
use crate::func::hashing::ALT_ZERO_HASH;
use crate::tokenizer::Token;
use crate::types::dynamic::Union;
use crate::{
@ -17,7 +16,7 @@ use std::{
fmt::Write,
hash::Hash,
iter::once,
num::{NonZeroU64, NonZeroU8, NonZeroUsize},
num::{NonZeroU8, NonZeroUsize},
};
/// _(internals)_ A binary expression.
@ -103,9 +102,9 @@ impl CustomExpr {
pub struct FnCallHashes {
/// Pre-calculated hash for a script-defined function ([`None`] if native functions only).
#[cfg(not(feature = "no_function"))]
script: Option<NonZeroU64>,
script: Option<u64>,
/// Pre-calculated hash for a native Rust function with no parameter types.
native: NonZeroU64,
native: u64,
}
impl fmt::Debug for FnCallHashes {
@ -128,8 +127,6 @@ impl fmt::Debug for FnCallHashes {
impl From<u64> for FnCallHashes {
#[inline]
fn from(hash: u64) -> Self {
let hash = NonZeroU64::new(if hash == 0 { ALT_ZERO_HASH } else { hash }).unwrap();
Self {
#[cfg(not(feature = "no_function"))]
script: Some(hash),
@ -146,7 +143,7 @@ impl FnCallHashes {
Self {
#[cfg(not(feature = "no_function"))]
script: None,
native: NonZeroU64::new(if hash == 0 { ALT_ZERO_HASH } else { hash }).unwrap(),
native: hash,
}
}
/// Create a [`FnCallHashes`] with both native Rust and script function hashes.
@ -155,8 +152,8 @@ impl FnCallHashes {
pub fn from_all(#[cfg(not(feature = "no_function"))] script: u64, native: u64) -> Self {
Self {
#[cfg(not(feature = "no_function"))]
script: NonZeroU64::new(if script == 0 { ALT_ZERO_HASH } else { script }),
native: NonZeroU64::new(if native == 0 { ALT_ZERO_HASH } else { native }).unwrap(),
script: Some(script),
native,
}
}
/// Is this [`FnCallHashes`] native-only?
@ -174,7 +171,7 @@ impl FnCallHashes {
#[inline(always)]
#[must_use]
pub const fn native(&self) -> u64 {
self.native.get()
self.native
}
/// Get the script hash.
///
@ -188,7 +185,7 @@ impl FnCallHashes {
#[must_use]
pub fn script(&self) -> u64 {
assert!(self.script.is_some());
self.script.as_ref().unwrap().get()
self.script.unwrap()
}
}

View File

@ -169,10 +169,6 @@ impl Engine {
args: Option<&mut FnCallArgs>,
allow_dynamic: bool,
) -> Option<&'s FnResolutionCacheEntry> {
if hash_base == 0 {
return None;
}
let mut hash = args.as_deref().map_or(hash_base, |args| {
calc_fn_hash_full(hash_base, args.iter().map(|a| a.type_id()))
});

View File

@ -13,25 +13,7 @@ pub type StraightHashMap<V> = hashbrown::HashMap<u64, V, StraightHasherBuilder>;
#[cfg(not(feature = "no_std"))]
pub type StraightHashMap<V> = std::collections::HashMap<u64, V, StraightHasherBuilder>;
/// 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;
/// A hasher that only takes one single [`u64`] and returns it as a non-zero hash key.
///
/// # Zeros
///
/// If the value is zero, it is mapped to `ALT_ZERO_HASH`.
/// A hasher that only takes one single [`u64`] and returns it as a hash key.
///
/// # Panics
///
@ -81,15 +63,11 @@ pub fn get_hasher() -> ahash::AHasher {
}
}
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified variable name.
/// Calculate a [`u64`] hash key from a namespace-qualified variable name.
///
/// Module names are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `ALT_ZERO_HASH`.
///
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
@ -110,13 +88,10 @@ pub fn calc_var_hash<'a>(namespace: impl IntoIterator<Item = &'a str>, var_name:
count.hash(s);
var_name.hash(s);
match s.finish() {
0 => ALT_ZERO_HASH,
r => r,
}
s.finish()
}
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name
/// Calculate a [`u64`] hash key from a namespace-qualified function name
/// and the number of parameters, but no parameter types.
///
/// Module names making up the namespace are passed in via `&str` references from an iterator.
@ -124,10 +99,6 @@ pub fn calc_var_hash<'a>(namespace: impl IntoIterator<Item = &'a str>, var_name:
///
/// If the function is not namespace-qualified, pass [`None`] as the namespace.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `ALT_ZERO_HASH`.
///
/// # Note
///
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
@ -152,19 +123,12 @@ pub fn calc_fn_hash<'a>(
fn_name.hash(s);
num.hash(s);
match s.finish() {
0 => ALT_ZERO_HASH,
r => r,
}
s.finish()
}
/// Calculate a non-zero [`u64`] hash key from a base [`u64`] hash key and a list of parameter types.
/// Calculate a [`u64`] hash key from a base [`u64`] hash key and a list of parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `ALT_ZERO_HASH`.
#[inline]
#[must_use]
pub fn calc_fn_hash_full(base: u64, params: impl IntoIterator<Item = TypeId>) -> u64 {
@ -177,8 +141,5 @@ pub fn calc_fn_hash_full(base: u64, params: impl IntoIterator<Item = TypeId>) ->
});
count.hash(s);
match s.finish() {
0 => ALT_ZERO_HASH,
r => r,
}
s.finish()
}

View File

@ -145,7 +145,7 @@ impl FuncInfo {
}
}
/// _(internals)_ Calculate a non-zero [`u64`] hash key from a namespace-qualified function name and parameter types.
/// _(internals)_ Calculate a [`u64`] 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.
@ -991,7 +991,7 @@ impl Module {
type_id
}
/// Set a Rust function into the [`Module`], returning a non-zero hash key.
/// Set a Rust function into the [`Module`], returning a [`u64`] hash key.
///
/// If there is an existing Rust function of the same hash, it is replaced.
///
@ -1089,7 +1089,7 @@ impl Module {
hash_fn
}
/// _(metadata)_ Set a Rust function into the [`Module`], returning a non-zero hash key.
/// _(metadata)_ Set a Rust function into the [`Module`], returning a [`u64`] hash key.
/// Exported under the `metadata` feature only.
///
/// If there is an existing Rust function of the same hash, it is replaced.
@ -1144,7 +1144,7 @@ impl Module {
/// Set a Rust function taking a reference to the scripting [`Engine`][crate::Engine],
/// the current set of functions, plus a list of mutable [`Dynamic`] references
/// into the [`Module`], returning a non-zero hash key.
/// into the [`Module`], returning a [`u64`] hash key.
///
/// Use this to register a built-in function which must reference settings on the scripting
/// [`Engine`][crate::Engine] (e.g. to prevent growing an array beyond the allowed maximum size),
@ -1234,7 +1234,7 @@ impl Module {
)
}
/// Set a Rust function into the [`Module`], returning a non-zero hash key.
/// Set a Rust function into the [`Module`], returning a [`u64`] hash key.
///
/// If there is a similar existing Rust function, it is replaced.
///
@ -1287,7 +1287,7 @@ impl Module {
)
}
/// Set a Rust getter function taking one mutable parameter, returning a non-zero hash key.
/// Set a Rust getter function taking one mutable parameter, returning a [`u64`] hash key.
/// This function is automatically exposed to the global namespace.
///
/// If there is a similar existing Rust getter function, it is replaced.
@ -1327,7 +1327,7 @@ impl Module {
}
/// Set a Rust setter function taking two parameters (the first one mutable) into the [`Module`],
/// returning a non-zero hash key.
/// returning a [`u64`] hash key.
/// This function is automatically exposed to the global namespace.
///
/// If there is a similar existing setter Rust function, it is replaced.
@ -1370,7 +1370,7 @@ impl Module {
)
}
/// Set a pair of Rust getter and setter functions into the [`Module`], returning both non-zero hash keys.
/// Set a pair of Rust getter and setter functions into the [`Module`], returning both [`u64`] hash keys.
/// This is a short-hand for [`set_getter_fn`][Module::set_getter_fn] and [`set_setter_fn`][Module::set_setter_fn].
///
/// These function are automatically exposed to the global namespace.
@ -1418,7 +1418,7 @@ impl Module {
}
/// Set a Rust index getter taking two parameters (the first one mutable) into the [`Module`],
/// returning a non-zero hash key.
/// returning a [`u64`] hash key.
/// This function is automatically exposed to the global namespace.
///
/// If there is a similar existing setter Rust function, it is replaced.
@ -1479,7 +1479,7 @@ impl Module {
}
/// Set a Rust index setter taking three parameters (the first one mutable) into the [`Module`],
/// returning a non-zero hash key.
/// returning a [`u64`] hash key.
/// This function is automatically exposed to the global namespace.
///
/// If there is a similar existing Rust function, it is replaced.
@ -1539,7 +1539,7 @@ impl Module {
)
}
/// Set a pair of Rust index getter and setter functions into the [`Module`], returning both non-zero hash keys.
/// Set a pair of Rust index getter and setter functions into the [`Module`], returning both [`u64`] hash keys.
/// This is a short-hand for [`set_indexer_get_fn`][Module::set_indexer_get_fn] and
/// [`set_indexer_set_fn`][Module::set_indexer_set_fn].
///