Skip to content

Truly independent web browser

LibJS: Avoid copying substring storage in RegExp.prototype.test

sideshowbarker5663ed5c3b64
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
index 170e9d4adea..684cf6bc45b 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
@@ -2430,8 +2430,7 @@ impl RegExpPrototype {
&& !sticky
&& let Some(compiled_regex) = get_or_compile_regex(&typed_regexp)
{
- let string_data = string.utf16_string();
- let utf16_view = Utf16View::of_string(&string_data);
+ let utf16_view = string.utf16_string_view();
if !typed_regexp.legacy_features_enabled() {
// Fastest path: just test, no captures or legacy props.
diff --git a/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs b/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs
index 5a4d5c7109c..a2235b169f5 100644
--- a/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs
+++ b/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs
@@ -15,7 +15,7 @@ use ak::Utf16FlyString;
use super::javascript_test_runner::{JSFileResult, parse_module, parse_script};
use super::test_runner::{Case, Suite, TestResult, get_time_in_ms, relative_path};
use crate::bytecode::property_access::Strict;
-use crate::gc::class::{Extends, GcCell};
+use crate::gc::class::{Extends, ExternalMemorySize, GcCell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
@@ -44,6 +44,10 @@ use crate::utf16::Utf16View;
pub const EXPOSED_GLOBAL_FUNCTIONS: &[(&str, RawNativeFunctionPointer)] = &[
("canParseSource", raw_native!(can_parse_source)),
("haveSameShape", raw_native!(have_same_shape)),
+ (
+ "getStringExternalMemorySize",
+ raw_native!(get_string_external_memory_size),
+ ),
("setDictionaryGeneration", raw_native!(set_dictionary_generation)),
("gc", raw_native!(collect_garbage)),
(
@@ -90,6 +94,11 @@ fn have_same_shape(vm: &Vm) -> ThrowCompletionOr<Value> {
Ok(Value::from_bool(lhs.as_object().shape() == rhs.as_object().shape()))
}
+fn get_string_external_memory_size(vm: &Vm) -> ThrowCompletionOr<Value> {
+ let string = vm.argument(0).to_primitive_string(vm)?;
+ Ok(Value::from_f64(string.external_memory_size() as f64))
+}
+
fn set_dictionary_generation(vm: &Vm) -> ThrowCompletionOr<Value> {
let shape = vm.argument(0).to_object(vm)?.shape();
if !shape.is_dictionary() {
diff --git a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.test-substring.js b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.test-substring.js
new file mode 100644
index 00000000000..21cea9edff8
--- /dev/null
+++ b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.test-substring.js
@@ -0,0 +1,32 @@
+describe("testing substrings without copying their characters", () => {
+ for (const character of ["a", "é", "Ā", "😀"]) {
+ for (const RegExpClass of [RegExp, class extends RegExp {}]) {
+ test(`${character}, ${RegExpClass === RegExp ? "RegExp" : "subclass"}`, () => {
+ let source = character.repeat(1024);
+ const matching = new RegExpClass("^(.)", "u");
+ const nonMatching = new RegExpClass("^!");
+ if (RegExpClass !== RegExp) {
+ Object.setPrototypeOf(matching, RegExp.prototype);
+ Object.setPrototypeOf(nonMatching, RegExp.prototype);
+ }
+ let string = source.substring(20);
+
+ for (let i = 0; i < 8; ++i) {
+ expect(getStringExternalMemorySize(string)).toBe(0);
+ expect(matching.test(string)).toBeTrue();
+ expect(getStringExternalMemorySize(string)).toBe(0);
+ if (RegExpClass === RegExp) expect(RegExp.$1).toBe(character);
+ expect(nonMatching.test(string)).toBeFalse();
+ expect(getStringExternalMemorySize(string)).toBe(0);
+ string = string.substring(20);
+ }
+
+ expect(getStringExternalMemorySize(source)).toBeGreaterThan(0);
+ source = null;
+ gc();
+ expect(matching.test(string)).toBeTrue();
+ expect(string.startsWith(character)).toBeTrue();
+ });
+ }
+ }
+});