Do not unnecessarily reindex module.

This commit is contained in:
Stephen Chung 2021-04-17 22:19:48 +08:00
parent 2efe9d08a4
commit bd145bd080
2 changed files with 20 additions and 13 deletions

View File

@ -2538,7 +2538,6 @@ impl Engine {
if let Some(global) = global { if let Some(global) = global {
let global = Shared::get_mut(global).unwrap(); let global = Shared::get_mut(global).unwrap();
global.set_var(name.clone(), value.clone()); global.set_var(name.clone(), value.clone());
global.build_index();
} }
} }

View File

@ -16,7 +16,7 @@ use std::{
any::TypeId, any::TypeId,
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
fmt, fmt,
iter::empty, iter::{empty, once},
num::NonZeroUsize, num::NonZeroUsize,
ops::{Add, AddAssign, Deref, DerefMut}, ops::{Add, AddAssign, Deref, DerefMut},
}; };
@ -166,7 +166,7 @@ impl Default for Module {
all_functions: Default::default(), all_functions: Default::default(),
type_iterators: Default::default(), type_iterators: Default::default(),
all_type_iterators: Default::default(), all_type_iterators: Default::default(),
indexed: false, indexed: true,
contains_indexed_global_functions: false, contains_indexed_global_functions: false,
identifiers: Default::default(), identifiers: Default::default(),
} }
@ -316,7 +316,7 @@ impl Module {
self.internal self.internal
} }
/// Set the interal status of the [`Module`]. /// Set the internal status of the [`Module`].
#[inline(always)] #[inline(always)]
pub(crate) fn set_internal(&mut self, value: bool) -> &mut Self { pub(crate) fn set_internal(&mut self, value: bool) -> &mut Self {
self.internal = value; self.internal = value;
@ -446,8 +446,14 @@ impl Module {
name: impl Into<Identifier>, name: impl Into<Identifier>,
value: impl Variant + Clone, value: impl Variant + Clone,
) -> &mut Self { ) -> &mut Self {
self.variables.insert(name.into(), Dynamic::from(value)); let ident = name.into();
self.indexed = false; let value = Dynamic::from(value);
if self.indexed {
let hash_var = crate::calc_fn_hash(once(""), &ident, 0);
self.all_variables.insert(hash_var, value.clone());
}
self.variables.insert(ident, value);
self self
} }
@ -1417,10 +1423,10 @@ impl Module {
match aliases.len() { match aliases.len() {
0 => (), 0 => (),
1 => { 1 => {
module.variables.insert(aliases.pop().unwrap(), value); module.set_var(aliases.pop().unwrap(), value);
} }
_ => aliases.into_iter().for_each(|alias| { _ => aliases.into_iter().for_each(|alias| {
module.variables.insert(alias, value.clone()); module.set_var(alias, value.clone());
}), }),
} }
}); });
@ -1538,7 +1544,7 @@ impl Module {
let mut functions = Default::default(); let mut functions = Default::default();
let mut type_iterators = Default::default(); let mut type_iterators = Default::default();
path.push("root"); path.push("");
self.contains_indexed_global_functions = index_module( self.contains_indexed_global_functions = index_module(
self, self,
@ -1571,10 +1577,12 @@ impl Module {
/// Set a type iterator into the [`Module`]. /// Set a type iterator into the [`Module`].
#[inline(always)] #[inline(always)]
pub fn set_iter(&mut self, typ: TypeId, func: IteratorFn) -> &mut Self { pub fn set_iter(&mut self, type_id: TypeId, func: IteratorFn) -> &mut Self {
self.type_iterators.insert(typ, func); if self.indexed {
self.indexed = false; self.all_type_iterators.insert(type_id, func.clone());
self.contains_indexed_global_functions = false; self.contains_indexed_global_functions = true;
}
self.type_iterators.insert(type_id, func);
self self
} }