Simplify to calc_fn_hash.

This commit is contained in:
Stephen Chung
2022-09-21 11:46:23 +08:00
parent d7ae65c425
commit 5d275b5307
12 changed files with 65 additions and 85 deletions

View File

@@ -198,10 +198,8 @@ impl Engine {
}
let mut hash = args.as_ref().map_or(hash_base, |args| {
combine_hashes(
hash_base,
calc_fn_params_hash(args.iter().map(|a| a.type_id())),
)
let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id()));
combine_hashes(hash_base, hash_params)
});
let cache = caches.fn_resolution_cache_mut();
@@ -611,7 +609,7 @@ impl Engine {
if num_params < 0 || num_params > crate::MAX_USIZE_INT {
false
} else {
let hash_script = calc_fn_hash(fn_name.as_str(), num_params as usize);
let hash_script = calc_fn_hash(None, fn_name.as_str(), num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script)
}
.into(),
@@ -815,7 +813,7 @@ impl Engine {
let fn_name = fn_ptr.fn_name();
let args_len = call_args.len() + fn_ptr.curry().len();
// Recalculate hashes
let new_hash = calc_fn_hash(fn_name, args_len).into();
let new_hash = calc_fn_hash(None, fn_name, args_len).into();
// Arguments are passed as-is, adding the curried arguments
let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len());
curry.extend(fn_ptr.curry().iter().cloned());
@@ -857,8 +855,8 @@ impl Engine {
// Recalculate hash
let new_hash = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, args_len),
calc_fn_hash(fn_name, args_len + 1),
calc_fn_hash(None, fn_name, args_len),
calc_fn_hash(None, fn_name, args_len + 1),
);
// Replace the first argument with the object pointer, adding the curried arguments
let mut curry = FnArgsVec::with_capacity(fn_ptr.curry().len());
@@ -944,8 +942,8 @@ impl Engine {
// Recalculate the hash based on the new function name and new arguments
hash = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, call_args.len()),
calc_fn_hash(fn_name, call_args.len() + 1),
calc_fn_hash(None, fn_name, call_args.len()),
calc_fn_hash(None, fn_name, call_args.len() + 1),
);
}
}
@@ -1036,9 +1034,9 @@ impl Engine {
// Recalculate hash
let args_len = total_args + curry.len();
hashes = if hashes.is_native_only() {
FnCallHashes::from_native(calc_fn_hash(name, args_len))
FnCallHashes::from_native(calc_fn_hash(None, name, args_len))
} else {
calc_fn_hash(name, args_len).into()
calc_fn_hash(None, name, args_len).into()
};
}
// Handle Fn()
@@ -1110,7 +1108,7 @@ impl Engine {
return Ok(if num_params < 0 || num_params > crate::MAX_USIZE_INT {
false
} else {
let hash_script = calc_fn_hash(&fn_name, num_params as usize);
let hash_script = calc_fn_hash(None, &fn_name, num_params as usize);
self.has_script_fn(Some(global), caches, lib, hash_script)
}
.into());

View File

@@ -91,7 +91,7 @@ pub fn get_hasher() -> ahash::AHasher {
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
pub fn calc_qualified_var_hash<'a>(
pub fn calc_var_hash<'a>(
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
var_name: &str,
) -> u64 {
@@ -113,9 +113,11 @@ pub fn calc_qualified_var_hash<'a>(
/// Calculate a non-zero [`u64`] hash key from a namespace-qualified function name
/// and the number of parameters, but no parameter types.
///
/// Module names are passed in via `&str` references from an iterator.
/// Module names making up the namespace are passed in via `&str` references from an iterator.
/// Parameter types are passed in via [`TypeId`] values from an iterator.
///
/// If the function is not namespace-qualified, pass [`None`] as the namespace.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
@@ -125,15 +127,15 @@ pub fn calc_qualified_var_hash<'a>(
/// The first module name is skipped. Hashing starts from the _second_ module in the chain.
#[inline]
#[must_use]
pub fn calc_qualified_fn_hash<'a>(
modules: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
pub fn calc_fn_hash<'a>(
namespace: impl IntoIterator<Item = &'a str, IntoIter = impl ExactSizeIterator<Item = &'a str>>,
fn_name: &str,
num: usize,
) -> u64 {
let s = &mut get_hasher();
// We always skip the first module
let iter = modules.into_iter();
let iter = namespace.into_iter();
let len = iter.len();
iter.skip(1).for_each(|m| m.hash(s));
len.hash(s);
@@ -146,20 +148,6 @@ pub fn calc_qualified_fn_hash<'a>(
}
}
/// Calculate a non-zero [`u64`] hash key from a non-namespace-qualified function name
/// and the number of parameters, but no 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 `DEFAULT_HASH`.
#[inline(always)]
#[must_use]
pub fn calc_fn_hash(fn_name: &str, num: usize) -> u64 {
calc_qualified_fn_hash(None, fn_name, num)
}
/// Calculate a non-zero [`u64`] hash key from a list of parameter types.
///
/// Parameter types are passed in via [`TypeId`] values from an iterator.

View File

@@ -20,8 +20,7 @@ pub use callable_function::CallableFunction;
#[cfg(not(feature = "no_function"))]
pub use func::Func;
pub use hashing::{
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,
combine_hashes, get_hasher, StraightHashMap,
calc_fn_hash, calc_fn_params_hash, calc_var_hash, combine_hashes, get_hasher, StraightHashMap,
};
pub use native::{
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,

View File

@@ -321,11 +321,11 @@ impl<'a> NativeCallContext<'a> {
let hash = if is_method_call {
FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))]
calc_fn_hash(fn_name, args_len - 1),
calc_fn_hash(fn_name, args_len),
calc_fn_hash(None, fn_name, args_len - 1),
calc_fn_hash(None, fn_name, args_len),
)
} else {
calc_fn_hash(fn_name, args_len).into()
calc_fn_hash(None, fn_name, args_len).into()
};
self.engine()