Remove free list of empty function resolution caches.

This commit is contained in:
Stephen Chung 2021-06-28 22:06:46 +08:00
parent a13a724361
commit 8f4a582f88

View File

@ -631,7 +631,7 @@ pub struct State {
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>, pub resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
/// Function resolution cache and free list. /// Function resolution cache and free list.
fn_resolution_caches: (StaticVec<FnResolutionCache>, Vec<FnResolutionCache>), fn_resolution_caches: StaticVec<FnResolutionCache>,
} }
impl State { impl State {
@ -645,11 +645,11 @@ impl State {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache { pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache {
if self.fn_resolution_caches.0.is_empty() { if self.fn_resolution_caches.is_empty() {
// Push a new function resolution cache if the stack is empty // Push a new function resolution cache if the stack is empty
self.fn_resolution_caches.0.push(BTreeMap::new()); self.fn_resolution_caches.push(Default::default());
} }
self.fn_resolution_caches.0.last_mut().expect( self.fn_resolution_caches.last_mut().expect(
"never fails because there is at least one function resolution cache by this point", "never fails because there is at least one function resolution cache by this point",
) )
} }
@ -657,9 +657,7 @@ impl State {
#[allow(dead_code)] #[allow(dead_code)]
#[inline(always)] #[inline(always)]
pub fn push_fn_resolution_cache(&mut self) { pub fn push_fn_resolution_cache(&mut self) {
self.fn_resolution_caches self.fn_resolution_caches.push(Default::default());
.0
.push(self.fn_resolution_caches.1.pop().unwrap_or_default());
} }
/// Remove the current function resolution cache from the stack and make the last one current. /// Remove the current function resolution cache from the stack and make the last one current.
/// ///
@ -668,13 +666,9 @@ impl State {
/// Panics if there are no more function resolution cache in the stack. /// Panics if there are no more function resolution cache in the stack.
#[inline(always)] #[inline(always)]
pub fn pop_fn_resolution_cache(&mut self) { pub fn pop_fn_resolution_cache(&mut self) {
let mut cache = self self.fn_resolution_caches
.fn_resolution_caches
.0
.pop() .pop()
.expect("there should be at least one function resolution cache"); .expect("there should be at least one function resolution cache");
cache.clear();
self.fn_resolution_caches.1.push(cache);
} }
} }