Use Entry API.

This commit is contained in:
Stephen Chung 2022-09-07 13:30:31 +08:00
parent a6ce459b32
commit b0ce3ee445

View File

@ -18,6 +18,7 @@ use crate::{
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
any::{type_name, TypeId}, any::{type_name, TypeId},
collections::hash_map::Entry,
convert::TryFrom, convert::TryFrom,
mem, mem,
}; };
@ -199,10 +200,9 @@ impl Engine {
) )
}); });
let result = caches match caches.fn_resolution_cache_mut().entry(hash) {
.fn_resolution_cache_mut() Entry::Occupied(entry) => entry.into_mut().as_ref(),
.entry(hash) Entry::Vacant(entry) => {
.or_insert_with(|| {
let num_args = args.as_ref().map_or(0, |a| a.len()); let num_args = args.as_ref().map_or(0, |a| a.len());
let mut max_bitmask = 0; // One above maximum bitmask based on number of parameters. let mut max_bitmask = 0; // One above maximum bitmask based on number of parameters.
// Set later when a specific matching function is not found. // Set later when a specific matching function is not found.
@ -211,45 +211,27 @@ impl Engine {
loop { loop {
let func = lib let func = lib
.iter() .iter()
.find_map(|&m| { .find_map(|&m| m.get_fn(hash).map(|f| (f, m.id())))
m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry {
func,
source: m.id().map(|s| Box::new(s.into())),
})
})
.or_else(|| { .or_else(|| {
self.global_modules.iter().find_map(|m| { self.global_modules
m.get_fn(hash).cloned().map(|func| FnResolutionCacheEntry { .iter()
func, .find_map(|m| m.get_fn(hash).map(|f| (f, m.id())))
source: m.id().map(|s| Box::new(s.into())),
})
})
}); });
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
let func = func let func = func.or_else(|| _global.get_qualified_fn(hash)).or_else(|| {
.or_else(|| { self.global_sub_modules
_global.get_qualified_fn(hash).map(|(func, source)| { .values()
FnResolutionCacheEntry { .find_map(|m| m.get_qualified_fn(hash).map(|f| (f, m.id())))
func: func.clone(), });
source: source.map(|s| Box::new(s.into())),
}
})
})
.or_else(|| {
self.global_sub_modules.values().find_map(|m| {
m.get_qualified_fn(hash).cloned().map(|func| {
FnResolutionCacheEntry {
func,
source: m.id().map(|s| Box::new(s.into())),
}
})
})
});
// Specific version found if let Some((f, s)) = func {
if let Some(f) = func { // Specific version found - insert into cache and return it
return Some(f); let new_entry = FnResolutionCacheEntry {
func: f.clone(),
source: s.map(|s| Box::new(s.into())),
};
return entry.insert(Some(new_entry)).as_ref();
} }
// Check `Dynamic` parameters for functions with parameters // Check `Dynamic` parameters for functions with parameters
@ -282,7 +264,8 @@ impl Engine {
return None; return None;
} }
return args.and_then(|args| { // Try to find a built-in version
let builtin = args.and_then(|args| {
if is_op_assignment { if is_op_assignment {
let (first_arg, rest_args) = args.split_first().unwrap(); let (first_arg, rest_args) = args.split_first().unwrap();
@ -301,6 +284,8 @@ impl Engine {
}) })
} }
}); });
return entry.insert(builtin).as_ref();
} }
// Try all permutations with `Dynamic` wildcards // Try all permutations with `Dynamic` wildcards
@ -323,9 +308,8 @@ impl Engine {
bitmask += 1; bitmask += 1;
} }
}); }
}
result.as_ref()
} }
/// # Main Entry-Point /// # Main Entry-Point