Skip to content

Commit 1cf5efc

Browse files
committed
Fix interner max.
1 parent 45bfdb6 commit 1cf5efc

6 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Deprecated API's
99

1010
* The plugin macros `export_fn`, `register_exported_fn!`, `set_exported_fn!` and `set_exported_global_fn!` are deprecated because they do not add value over existing direct API's.
1111

12+
New features
13+
------------
14+
15+
* New options `Engine::set_max_strings_interned` and `Engine::max_strings_interned` are added to limit the maximum number of strings interned in the `Engine`'s string interner.
16+
1217
Enhancements
1318
------------
1419

‎src/api/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Engine {
6363
guard.set_max(max);
6464
}
6565
} else {
66-
self.interned_strings = Some(StringsInterner::new(self.max_strings_interned()).into());
66+
self.interned_strings = Some(StringsInterner::new(max).into());
6767
}
6868
self
6969
}

‎src/eval/cache.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct FnResolutionCache {
2626
/// Hash map containing cached functions.
2727
pub dict: StraightHashMap<Option<FnResolutionCacheEntry>>,
2828
/// Bloom filter to avoid caching "one-hit wonders".
29-
pub filter: BloomFilterU64,
29+
pub bloom_filter: BloomFilterU64,
3030
}
3131

3232
impl FnResolutionCache {
@@ -35,7 +35,7 @@ impl FnResolutionCache {
3535
#[allow(dead_code)]
3636
pub fn clear(&mut self) {
3737
self.dict.clear();
38-
self.filter.clear();
38+
self.bloom_filter.clear();
3939
}
4040
}
4141

‎src/func/call.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl Engine {
222222
func: f.clone(),
223223
source: s.cloned(),
224224
};
225-
return if cache.filter.is_absent_and_set(hash) {
225+
return if cache.bloom_filter.is_absent_and_set(hash) {
226226
// Do not cache "one-hit wonders"
227227
*local_entry = Some(new_entry);
228228
local_entry.as_ref()
@@ -298,7 +298,7 @@ impl Engine {
298298
}),
299299
});
300300

301-
return if cache.filter.is_absent_and_set(hash) {
301+
return if cache.bloom_filter.is_absent_and_set(hash) {
302302
// Do not cache "one-hit wonders"
303303
*local_entry = builtin;
304304
local_entry.as_ref()

‎src/func/script.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl Engine {
238238
// Then check sub-modules
239239
|| self.global_sub_modules.values().any(|m| m.contains_qualified_fn(hash_script));
240240

241-
if !res && !cache.filter.is_absent_and_set(hash_script) {
241+
if !res && !cache.bloom_filter.is_absent_and_set(hash_script) {
242242
// Do not cache "one-hit wonders"
243243
cache.dict.insert(hash_script, None);
244244
}

‎src/types/interner.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
};
1717

1818
/// Maximum length of strings interned.
19-
pub const MAX_STRING_LEN: usize = 24;
19+
pub const MAX_STRING_LEN: usize = 32;
2020

2121
/// _(internals)_ A cache for interned strings.
2222
/// Exported under the `internals` feature only.
@@ -61,6 +61,7 @@ impl StringsInterner {
6161
#[inline(always)]
6262
pub fn set_max(&mut self, max: usize) {
6363
self.max_strings_interned = max;
64+
self.throttle_cache(None);
6465
}
6566
/// The maximum number of strings to be interned.
6667
#[inline(always)]
@@ -105,14 +106,14 @@ impl StringsInterner {
105106
};
106107

107108
// Throttle the cache upon exit
108-
self.throttle_cache(hash);
109+
self.throttle_cache(Some(hash));
109110

110111
result
111112
}
112113

113114
/// If the interner is over capacity, remove the longest entry that has the lowest count
114115
#[inline]
115-
fn throttle_cache(&mut self, skip_hash: u64) {
116+
fn throttle_cache(&mut self, skip_hash: Option<u64>) {
116117
if self.max() == 0 {
117118
self.clear();
118119
return;
@@ -130,7 +131,7 @@ impl StringsInterner {
130131
let mut index = 0;
131132

132133
for (&k, v) in &self.cache {
133-
if k != skip_hash
134+
if skip_hash.map_or(true, |hash| k != hash)
134135
&& (v.strong_count() < min_count
135136
|| (v.strong_count() == min_count && v.len() > max_len))
136137
{

0 commit comments

Comments
 (0)