Remove key type from StraightHashMap.

This commit is contained in:
Stephen Chung 2022-09-12 23:08:38 +08:00
parent 08f6682d09
commit 40a12b2b7d
5 changed files with 19 additions and 19 deletions

View File

@ -20,12 +20,12 @@ pub struct FnResolutionCacheEntry {
/// _(internals)_ A function resolution cache with a bloom filter. /// _(internals)_ A function resolution cache with a bloom filter.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
/// ///
/// [`FnResolutionCacheEntry`] is [`Box`]ed in order to pack as many entries inside a single B-Tree /// The bloom filter is used to rapidly check whether a function hash has never been encountered.
/// level as possible. /// It enables caching a hash only during the second encounter to avoid "one-hit wonders".
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct FnResolutionCache { pub struct FnResolutionCache {
/// Hash map containing cached functions. /// Hash map containing cached functions.
pub map: StraightHashMap<u64, Option<FnResolutionCacheEntry>>, pub map: StraightHashMap<Option<FnResolutionCacheEntry>>,
/// Bloom filter to avoid caching "one-hit wonders". /// Bloom filter to avoid caching "one-hit wonders".
pub filter: BloomFilterU64, pub filter: BloomFilterU64,
} }
@ -47,7 +47,7 @@ impl FnResolutionCache {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Caches<'a> { pub struct Caches<'a> {
/// Stack of [function resolution caches][FnResolutionCache]. /// Stack of [function resolution caches][FnResolutionCache].
fn_resolution_caches: StaticVec<FnResolutionCache>, stack: StaticVec<FnResolutionCache>,
/// Take care of the lifetime parameter. /// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>, dummy: PhantomData<&'a ()>,
} }
@ -58,7 +58,7 @@ impl Caches<'_> {
#[must_use] #[must_use]
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
fn_resolution_caches: StaticVec::new_const(), stack: StaticVec::new_const(),
dummy: PhantomData, dummy: PhantomData,
} }
} }
@ -66,27 +66,27 @@ impl Caches<'_> {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn fn_resolution_caches_len(&self) -> usize { 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. /// Get a mutable reference to the current function resolution cache.
#[inline] #[inline]
#[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.is_empty() { if self.stack.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.push_fn_resolution_cache(); 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. /// Push an empty function resolution cache onto the stack and make it current.
#[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.push(Default::default()); self.stack.push(Default::default());
} }
/// Rewind the function resolution caches stack to a particular size. /// Rewind the function resolution caches stack to a particular size.
#[inline(always)] #[inline(always)]
pub fn rewind_fn_resolution_caches(&mut self, len: usize) { pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
self.fn_resolution_caches.truncate(len); self.stack.truncate(len);
} }
} }

View File

@ -8,10 +8,10 @@ use std::{
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
pub type StraightHashMap<K, V> = hashbrown::HashMap<K, V, StraightHasherBuilder>; pub type StraightHashMap<V> = hashbrown::HashMap<u64, V, StraightHasherBuilder>;
#[cfg(not(feature = "no_std"))] #[cfg(not(feature = "no_std"))]
pub type StraightHashMap<K, V> = std::collections::HashMap<K, V, StraightHasherBuilder>; pub type StraightHashMap<V> = std::collections::HashMap<u64, V, StraightHasherBuilder>;
/// Dummy hash value to map zeros to. This value can be anything. /// Dummy hash value to map zeros to. This value can be anything.
/// ///

View File

@ -176,12 +176,12 @@ pub struct Module {
/// [`Module`] variables. /// [`Module`] variables.
variables: BTreeMap<Identifier, Dynamic>, variables: BTreeMap<Identifier, Dynamic>,
/// Flattened collection of all [`Module`] variables, including those in sub-modules. /// Flattened collection of all [`Module`] variables, including those in sub-modules.
all_variables: StraightHashMap<u64, Dynamic>, all_variables: StraightHashMap<Dynamic>,
/// Functions (both native Rust and scripted). /// Functions (both native Rust and scripted).
functions: StraightHashMap<u64, Box<FuncInfo>>, functions: StraightHashMap<Box<FuncInfo>>,
/// Flattened collection of all functions, native Rust and scripted. /// Flattened collection of all functions, native Rust and scripted.
/// including those in sub-modules. /// including those in sub-modules.
all_functions: StraightHashMap<u64, CallableFunction>, all_functions: StraightHashMap<CallableFunction>,
/// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters. /// Native Rust functions (in scripted hash format) that contain [`Dynamic`] parameters.
dynamic_functions: BloomFilterU64, dynamic_functions: BloomFilterU64,
/// Iterator functions, keyed by the type producing the iterator. /// Iterator functions, keyed by the type producing the iterator.
@ -2025,8 +2025,8 @@ impl Module {
fn index_module<'a>( fn index_module<'a>(
module: &'a Module, module: &'a Module,
path: &mut Vec<&'a str>, path: &mut Vec<&'a str>,
variables: &mut StraightHashMap<u64, Dynamic>, variables: &mut StraightHashMap<Dynamic>,
functions: &mut StraightHashMap<u64, CallableFunction>, functions: &mut StraightHashMap<CallableFunction>,
type_iterators: &mut BTreeMap<TypeId, Shared<IteratorFn>>, type_iterators: &mut BTreeMap<TypeId, Shared<IteratorFn>>,
) -> bool { ) -> bool {
let mut contains_indexed_global_functions = false; let mut contains_indexed_global_functions = false;

View File

@ -32,7 +32,7 @@ use std::{
pub type ParseResult<T> = Result<T, ParseError>; pub type ParseResult<T> = Result<T, ParseError>;
type FnLib = StraightHashMap<u64, Shared<ScriptFnDef>>; type FnLib = StraightHashMap<Shared<ScriptFnDef>>;
const KEYWORD_SEMICOLON: &str = Token::SemiColon.literal_syntax(); const KEYWORD_SEMICOLON: &str = Token::SemiColon.literal_syntax();

View File

@ -27,7 +27,7 @@ pub struct StringsInterner<'a> {
/// Maximum string length. /// Maximum string length.
pub max_string_len: usize, pub max_string_len: usize,
/// Normal strings. /// Normal strings.
strings: StraightHashMap<u64, ImmutableString>, strings: StraightHashMap<ImmutableString>,
/// Take care of the lifetime parameter. /// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>, dummy: PhantomData<&'a ()>,
} }