Skip to content

Commit 6f5f583

Browse files
committed
[JSC] JSON.parse should take keys found in JSONCache's name table, and their values, through a dedicated loop
https://bugs.webkit.org/show_bug.cgi?id=328615 rdar://190797577 Reviewed by Keith Miller. JSON Tokenizing can be much faster if we are leveraging the skew of next token. When parsing an object and having a key ("key"), then next only valid token is colon (:). Based on this skew, we scan tokens more quickly before going to the generic lexing code. Given that JSON.parse is extremely frequently executed, we are tuning super carefuly to make everything super efficient. 1. A key found in the name table was returned as an std::optional<ExistingProperty>, folded into the Variant<ExistingProperty, Identifier> that the generic key lookup produces, and paired with a consumedColon flag carried across the iteration. The compiler ends up spilling them into the stack. 2. The lexer's position is a member, and the source is read one byte at a time, so the compiler has to assume any byte store could change it. It was stored after every advance, including after every digit of a number, and loaded again at the top of the loop. The token after each value was likewise stored and loaded back to decide whether the object continues. This patch gives keys found in the name table their own loop, which reads the source through a local cursor that stays in a register. For each key it matches the name, then reads a string value found in JSONCache's source-text table, or an integer of at most nine digits without a sign, directly through the cursor, stores the property with its transition, and dispatches on the separator byte. Any other value is read by the lexer from the same position, and any other key takes the existing path for that one property before returning to the loop. ToT Patched json-parse-object-reviver-same-value 78.3654+-0.2510 ? 78.9265+-0.3672 ? todomvc-javascript-es5-json-parse 15.5418+-0.0712 ^ 12.6900+-0.0496 ^ definitely 1.2247x faster vanilla-es2015-babel-webpack-todomvc-json-parse 25.2949+-0.1827 ^ 21.5578+-0.1615 ^ definitely 1.1734x faster todomvc-javascript-es6-webpack-json-parse 27.4154+-0.1697 ^ 22.1923+-0.0995 ^ definitely 1.2354x faster json-parse-array-reviver-same-value 56.1238+-0.1920 ^ 54.8521+-0.3038 ^ definitely 1.0232x faster flight-todomvc-json-parse 16.0599+-0.0981 ^ 14.0560+-0.0439 ^ definitely 1.1426x faster json-parse-object-reviver 80.3643+-0.2950 ? 80.9511+-0.5340 ? json-parse-leaf-object 9.8980+-0.0613 ^ 7.6658+-0.0689 ^ definitely 1.2912x faster vanilla-es2015-todomvc-json-parse 25.2668+-0.2169 ^ 21.5433+-0.0925 ^ definitely 1.1728x faster json-parse-utf16-leaf-object 82.6690+-0.2743 ^ 62.4780+-0.4512 ^ definitely 1.3232x faster json-parse-repeated-short-strings 9.3115+-0.0552 ^ 7.4934+-0.0350 ^ definitely 1.2426x faster vanilla-todomvc-json-parse 18.8332+-0.0518 ^ 15.9560+-0.0715 ^ definitely 1.1803x faster json-parse-utf16-nested-records 32.1751+-0.1018 ^ 28.4392+-0.1211 ^ definitely 1.1314x faster json-parse-array-reviver 61.4988+-0.4369 60.5441+-0.5521 might be 1.0158x faster json-parse-double 46.6939+-0.1455 ^ 45.6980+-0.3330 ^ definitely 1.0218x faster json-parse-short-string-ids 108.9573+-1.1631 ^ 103.1068+-1.3449 ^ definitely 1.0567x faster json-parse-int32-array 208.2562+-1.2047 ^ 195.6330+-0.9339 ^ definitely 1.0645x faster <geometric> 37.0365+-0.0552 ^ 32.7853+-0.0497 ^ definitely 1.1297x faster Test: JSTests/stress/json-parse-cached-integer-values.js * JSTests/stress/json-parse-cached-integer-values.js: Added. (shouldBe): (parse): (shouldThrow): (iteration.p.of.prefixes.const.number.of.numbers.const.number.of.invalid.shouldThrow.shouldThrow.shouldThrow.shouldThrow.shouldThrow.join): * Source/JavaScriptCore/runtime/LiteralParser.cpp: (JSC::scanJSONIntegerDigits): (JSC::reviverMode>::Lexer::lexNumber): (JSC::requires): (JSC::reviverMode>::Lexer::skipWhitespaceBeforeKey): Deleted. * Source/JavaScriptCore/runtime/LiteralParser.h: (JSC::LiteralParser::Lexer::end const): (JSC::LiteralParser::Lexer::setPtr): * Source/JavaScriptCore/runtime/ParseInt.h: (JSC::parseInt): Canonical link: https://commits.webkit.org/324783@main
1 parent 7b78a52 commit 6f5f583

4 files changed

Lines changed: 462 additions & 257 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
function shouldBe(actual, expected) {
2+
if (!Object.is(actual, expected))
3+
throw new Error(`bad value: ${String(actual)}, expected: ${String(expected)}`);
4+
}
5+
6+
// JSONCache matches a key or a string value only when at least 32 characters of source remain.
7+
const padding = " ".repeat(40);
8+
9+
// The first parse fills JSONCache and the later ones read keys and values from it. Every GC clears it.
10+
function parse(text) {
11+
let result;
12+
for (let i = 0; i < 3; ++i)
13+
result = JSON.parse(text);
14+
return result;
15+
}
16+
17+
function shouldThrow(text, message) {
18+
for (let i = 0; i < 3; ++i) {
19+
let error = null;
20+
try {
21+
JSON.parse(text);
22+
} catch (e) {
23+
error = e;
24+
}
25+
if (!(error instanceof SyntaxError))
26+
throw new Error(`did not throw SyntaxError: ${text}`);
27+
if (message && !error.message.includes(message))
28+
throw new Error(`bad error message: ${error.message}, expected: ${message}`);
29+
}
30+
}
31+
32+
const numbers = [
33+
"0", "7", "10", "123456789", "999999999", "1000000000", "1234567890", "2147483647", "2147483648",
34+
"4294967295", "4294967296", "99999999999999999999", "1.5", "1e5", "1E5", "1e-5", "0.0", "-0", "-1", "-123",
35+
];
36+
const invalid = ["00", "01", "1.", "1e", "1a", "1-", "-", "-a", "0x1", "1.e5"];
37+
const beforeComma = [",", " ,", "\n,", "\t,"];
38+
const beforeBrace = ["}", " }", "\n}", "\t}"];
39+
// The second prefix holds a character outside Latin-1, which makes the source 16-bit.
40+
const prefixes = [["", undefined], ['"p":"\u3042",', "\u3042"]];
41+
const cached = "cached";
42+
const cachedLong = "q".repeat(30);
43+
44+
const iterations = Math.min(testLoopCount, 2);
45+
for (let iteration = 0; iteration < iterations; ++iteration) {
46+
for (const [prefix, p] of prefixes) {
47+
for (const number of numbers) {
48+
const expected = Number(number);
49+
for (const separator of beforeComma) {
50+
const result = parse(`{${prefix}"a":${number}${separator}"b":2}${padding}`);
51+
shouldBe(result.p, p);
52+
shouldBe(result.a, expected);
53+
shouldBe(result.b, 2);
54+
}
55+
for (const separator of beforeBrace) {
56+
const result = parse(`{${prefix}"a":1,"b":${number}${separator}${padding}`);
57+
shouldBe(result.p, p);
58+
shouldBe(result.a, 1);
59+
shouldBe(result.b, expected);
60+
}
61+
shouldBe(parse(`{${prefix}"a": ${number}, "b": ${number}\n}${padding}`).b, expected);
62+
}
63+
64+
for (const number of invalid) {
65+
for (const separator of beforeComma)
66+
shouldThrow(`{${prefix}"a":${number}${separator}"b":2}${padding}`);
67+
for (const separator of beforeBrace)
68+
shouldThrow(`{${prefix}"a":1,"b":${number}${separator}${padding}`);
69+
}
70+
71+
shouldThrow(`{${prefix}"a":1,}${padding}`, "Property name must be a string literal");
72+
shouldThrow(`{${prefix}"a":1, }${padding}`, "Property name must be a string literal");
73+
shouldThrow(`{${prefix}"a":"${cached}",}${padding}`, "Property name must be a string literal");
74+
shouldThrow(`{${prefix}"a":1,"b":2,}${padding}`, "Property name must be a string literal");
75+
shouldThrow(`{${prefix}"a":1,"b":12`);
76+
shouldThrow(`{${prefix}"a":1,"b":12,`);
77+
shouldThrow(`{${prefix}"a":1,"b":12}]`);
78+
}
79+
80+
// String values found in JSONCache, followed by whitespace, by a wrong token, and by the end of the input.
81+
shouldBe(parse(`{"a":"${cached}","b":2}${padding}`).a, cached);
82+
shouldBe(parse(`{"a":"${cached}" ,"b":2}${padding}`).a, cached);
83+
shouldBe(parse(`{"a":"${cached}"\n,"b":2}${padding}`).a, cached);
84+
shouldBe(parse(`{"a":1,"b":"${cached}"\t}${padding}`).b, cached);
85+
shouldBe(parse(`{"a": "${cached}", "b": "${cached}"}${padding}`).b, cached);
86+
shouldThrow(`{"a":"${cached}":"b"}${padding}`);
87+
shouldThrow(`{"a":"${cached}" "b":2}${padding}`);
88+
shouldThrow(`{"a":"${cached}"]${padding}`);
89+
shouldThrow(`{"a":"${cached}"1}${padding}`);
90+
shouldBe(parse(`{"a":"${cachedLong}"}${padding}`).a, cachedLong);
91+
shouldThrow(`{"a":"${cachedLong}"`);
92+
shouldThrow(`{"a":"${cachedLong}",`);
93+
shouldThrow(`{"a":"${cachedLong}" `);
94+
95+
// Enough properties of one shape to move past inline storage and grow the out-of-line storage
96+
// several times, with each value read from the cursor and the objects kept alive across GCs.
97+
const keyCount = 40;
98+
const wide = `{${Array.from({ length: keyCount }, (_, i) => `"k${i}":${i % 3 ? i : `"${cached}"`}`).join(",")}}${padding}`;
99+
const wideObjects = [];
100+
for (let i = 0; i < 20; ++i) {
101+
wideObjects.push(JSON.parse(wide));
102+
if (i % 5 === 1)
103+
edenGC();
104+
if (i % 5 === 3)
105+
fullGC();
106+
}
107+
for (const object of wideObjects) {
108+
shouldBe(Object.keys(object).length, keyCount);
109+
for (let i = 0; i < keyCount; ++i)
110+
shouldBe(object[`k${i}`], i % 3 ? i : cached);
111+
}
112+
113+
// Nested values add their own names and transitions to JSONCache, which can evict the entries of the
114+
// object around them, and later keys of that object must still be read correctly.
115+
for (let i = 0; i < 20; ++i) {
116+
const nestedObject = `{${Array.from({ length: 16 }, (_, j) => `"n${iteration}_${i}_${j}":${j}`).join(",")}}`;
117+
const nestedArray = `[${Array.from({ length: 8 }, (_, j) => `{"m${i}_${j}":"${cached}","z":${j}}`).join(",")}]`;
118+
const text = `{"a":1,"o":${nestedObject},"b":2,"r":${nestedArray},"c":"${cached}","d":3}${padding}`;
119+
const result = parse(text);
120+
shouldBe(result.a, 1);
121+
shouldBe(result.o[`n${iteration}_${i}_15`], 15);
122+
shouldBe(result.b, 2);
123+
shouldBe(result.r.length, 8);
124+
shouldBe(result.r[7][`m${i}_7`], cached);
125+
shouldBe(result.r[7].z, 7);
126+
shouldBe(result.c, cached);
127+
shouldBe(result.d, 3);
128+
}
129+
130+
gc();
131+
}

0 commit comments

Comments
 (0)