Skip to content

Commit 9f224fe

Browse files
committed
[JSC] Avoid String ref count churn in RegExp constructor, padStart, and replace
https://bugs.webkit.org/show_bug.cgi?id=327933 Reviewed by Yusuke Suzuki. These functions copied a String argument only to read it, which costs an atomic ref and deref. Read the JSString via view() or value() instead. ToT Patched new-regexp-with-flags 68.1961+-1.2633 ^ 56.9854+-0.5497 ^ definitely 1.1967x faster string-pad-start-short 131.0631+-3.2587 ^ 105.2411+-3.2880 ^ definitely 1.2454x faster string-replace-all-short-string 192.7163+-2.4593 ^ 173.6625+-3.8412 ^ definitely 1.1097x faster Tests: JSTests/microbenchmarks/new-regexp-with-flags.js JSTests/microbenchmarks/string-pad-start-short.js JSTests/microbenchmarks/string-replace-all-short-string.js JSTests/stress/new-regexp-flags-rope-or-non-string.js JSTests/stress/string-pad-fill-rope-or-non-string.js JSTests/stress/string-replace-arguments-rope-or-non-string.js * JSTests/microbenchmarks/new-regexp-with-flags.js: Added. (test): * JSTests/microbenchmarks/string-pad-start-short.js: Added. (test): * JSTests/microbenchmarks/string-replace-all-short-string.js: Added. (replacer): (test): * JSTests/stress/new-regexp-flags-rope-or-non-string.js: Added. (shouldBe): (substring): (let.object.toString): (let.thrower.toString): (create): (createOrThrow): (test): * JSTests/stress/string-pad-fill-rope-or-non-string.js: Added. (shouldBe): (substring): (let.object.toString): (let.thrower.toString): (test): (testLong): * JSTests/stress/string-replace-arguments-rope-or-non-string.js: Added. (shouldBe): (substring): (let.object.toString): (let.thrower.toString): (offset): (mixed): (test): (let.search.toString): (let.replacement.toString): (testSearch): * Source/JavaScriptCore/runtime/RegExpConstructor.cpp: (JSC::toFlags): * Source/JavaScriptCore/runtime/StringPrototype.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): (JSC::padString): * Source/JavaScriptCore/runtime/StringPrototypeInlines.h: (JSC::replaceUsingStringSearch): Canonical link: https://commits.webkit.org/324186@main
1 parent e1bfd55 commit 9f224fe

10 files changed

Lines changed: 407 additions & 20 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var patterns = ["fo+", "ba[rz]", "x.*y", "^f$"];
2+
var flags = ["g", "i", "gi", "m"];
3+
4+
function test(pattern, flags) {
5+
return new RegExp(pattern, flags);
6+
}
7+
noInline(test);
8+
9+
for (var i = 0; i < 1e6; ++i)
10+
test(patterns[i & 3], flags[(i >> 2) & 3]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var strings = ["1", "2", "3", "4", "15", "26", "37", "48"];
2+
3+
function test(string) {
4+
return string.padStart(2, "0") + string.padStart(8, "0") + string.padEnd(8);
5+
}
6+
noInline(test);
7+
8+
for (var i = 0; i < 1e6; ++i)
9+
test(strings[i & 7]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var strings = ["foo", "bar", "xfoooy", "baz", "fo", "f"];
2+
3+
function replacer(match) {
4+
return "0";
5+
}
6+
7+
function test(string) {
8+
return string.replaceAll("o", "0") + string.replaceAll("o", replacer) + string.replace("o", replacer);
9+
}
10+
noInline(test);
11+
12+
for (var i = 0; i < 1e6; ++i)
13+
test(strings[i % 6]);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`bad value: ${actual}, expected: ${expected}`);
4+
}
5+
6+
function shouldThrow(func, errorType) {
7+
let error;
8+
try {
9+
func();
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof errorType))
14+
throw new Error(`bad error: ${error}`);
15+
}
16+
17+
function rope(a, b) {
18+
return a + b;
19+
}
20+
noInline(rope);
21+
22+
function substring(string, start, end) {
23+
return string.slice(start, end);
24+
}
25+
noInline(substring);
26+
27+
let flagsBase = "xgimyz";
28+
let count = 0;
29+
let object = {
30+
toString() {
31+
++count;
32+
return "gi";
33+
}
34+
};
35+
let thrower = {
36+
toString() {
37+
throw new RangeError("thrower");
38+
}
39+
};
40+
41+
function create(pattern, flags) {
42+
return new RegExp(pattern, flags);
43+
}
44+
noInline(create);
45+
46+
function createOrThrow(flags) {
47+
try {
48+
return new RegExp("a", flags).flags;
49+
} catch (error) {
50+
return error.constructor;
51+
}
52+
}
53+
noInline(createOrThrow);
54+
55+
function test(i) {
56+
shouldBe(create("a", rope("g", "i")).flags, "gi");
57+
shouldBe(create("a", substring(flagsBase, 1, 4)).flags, "gim");
58+
shouldBe(create("a", rope("", "")).flags, "");
59+
shouldBe(create(/a/g, rope("i", "m")).flags, "im");
60+
shouldBe(RegExp("a", rope("g", "y")).flags, "gy");
61+
62+
let before = count;
63+
shouldBe(create("a", object).flags, "gi");
64+
shouldBe(count, before + 1);
65+
66+
shouldBe(createOrThrow((i & 63) ? "g" : 1), (i & 63) ? "g" : SyntaxError);
67+
shouldBe(createOrThrow((i & 63) ? rope("g", "i") : rope("g", "g")), (i & 63) ? "gi" : SyntaxError);
68+
}
69+
noInline(test);
70+
71+
for (let i = 0; i < testLoopCount; ++i)
72+
test(i);
73+
74+
shouldThrow(() => new RegExp("a", null), SyntaxError);
75+
shouldThrow(() => new RegExp("a", true), SyntaxError);
76+
shouldThrow(() => new RegExp("a", 1.5), SyntaxError);
77+
shouldThrow(() => new RegExp("a", Symbol()), TypeError);
78+
shouldThrow(() => new RegExp("a", thrower), RangeError);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`bad value: ${actual}, expected: ${expected}`);
4+
}
5+
6+
function shouldThrow(func, errorType) {
7+
let error;
8+
try {
9+
func();
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof errorType))
14+
throw new Error(`bad error: ${error}`);
15+
}
16+
17+
function rope(a, b) {
18+
return a + b;
19+
}
20+
noInline(rope);
21+
22+
function substring(string, start, end) {
23+
return string.slice(start, end);
24+
}
25+
noInline(substring);
26+
27+
let base = "0123456789abcdefghijklmnopqrstuvwxyz";
28+
let count = 0;
29+
let object = {
30+
toString() {
31+
++count;
32+
return "xy";
33+
}
34+
};
35+
let thrower = {
36+
toString() {
37+
throw new RangeError("thrower");
38+
}
39+
};
40+
41+
function test(i) {
42+
shouldBe("7".padStart(3, rope("0", "1")), "017");
43+
shouldBe("7".padEnd(6, rope("0", "1")), "701010");
44+
shouldBe("7".padStart(4, substring(base, 10, 12)), "aba7");
45+
shouldBe("7".padEnd(4, substring(base, 10, 12)), "7aba");
46+
shouldBe("7".padStart(3, rope("\u3042", "\u3044")), "\u3042\u30447");
47+
shouldBe("\u3042".padEnd(3, rope("a", "b")), "\u3042ab");
48+
shouldBe("7".padStart(3, 0), "007");
49+
shouldBe("7".padStart(5, 12 + (i & 1)), (i & 1) ? "13137" : "12127");
50+
shouldBe("7".padEnd(5, 1.5), "71.51");
51+
shouldBe("7".padStart(5, true), "true7");
52+
shouldBe("7".padStart(5, null), "null7");
53+
shouldBe("7".padStart(3, undefined), " 7");
54+
shouldBe("7".padEnd(3), "7 ");
55+
shouldBe("7".padEnd(3, rope("", "")), "7");
56+
57+
let before = count;
58+
shouldBe("7".padStart(4, object), "xyx7");
59+
shouldBe(count, before + 1);
60+
shouldBe("abc".padStart(2, object), "abc");
61+
shouldBe(count, before + 1);
62+
}
63+
noInline(test);
64+
65+
function testLong() {
66+
let long = "7".padStart(1030, rope("ab", "c"));
67+
shouldBe(long.length, 1030);
68+
shouldBe(long.slice(0, 5), "abcab");
69+
shouldBe(long.slice(1024), "bcabc7");
70+
71+
long = "7".padEnd(1100, rope("0123456789", "abc"));
72+
shouldBe(long.length, 1100);
73+
shouldBe(long.slice(0, 15), "70123456789abc0");
74+
shouldBe(long.slice(1090), "abc0123456");
75+
76+
long = "7".padStart(1030, substring(base, 10, 23));
77+
shouldBe(long.length, 1030);
78+
shouldBe(long.slice(0, 14), "abcdefghijklma");
79+
shouldBe(long.slice(1025), "lmab7");
80+
81+
long = "7".padStart(1030, 0);
82+
shouldBe(long.length, 1030);
83+
shouldBe(long.slice(1026), "0007");
84+
85+
long = "7".padEnd(1030);
86+
shouldBe(long.length, 1030);
87+
shouldBe(long.slice(0, 3), "7 ");
88+
89+
let before = count;
90+
long = "7".padEnd(1030, object);
91+
shouldBe(long.length, 1030);
92+
shouldBe(long.slice(0, 4), "7xyx");
93+
shouldBe(count, before + 1);
94+
}
95+
noInline(testLong);
96+
97+
for (let i = 0; i < testLoopCount; ++i)
98+
test(i);
99+
100+
for (let i = 0; i < 10; ++i)
101+
testLong();
102+
103+
shouldThrow(() => "7".padStart(3, Symbol()), TypeError);
104+
shouldThrow(() => "7".padEnd(3, thrower), RangeError);
105+
shouldThrow(() => "7".padEnd(2000, thrower), RangeError);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`bad value: ${actual}, expected: ${expected}`);
4+
}
5+
6+
function shouldThrow(func, errorType) {
7+
let error;
8+
try {
9+
func();
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof errorType))
14+
throw new Error(`bad error: ${error}`);
15+
}
16+
17+
function rope(a, b) {
18+
return a + b;
19+
}
20+
noInline(rope);
21+
22+
function substring(string, start, end) {
23+
return string.slice(start, end);
24+
}
25+
noInline(substring);
26+
27+
let base = "0123456789abcdefghijklmnopqrstuvwxyz";
28+
let log = "";
29+
let search = {
30+
toString() {
31+
log += "s";
32+
return "o";
33+
}
34+
};
35+
let replacement = {
36+
toString() {
37+
log += "r";
38+
return "0";
39+
}
40+
};
41+
let thrower = {
42+
toString() {
43+
throw new RangeError("thrower");
44+
}
45+
};
46+
47+
function offset(match, offset) {
48+
return offset;
49+
}
50+
51+
function test(i) {
52+
shouldBe("fabab".replaceAll(rope("a", "b"), "o"), "foo");
53+
shouldBe("fabab".replaceAll(substring(base, 10, 12), "o"), "foo");
54+
shouldBe("fabab".replace(substring(base, 10, 12), "o"), "foab");
55+
shouldBe("a1b1".replaceAll(1, "x"), "axbx");
56+
shouldBe("a12b13".replaceAll(12 + (i & 1), "x"), (i & 1) ? "a12bx" : "axb13");
57+
shouldBe("a1.5b1.5".replaceAll(1.5, "x"), "axbx");
58+
shouldBe("anullbnull".replaceAll(null, "x"), "axbx");
59+
shouldBe("aundefinedb".replaceAll(undefined, "x"), "axb");
60+
shouldBe("atruebtrue".replaceAll(true, "x"), "axbx");
61+
shouldBe("foo".replaceAll(rope("", ""), "-"), "-f-o-o-");
62+
shouldBe("a1b1".replaceAll(1, offset), "a1b3");
63+
64+
log = "";
65+
shouldBe("foo".replaceAll(search, replacement), "f00");
66+
shouldBe(log, "sr");
67+
shouldBe("foo".replace(search, replacement), "f0o");
68+
shouldBe(log, "srsr");
69+
shouldBe("fxx".replaceAll(search, "0"), "fxx");
70+
shouldBe(log, "srsrs");
71+
}
72+
noInline(test);
73+
74+
for (let i = 0; i < testLoopCount; ++i)
75+
test(i);
76+
77+
shouldThrow(() => "foo".replaceAll(Symbol(), "x"), TypeError);
78+
shouldThrow(() => "foo".replaceAll(thrower, "x"), RangeError);
79+
shouldThrow(() => "foo".replaceAll(thrower, replacement), RangeError);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`bad value: ${actual}, expected: ${expected}`);
4+
}
5+
6+
function shouldThrow(func, errorType) {
7+
let error;
8+
try {
9+
func();
10+
} catch (e) {
11+
error = e;
12+
}
13+
if (!(error instanceof errorType))
14+
throw new Error(`bad error: ${error}`);
15+
}
16+
17+
function rope(a, b) {
18+
return a + b;
19+
}
20+
noInline(rope);
21+
22+
function substring(string, start, end) {
23+
return string.slice(start, end);
24+
}
25+
noInline(substring);
26+
27+
let base = "0123456789abcdefghijklmnopqrstuvwxyz";
28+
let count = 0;
29+
let object = {
30+
toString() {
31+
++count;
32+
return "xy";
33+
}
34+
};
35+
let thrower = {
36+
toString() {
37+
throw new RangeError("thrower");
38+
}
39+
};
40+
41+
function offset(match, offset) {
42+
return offset;
43+
}
44+
45+
function mixed(match, offset) {
46+
if (offset === 1)
47+
return rope("a", "b");
48+
return object;
49+
}
50+
51+
function test(i) {
52+
shouldBe("foo".replace("o", rope("a", "b")), "fabo");
53+
shouldBe("foo".replaceAll("o", rope("a", "b")), "fabab");
54+
shouldBe("foo".replace("o", rope("[$", "&]")), "f[o]o");
55+
shouldBe("foo".replaceAll("o", rope("[$", "&]")), "f[o][o]");
56+
shouldBe("foo".replace("o", substring(base, 10, 12)), "fabo");
57+
shouldBe("foo".replaceAll("o", substring(base, 10, 12)), "fabab");
58+
shouldBe("foo".replace("o", 5), "f5o");
59+
shouldBe("foo".replaceAll("o", 12 + (i & 1)), (i & 1) ? "f1313" : "f1212");
60+
shouldBe("foo".replace("o", 1.5), "f1.5o");
61+
shouldBe("foo".replaceAll("o", null), "fnullnull");
62+
shouldBe("foo".replace("o", undefined), "fundefinedo");
63+
shouldBe("foo".replaceAll("o"), "fundefinedundefined");
64+
shouldBe("foo".replaceAll("o", ""), "f");
65+
shouldBe("foo".replace("x", rope("a", "b")), "foo");
66+
shouldBe("foo".replaceAll("x", 5), "foo");
67+
shouldBe(rope("fo", "o").replaceAll(rope("o", "o"), rope("a", "b")), "fab");
68+
69+
let before = count;
70+
shouldBe("foo".replaceAll("o", object), "fxyxy");
71+
shouldBe(count, before + 1);
72+
shouldBe("foo".replace("x", object), "foo");
73+
shouldBe(count, before + 2);
74+
75+
shouldBe("foo".replace("o", offset), "f1o");
76+
shouldBe("foo".replaceAll("o", offset), "f12");
77+
shouldBe("foo".replaceAll("o", String), "foo");
78+
shouldBe("foo".replaceAll("o", mixed), "fabxy");
79+
shouldBe(count, before + 3);
80+
}
81+
noInline(test);
82+
83+
for (let i = 0; i < testLoopCount; ++i)
84+
test(i);
85+
86+
shouldThrow(() => "foo".replace("o", Symbol()), TypeError);
87+
shouldThrow(() => "foo".replaceAll("o", thrower), RangeError);
88+
shouldThrow(() => "foo".replaceAll("o", () => thrower), RangeError);
89+
shouldThrow(() => "foo".replace("x", thrower), RangeError);

0 commit comments

Comments
 (0)