Do not cache "one-hit wonders"

This commit is contained in:
Stephen Chung
2022-09-12 19:47:29 +08:00
parent c1ae9e0405
commit 44219c732c
5 changed files with 84 additions and 29 deletions

View File

@@ -257,8 +257,9 @@ impl Engine {
let hash = combine_hashes(hashes.native, hash);
let cache = caches.fn_resolution_cache_mut();
let local_entry: CallableFunction;
let func = match cache.entry(hash) {
let func = match cache.map.entry(hash) {
Entry::Vacant(entry) => {
let func = if args.len() == 2 {
get_builtin_binary_op_fn(name, operands[0], operands[1])
@@ -267,14 +268,22 @@ impl Engine {
};
if let Some(f) = func {
&entry
.insert(Some(FnResolutionCacheEntry {
func: CallableFunction::from_fn_builtin(f),
source: None,
}))
.as_ref()
.unwrap()
.func
if cache.filter.is_absent(hash) {
// Do not cache "one-hit wonders"
cache.filter.mark(hash);
local_entry = CallableFunction::from_fn_builtin(f);
&local_entry
} else {
// Cache repeated calls
&entry
.insert(Some(FnResolutionCacheEntry {
func: CallableFunction::from_fn_builtin(f),
source: None,
}))
.as_ref()
.unwrap()
.func
}
} else {
let result = self.exec_fn_call(
None, global, caches, lib, name, *hashes, operands, false, false, pos,