From 40a12b2b7dd2eb0062d6fcd7908b673e599036ab Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 12 Sep 2022 23:08:38 +0800 Subject: [PATCH] Remove key type from StraightHashMap. --- src/eval/cache.rs | 20 ++++++++++---------- src/func/hashing.rs | 4 ++-- src/module/mod.rs | 10 +++++----- src/parser.rs | 2 +- src/types/interner.rs | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/eval/cache.rs b/src/eval/cache.rs index d7fd06e7..34a6941a 100644 --- a/src/eval/cache.rs +++ b/src/eval/cache.rs @@ -20,12 +20,12 @@ pub struct FnResolutionCacheEntry { /// _(internals)_ A function resolution cache with a bloom filter. /// Exported under the `internals` feature only. /// -/// [`FnResolutionCacheEntry`] is [`Box`]ed in order to pack as many entries inside a single B-Tree -/// level as possible. +/// The bloom filter is used to rapidly check whether a function hash has never been encountered. +/// It enables caching a hash only during the second encounter to avoid "one-hit wonders". #[derive(Debug, Clone, Default)] pub struct FnResolutionCache { /// Hash map containing cached functions. - pub map: StraightHashMap>, + pub map: StraightHashMap>, /// Bloom filter to avoid caching "one-hit wonders". pub filter: BloomFilterU64, } @@ -47,7 +47,7 @@ impl FnResolutionCache { #[derive(Debug, Clone)] pub struct Caches<'a> { /// Stack of [function resolution caches][FnResolutionCache]. - fn_resolution_caches: StaticVec, + stack: StaticVec, /// Take care of the lifetime parameter. dummy: PhantomData<&'a ()>, } @@ -58,7 +58,7 @@ impl Caches<'_> { #[must_use] pub const fn new() -> Self { Self { - fn_resolution_caches: StaticVec::new_const(), + stack: StaticVec::new_const(), dummy: PhantomData, } } @@ -66,27 +66,27 @@ impl Caches<'_> { #[inline(always)] #[must_use] pub fn fn_resolution_caches_len(&self) -> usize { - self.fn_resolution_caches.len() + self.stack.len() } /// Get a mutable reference to the current function resolution cache. #[inline] #[must_use] pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache { - if self.fn_resolution_caches.is_empty() { + if self.stack.is_empty() { // Push a new function resolution cache if the stack is empty self.push_fn_resolution_cache(); } - self.fn_resolution_caches.last_mut().unwrap() + self.stack.last_mut().unwrap() } /// Push an empty function resolution cache onto the stack and make it current. #[allow(dead_code)] #[inline(always)] pub fn push_fn_resolution_cache(&mut self) { - self.fn_resolution_caches.push(Default::default()); + self.stack.push(Default::default()); } /// Rewind the function resolution caches stack to a particular size. #[inline(always)] pub fn rewind_fn_resolution_caches(&mut self, len: usize) { - self.fn_resolution_caches.truncate(len); + self.stack.truncate(len); } } diff --git a/src/func/hashing.rs b/src/func/hashing.rs index b06b08e6..a7ef71fb 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -8,10 +8,10 @@ use std::{ }; #[cfg(feature = "no_std")] -pub type StraightHashMap = hashbrown::HashMap; +pub type StraightHashMap = hashbrown::HashMap; #[cfg(not(feature = "no_std"))] -pub type StraightHashMap = std::collections::HashMap; +pub type StraightHashMap = std::collections::HashMap; /// Dummy hash value to map zeros to. This value can be anything. /// diff --git a/src/module/mod.rs b/src/module/mod.rs index ffb2bd82..8e5440ba 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -176,12 +176,12 @@ pub struct Module { /// [`Module`] variables. variables: BTreeMap, /// Flattened collection of all [`Module`] variables, including those in sub-modules. - all_variables: StraightHashMap, + all_variables: StraightHashMap, /// Functions (both native Rust and scripted). - functions: StraightHashMap>, + functions: StraightHashMap>, /// Flattened collection of all functions, native Rust and scripted. /// including those in sub-modules. - all_functions: StraightHashMap, + all_functions: StraightHashMap, /// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters. dynamic_functions: BloomFilterU64, /// Iterator functions, keyed by the type producing the iterator. @@ -2025,8 +2025,8 @@ impl Module { fn index_module<'a>( module: &'a Module, path: &mut Vec<&'a str>, - variables: &mut StraightHashMap, - functions: &mut StraightHashMap, + variables: &mut StraightHashMap, + functions: &mut StraightHashMap, type_iterators: &mut BTreeMap>, ) -> bool { let mut contains_indexed_global_functions = false; diff --git a/src/parser.rs b/src/parser.rs index a48a7c4b..4d22d30b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,7 +32,7 @@ use std::{ pub type ParseResult = Result; -type FnLib = StraightHashMap>; +type FnLib = StraightHashMap>; const KEYWORD_SEMICOLON: &str = Token::SemiColon.literal_syntax(); diff --git a/src/types/interner.rs b/src/types/interner.rs index 2b8bada1..ea235f35 100644 --- a/src/types/interner.rs +++ b/src/types/interner.rs @@ -27,7 +27,7 @@ pub struct StringsInterner<'a> { /// Maximum string length. pub max_string_len: usize, /// Normal strings. - strings: StraightHashMap, + strings: StraightHashMap, /// Take care of the lifetime parameter. dummy: PhantomData<&'a ()>, }