Using hashing for full hash instead of xor.

This commit is contained in:
Stephen Chung 2022-11-04 21:59:49 +08:00
parent 35b02ce9b7
commit 470af6af71
6 changed files with 34 additions and 51 deletions

View File

@ -10,9 +10,8 @@ use crate::engine::{
use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState}; use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState};
use crate::tokenizer::{is_valid_function_name, Token}; use crate::tokenizer::{is_valid_function_name, Token};
use crate::{ use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnArgsVec, FnPtr, calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, Module,
ImmutableString, Module, OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, ERR,
Scope, ERR,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use hashbrown::hash_map::Entry; use hashbrown::hash_map::Entry;
@ -178,8 +177,7 @@ impl Engine {
} }
let mut hash = args.as_ref().map_or(hash_base, |args| { let mut hash = args.as_ref().map_or(hash_base, |args| {
let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id())); calc_fn_hash_full(hash_base, args.iter().map(|a| a.type_id()))
combine_hashes(hash_base, hash_params)
}); });
let cache = caches.fn_resolution_cache_mut(); let cache = caches.fn_resolution_cache_mut();
@ -287,7 +285,8 @@ impl Engine {
} }
// Try all permutations with `Dynamic` wildcards // Try all permutations with `Dynamic` wildcards
let hash_params = calc_fn_params_hash( hash = calc_fn_hash_full(
hash_base,
args.as_ref() args.as_ref()
.expect("no permutations") .expect("no permutations")
.iter() .iter()
@ -302,7 +301,6 @@ impl Engine {
} }
}), }),
); );
hash = combine_hashes(hash_base, hash_params);
bitmask += 1; bitmask += 1;
} }
@ -1335,9 +1333,7 @@ impl Engine {
// Then search native Rust functions // Then search native Rust functions
None => { None => {
self.track_operation(global, pos)?; self.track_operation(global, pos)?;
let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id())); let hash_qualified_fn = calc_fn_hash_full(hash, args.iter().map(|a| a.type_id()));
let hash_qualified_fn = combine_hashes(hash, hash_params);
module.get_qualified_fn(hash_qualified_fn) module.get_qualified_fn(hash_qualified_fn)
} }
r => r, r => r,
@ -1355,16 +1351,18 @@ impl Engine {
// Try all permutations with `Dynamic` wildcards // Try all permutations with `Dynamic` wildcards
while bitmask < max_bitmask { while bitmask < max_bitmask {
let hash_params = calc_fn_params_hash(args.iter().enumerate().map(|(i, a)| { let hash_qualified_fn = calc_fn_hash_full(
let mask = 1usize << (num_args - i - 1); hash,
if bitmask & mask == 0 { args.iter().enumerate().map(|(i, a)| {
a.type_id() let mask = 1usize << (num_args - i - 1);
} else { if bitmask & mask == 0 {
// Replace with `Dynamic` a.type_id()
TypeId::of::<Dynamic>() } else {
} // Replace with `Dynamic`
})); TypeId::of::<Dynamic>()
let hash_qualified_fn = combine_hashes(hash, hash_params); }
}),
);
self.track_operation(global, pos)?; self.track_operation(global, pos)?;

View File

@ -156,7 +156,7 @@ pub fn calc_fn_hash<'a>(
} }
} }
/// Calculate a non-zero [`u64`] hash key from a list of parameter types. /// Calculate a non-zero [`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. /// Parameter types are passed in via [`TypeId`] values from an iterator.
/// ///
@ -165,10 +165,12 @@ pub fn calc_fn_hash<'a>(
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`. /// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn calc_fn_params_hash( pub fn calc_fn_hash_full(
base: u64,
params: impl IntoIterator<Item = TypeId, IntoIter = impl ExactSizeIterator<Item = TypeId>>, params: impl IntoIterator<Item = TypeId, IntoIter = impl ExactSizeIterator<Item = TypeId>>,
) -> u64 { ) -> u64 {
let s = &mut get_hasher(); let s = &mut get_hasher();
base.hash(s);
let iter = params.into_iter(); let iter = params.into_iter();
let len = iter.len(); let len = iter.len();
iter.for_each(|t| { iter.for_each(|t| {
@ -181,17 +183,3 @@ pub fn calc_fn_params_hash(
r => r, r => r,
} }
} }
/// Combine two [`u64`] hashes by taking the XOR of them.
///
/// # Zeros
///
/// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`.
#[inline(always)]
#[must_use]
pub const fn combine_hashes(a: u64, b: u64) -> u64 {
match a ^ b {
0 => ALT_ZERO_HASH,
r => r,
}
}

View File

@ -21,9 +21,7 @@ pub use call::FnCallArgs;
pub use callable_function::CallableFunction; pub use callable_function::CallableFunction;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
pub use func::Func; pub use func::Func;
pub use hashing::{ pub use hashing::{calc_fn_hash, calc_fn_hash_full, calc_var_hash, get_hasher, StraightHashMap};
calc_fn_hash, calc_fn_params_hash, calc_var_hash, combine_hashes, get_hasher, StraightHashMap,
};
pub use native::{ pub use native::{
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone, locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared, shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,

View File

@ -238,7 +238,7 @@ pub use func::Shared;
/// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag. /// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag.
pub use func::Locked; pub use func::Locked;
use func::{calc_fn_hash, calc_fn_params_hash, calc_var_hash, combine_hashes}; use func::{calc_fn_hash, calc_fn_hash_full, calc_var_hash};
pub use rhai_codegen::*; pub use rhai_codegen::*;

View File

@ -10,8 +10,8 @@ use crate::func::{
}; };
use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection}; use crate::types::{dynamic::Variant, BloomFilterU64, CustomTypesCollection};
use crate::{ use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Identifier, ImmutableString, calc_fn_hash, calc_fn_hash_full, Dynamic, Identifier, ImmutableString, NativeCallContext,
NativeCallContext, RhaiResultOf, Shared, SmartString, StaticVec, RhaiResultOf, Shared, SmartString, StaticVec,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -151,9 +151,10 @@ pub fn calc_native_fn_hash<'a>(
fn_name: &str, fn_name: &str,
params: &[TypeId], params: &[TypeId],
) -> u64 { ) -> u64 {
let hash_script = calc_fn_hash(modules, fn_name, params.len()); calc_fn_hash_full(
let hash_params = calc_fn_params_hash(params.iter().copied()); calc_fn_hash(modules, fn_name, params.len()),
combine_hashes(hash_script, hash_params) params.iter().copied(),
)
} }
/// A module which may contain variables, sub-modules, external Rust functions, /// A module which may contain variables, sub-modules, external Rust functions,
@ -1028,8 +1029,7 @@ impl Module {
let name = name.as_ref(); let name = name.as_ref();
let hash_script = calc_fn_hash(None, name, param_types.len()); let hash_script = calc_fn_hash(None, name, param_types.len());
let hash_params = calc_fn_params_hash(param_types.iter().copied()); let hash_fn = calc_fn_hash_full(hash_script, param_types.iter().copied());
let hash_fn = combine_hashes(hash_script, hash_params);
if is_dynamic { if is_dynamic {
self.dynamic_functions_filter.mark(hash_script); self.dynamic_functions_filter.mark(hash_script);

View File

@ -11,8 +11,8 @@ use crate::func::hashing::get_hasher;
use crate::tokenizer::Token; use crate::tokenizer::Token;
use crate::types::dynamic::AccessMode; use crate::types::dynamic::AccessMode;
use crate::{ use crate::{
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, FnPtr, Identifier, calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnPtr, Identifier, ImmutableString, Position,
ImmutableString, Position, Scope, StaticVec, AST, INT, Scope, StaticVec, AST, INT,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -171,8 +171,7 @@ fn has_native_fn_override(
hash_script: u64, hash_script: u64,
arg_types: impl AsRef<[TypeId]>, arg_types: impl AsRef<[TypeId]>,
) -> bool { ) -> bool {
let hash_params = calc_fn_params_hash(arg_types.as_ref().iter().copied()); let hash = calc_fn_hash_full(hash_script, arg_types.as_ref().iter().copied());
let hash = combine_hashes(hash_script, hash_params);
// First check the global namespace and packages, but skip modules that are standard because // First check the global namespace and packages, but skip modules that are standard because
// they should never conflict with system functions. // they should never conflict with system functions.