Skip to content

Truly independent web browser

LibJS: Drop compiled regexes that no RegExp object holds

Andreas Klingeae01b006b0b
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 18caf64f5d4..75689611742 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -86,6 +86,7 @@ use crate::runtime::primitive_string::PrimitiveString;
use crate::runtime::promise::{Promise, PromiseState, RejectionOperation};
use crate::runtime::property_key::PropertyKey;
use crate::runtime::reference::{BaseType, Reference};
+use crate::runtime::regexp_prototype::RegexCache;
use crate::runtime::shape::{ConstructShapeCache, PrototypeTransitionCache};
use crate::runtime::shared_function_instance_data::SharedFunctionInstanceData;
use crate::runtime::source_text_module::SourceTextModule;
@@ -608,6 +609,7 @@ pub struct Vm {
/// The strings are weak: the sweep callback drops the ones that die.
interned_strings: RefCell<HashMap<usize, Gc<PrimitiveString>, foldhash::fast::RandomState>>,
json_parse_cache: JsonParseCache,
+ regex_cache: RegexCache,
json_stringify_cache: JsonStringifyCache,
empty_string: OnceCell<Gc<PrimitiveString>>,
empty_realm: OnceCell<Gc<Realm>>,
@@ -803,6 +805,7 @@ impl Vm {
),
interned_strings: RefCell::default(),
json_parse_cache: JsonParseCache::default(),
+ regex_cache: RegexCache::default(),
json_stringify_cache: JsonStringifyCache::new(),
empty_string: OnceCell::new(),
empty_realm: OnceCell::new(),
@@ -2202,6 +2205,10 @@ impl Vm {
.expose_provenance() as u64
}
+ pub fn regex_cache(&self) -> &RegexCache {
+ &self.regex_cache
+ }
+
pub fn json_parse_cache(&self) -> &JsonParseCache {
&self.json_parse_cache
}
@@ -2557,10 +2564,11 @@ unsafe extern "C" fn sweep(context: *mut c_void) {
// NB: The VM has no heap anymore when this runs for the last collection of a heap that goes away.
if vm.heap.get().is_some_and(Heap::is_returning_memory_to_system) {
crate::source_text::park_all_source_texts();
- crate::runtime::regexp_prototype::clear_regex_cache();
+ vm.regex_cache.clear();
crate::work_zone::release_idle_memory();
} else {
crate::source_text::park_idle_source_texts();
+ vm.regex_cache.remove_unused();
}
vm.remove_dead_async_waits(&sweep);
vm.remove_dead_cells_from_weak_containers(&sweep);
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
index e87851d922e..0c926b03f13 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
@@ -214,9 +214,6 @@ fn increment_last_index(
)
}
-/// get_or_compile_regex(): the compiled regex of a RegExp object, which is compiled the first time it is asked for.
-/// Returns None for a pattern the regex engine cannot compile.
-// FIXME: Add an eviction policy to bound the size of this cache.
struct RegexCacheKey {
pattern: Utf16String,
flags: RegExpFlags,
@@ -239,23 +236,31 @@ impl Hash for RegexCacheKey {
}
}
-thread_local! {
- /// The compiled regexes of every pattern and flags. Compiled regexes hold no cells, and each thread runs its own
- /// VM, so each thread keeps its own.
- static REGEX_CACHE: RefCell<HashMap<RegexCacheKey, Rc<EcmaScriptRegex>, foldhash::fast::RandomState>> =
- RefCell::new(HashMap::default());
+/// The compiled regexes of the VM by pattern and flags, so that RegExp objects of one pattern share one. Compiled
+/// regexes hold no cells. A regex stays for as long as a RegExp object holds it, and one collection longer.
+#[derive(Default)]
+pub struct RegexCache {
+ regexes: RefCell<HashMap<RegexCacheKey, Rc<EcmaScriptRegex>, foldhash::fast::RandomState>>,
}
-/// Lets go of the compiled regexes of the calling thread that no RegExp object holds, for a process that is short of
-/// memory.
-pub(crate) fn clear_regex_cache() {
- REGEX_CACHE.with_borrow_mut(|cache| {
- cache.clear();
- cache.shrink_to_fit();
- });
+impl RegexCache {
+ /// Lets go of every regex, for a process that is short of memory.
+ pub(crate) fn clear(&self) {
+ let mut regexes = self.regexes.borrow_mut();
+ regexes.clear();
+ regexes.shrink_to_fit();
+ }
+
+ /// Lets go of the regexes that only the cache holds. RegExp objects that die in a collection let go of theirs when
+ /// they are swept, after this runs, so their regexes go at the next collection.
+ pub(crate) fn remove_unused(&self) {
+ self.regexes.borrow_mut().retain(|_, regex| Rc::strong_count(regex) > 1);
+ }
}
-fn get_or_compile_regex(regexp_object: &RegExpObject) -> Option<Rc<EcmaScriptRegex>> {
+/// get_or_compile_regex(): the compiled regex of a RegExp object, which is compiled the first time it is asked for.
+/// Returns None for a pattern the regex engine cannot compile.
+fn get_or_compile_regex(vm: &Vm, regexp_object: &RegExpObject) -> Option<Rc<EcmaScriptRegex>> {
// Fast path: check the inline cache on the RegExpObject.
if let Some(cached) = regexp_object.cached_regex() {
return Some(cached);
@@ -269,7 +274,7 @@ fn get_or_compile_regex(regexp_object: &RegExpObject) -> Option<Rc<EcmaScriptReg
flags: flag_bits,
};
- if let Some(cached) = REGEX_CACHE.with_borrow(|cache| cache.get(&cache_key).cloned()) {
+ if let Some(cached) = vm.regex_cache().regexes.borrow().get(&cache_key).cloned() {
regexp_object.set_cached_regex(Some(Rc::clone(&cached)));
return Some(cached);
}
@@ -287,7 +292,10 @@ fn get_or_compile_regex(regexp_object: &RegExpObject) -> Option<Rc<EcmaScriptReg
EcmaScriptRegex::compile(Utf16View::Utf16(&normalized_pattern), compile_flags_for(flag_bits)).ok()?;
let compiled = Rc::new(compiled);
- REGEX_CACHE.with_borrow_mut(|cache| cache.insert(cache_key, Rc::clone(&compiled)));
+ vm.regex_cache()
+ .regexes
+ .borrow_mut()
+ .insert(cache_key, Rc::clone(&compiled));
regexp_object.set_cached_regex(Some(Rc::clone(&compiled)));
Some(compiled)
}
@@ -417,7 +425,7 @@ fn regexp_builtin_exec(
}
let last_index = last_index as usize;
- let Some(compiled_regex) = get_or_compile_regex(&regexp_object) else {
+ let Some(compiled_regex) = get_or_compile_regex(vm, &regexp_object) else {
return Ok(Value::NULL);
};
@@ -1255,7 +1263,7 @@ impl RegExpPrototype {
// getter reading these very bits, which the predicate has just confirmed is what would happen.
let full_unicode = is_unicode || is_unicode_sets;
- let Some(compiled_regex) = get_or_compile_regex(&typed_regexp) else {
+ let Some(compiled_regex) = get_or_compile_regex(vm, &typed_regexp) else {
return Ok(None);
};
@@ -1503,7 +1511,7 @@ impl RegExpPrototype {
if flag_bits.has(RegExpFlags::STICKY) {
return Ok(None);
}
- let Some(compiled_regex) = get_or_compile_regex(&typed_regexp) else {
+ let Some(compiled_regex) = get_or_compile_regex(vm, &typed_regexp) else {
return Ok(None);
};
if !compiled_regex.named_groups().is_empty() {
@@ -2060,7 +2068,7 @@ impl RegExpPrototype {
if flag_bits.has(RegExpFlags::STICKY) {
return Ok(None);
}
- let Some(compiled_regex) = get_or_compile_regex(&typed_regexp) else {
+ let Some(compiled_regex) = get_or_compile_regex(vm, &typed_regexp) else {
return Ok(None);
};
@@ -2437,7 +2445,7 @@ impl RegExpPrototype {
// Only use fast path when we don't need to update lastIndex.
if !global
&& !sticky
- && let Some(compiled_regex) = get_or_compile_regex(&typed_regexp)
+ && let Some(compiled_regex) = get_or_compile_regex(vm, &typed_regexp)
{
let utf16_view = string.utf16_string_view();