From 96dbbc76e4640cf29ccb05947eea41ecd494d402 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 12 Sep 2021 13:33:55 +0800 Subject: [PATCH] Use StaticVec for function resolution caches for the common case where no modules are used. --- src/engine.rs | 12 ++++++------ src/lib.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index bc7ff71c..4f4ce8bb 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -662,18 +662,18 @@ pub struct EvalState { pub num_operations: u64, /// Number of modules loaded. pub num_modules: usize, + /// Stack of function resolution caches. + fn_resolution_caches: StaticVec, /// Embedded module resolver. #[cfg(not(feature = "no_module"))] pub embedded_module_resolver: Option>, - /// Stack of function resolution caches. - fn_resolution_caches: Vec, } impl EvalState { /// Create a new [`EvalState`]. #[inline(always)] #[must_use] - pub const fn new() -> Self { + pub fn new() -> Self { Self { source: None, always_search_scope: false, @@ -682,7 +682,7 @@ impl EvalState { num_modules: 0, #[cfg(not(feature = "no_module"))] embedded_module_resolver: None, - fn_resolution_caches: Vec::new(), + fn_resolution_caches: StaticVec::new(), } } /// Is the state currently at global (root) level? @@ -697,7 +697,7 @@ impl EvalState { pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache { if self.fn_resolution_caches.is_empty() { // Push a new function resolution cache if the stack is empty - self.fn_resolution_caches.push(Default::default()); + self.push_fn_resolution_cache(); } self.fn_resolution_caches .last_mut() @@ -713,7 +713,7 @@ impl EvalState { /// /// # Panics /// - /// Panics if there are no more function resolution cache in the stack. + /// Panics if there is no more function resolution cache in the stack. #[inline(always)] pub fn pop_fn_resolution_cache(&mut self) { self.fn_resolution_caches diff --git a/src/lib.rs b/src/lib.rs index d99f68f2..b21ff361 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -271,7 +271,7 @@ pub use module::NamespaceRef; /// /// The number 3, other than being the holy number, is carefully chosen for a balance between /// storage space and reduce allocations. That is because most function calls (and most functions, -/// in that matter) contain fewer than 4 arguments, the exception being closures that capture a +/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a /// large number of external variables. /// /// In addition, most script blocks either contain many statements, or just one or two lines; @@ -306,7 +306,7 @@ type StaticVec = smallvec::SmallVec<[T; 3]>; /// /// The number 3, other than being the holy number, is carefully chosen for a balance between /// storage space and reduce allocations. That is because most function calls (and most functions, -/// in that matter) contain fewer than 4 arguments, the exception being closures that capture a +/// for that matter) contain fewer than 4 arguments, the exception being closures that capture a /// large number of external variables. /// /// In addition, most script blocks either contain many statements, or just one or two lines;