Reduce BTree sizes by boxing values.

This commit is contained in:
Stephen Chung 2021-05-03 13:45:41 +08:00
parent aad842fe50
commit c749bbb653
5 changed files with 29 additions and 22 deletions

View File

@ -558,7 +558,7 @@ pub struct FnResolutionCacheEntry {
}
/// A function resolution cache.
pub type FnResolutionCache = BTreeMap<u64, Option<FnResolutionCacheEntry>>;
pub type FnResolutionCache = BTreeMap<u64, Option<Box<FnResolutionCacheEntry>>>;
/// _(INTERNALS)_ A type that holds all the current states of the [`Engine`].
/// Exported under the `internals` feature only.
@ -776,7 +776,7 @@ pub struct Engine {
pub(crate) module_resolver: Box<dyn crate::ModuleResolver>,
/// A map mapping type names to pretty-print names.
pub(crate) type_names: BTreeMap<Identifier, Identifier>,
pub(crate) type_names: BTreeMap<Identifier, Box<Identifier>>,
/// An empty [`ImmutableString`] for cloning purposes.
pub(crate) empty_string: ImmutableString,
@ -786,7 +786,7 @@ pub struct Engine {
/// A map containing custom keywords and precedence to recognize.
pub(crate) custom_keywords: BTreeMap<Identifier, Option<Precedence>>,
/// Custom syntax.
pub(crate) custom_syntax: BTreeMap<Identifier, CustomSyntax>,
pub(crate) custom_syntax: BTreeMap<Identifier, Box<CustomSyntax>>,
/// Callback closure for resolving variable access.
pub(crate) resolve_var: Option<OnVarCallback>,

View File

@ -265,7 +265,8 @@ impl Engine {
#[inline(always)]
pub fn register_type_with_name<T: Variant + Clone>(&mut self, name: &str) -> &mut Self {
// Add the pretty-print type name into the map
self.type_names.insert(type_name::<T>().into(), name.into());
self.type_names
.insert(type_name::<T>().into(), Box::new(name.into()));
self
}
/// Register an type iterator for an iterable type with the [`Engine`].

View File

@ -161,7 +161,7 @@ impl Engine {
args: Option<&mut FnCallArgs>,
allow_dynamic: bool,
is_op_assignment: bool,
) -> &'s Option<FnResolutionCacheEntry> {
) -> &'s Option<Box<FnResolutionCacheEntry>> {
let mut hash = if let Some(ref args) = args {
let hash_params = calc_fn_params_hash(args.iter().map(|a| a.type_id()));
combine_hashes(hash_script, hash_params)
@ -222,7 +222,7 @@ impl Engine {
match func {
// Specific version found
Some(f) => return Some(f),
Some(f) => return Some(Box::new(f)),
// Stop when all permutations are exhausted
None if bitmask >= max_bitmask => {
@ -250,6 +250,7 @@ impl Engine {
},
)
}
.map(Box::new)
});
}
@ -311,7 +312,8 @@ impl Engine {
is_op_assignment,
);
if let Some(FnResolutionCacheEntry { func, source }) = func {
if let Some(f) = func {
let FnResolutionCacheEntry { func, source } = f.as_ref();
assert!(func.is_native());
// Calling pure function but the first argument is a reference?
@ -717,10 +719,11 @@ impl Engine {
};
#[cfg(not(feature = "no_function"))]
if let Some(FnResolutionCacheEntry { func, source }) = hash_script.and_then(|hash| {
if let Some(f) = hash_script.and_then(|hash| {
self.resolve_function(mods, state, lib, fn_name, hash, None, false, false)
.clone()
}) {
let FnResolutionCacheEntry { func, source } = *f;
// Script function call
assert!(func.is_script());

View File

@ -48,7 +48,7 @@ impl Default for FnNamespace {
#[derive(Debug, Clone)]
pub struct FuncInfo {
/// Function instance.
pub func: CallableFunction,
pub func: Shared<CallableFunction>,
/// Function namespace.
pub namespace: FnNamespace,
/// Function access mode.
@ -140,7 +140,7 @@ pub struct Module {
functions: BTreeMap<u64, Box<FuncInfo>>,
/// Flattened collection of all external Rust functions, native or scripted.
/// including those in sub-modules.
all_functions: BTreeMap<u64, CallableFunction>,
all_functions: BTreeMap<u64, Shared<CallableFunction>>,
/// Iterator functions, keyed by the type producing the iterator.
type_iterators: BTreeMap<TypeId, IteratorFn>,
/// Flattened collection of iterator functions, including those in sub-modules.
@ -497,7 +497,7 @@ impl Module {
param_types: Default::default(),
#[cfg(feature = "metadata")]
param_names,
func: fn_def.into(),
func: Into::<CallableFunction>::into(fn_def).into(),
}),
);
self.indexed = false;
@ -721,7 +721,7 @@ impl Module {
param_types,
#[cfg(feature = "metadata")]
param_names,
func,
func: func.into(),
}),
);
@ -1119,7 +1119,7 @@ impl Module {
/// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call.
#[inline(always)]
pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> {
self.functions.get(&hash_fn).map(|f| &f.func)
self.functions.get(&hash_fn).map(|f| f.func.as_ref())
}
/// Does the particular namespace-qualified function exist in the [`Module`]?
@ -1135,7 +1135,9 @@ impl Module {
/// The [`u64`] hash is calculated by [`build_index`][Module::build_index].
#[inline(always)]
pub(crate) fn get_qualified_fn(&self, hash_qualified_fn: u64) -> Option<&CallableFunction> {
self.all_functions.get(&hash_qualified_fn)
self.all_functions
.get(&hash_qualified_fn)
.map(|f| f.as_ref())
}
/// Combine another [`Module`] into this [`Module`].
@ -1486,7 +1488,7 @@ impl Module {
module: &'a Module,
path: &mut Vec<&'a str>,
variables: &mut BTreeMap<u64, Dynamic>,
functions: &mut BTreeMap<u64, CallableFunction>,
functions: &mut BTreeMap<u64, Shared<CallableFunction>>,
type_iterators: &mut BTreeMap<TypeId, IteratorFn>,
) -> bool {
let mut contains_indexed_global_functions = false;

View File

@ -230,13 +230,14 @@ impl Engine {
new_vars: isize,
func: impl Fn(&mut EvalContext, &[Expression]) -> RhaiResult + SendSync + 'static,
) -> &mut Self {
let syntax = CustomSyntax {
self.custom_syntax.insert(
key.into(),
Box::new(CustomSyntax {
parse: Box::new(parse),
func: (Box::new(func) as Box<FnCustomSyntaxEval>).into(),
scope_delta: new_vars,
};
self.custom_syntax.insert(key.into(), syntax);
}),
);
self
}
}