Skip to content

Commit 102ad33

Browse files
perf(html): reduce sparse document parser overhead (#22135)
* perf(css,html): reduce syntax parser overhead * refactor(css): preserve spec token operations * perf(html): avoid redundant syntax parser scans * fix(html): handle NUL characters in CDATA sections * perf(html): keep the clamped column capacity, memoize the CDATA bracket scan `_growNodeColumns` clamps an incremental doubling to exactly `_COLUMN_SHRINK_CAPACITY` so the columns stay in the kept regime, but the release comparisons had been tightened to include that capacity, freeing the very value the clamp aims at. A document whose node count lands between its `len/12` pre-size and the threshold then re-allocated every column on each parse: the `html-printer-document-unit` document fixture allocated 5.41 MiB of typed arrays per parse under `beautify` and `minify`, against none on `main`. The CDATA fast-forward's `]` scan is memoized like the NUL one beside it. NULs separated by ordinary characters re-entered the "anything else" branch and rescanned the rest of the section each time: 4000 NULs over a 404 KB section scanned 808,194,000 characters, now 403,998. Trim the branch's comment back under the three-line limit. * test(html): benchmark a sparse inline-style document The printer fixtures all run about 12 bytes per `<`, so their columns are sized from markup either way and the sparse pre-size path is never exercised. A 2 MiB critical-CSS `<style>` behind eleven delimiters is the shape the markup probe exists for: 40.39 -> 3.38 ms and 7.667 -> 0 MiB per call under `beautify`. --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent a1f0fb1 commit 102ad33

4 files changed

Lines changed: 97 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Reduce HTML syntax processing time and memory use.

‎lib/html/syntax.js‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ const tokenize = (input, pos = 0, callbacks = {}) => {
617617
let nextNul = -1;
618618
let nextHyphen = -1;
619619
let nextGt = -1;
620+
let nextBracket = -1;
620621
// Same memo scheme for the closing quote of quoted attribute values (long
621622
// values: data: URIs, srcset, inline style). Memoized so a value with many
622623
// `&` references doesn't re-run the quote scan per reference.
@@ -2689,11 +2690,24 @@ const tokenize = (input, pos = 0, callbacks = {}) => {
26892690
if (cc === CC_RIGHT_SQUARE_BRACKET) {
26902691
state = STATE_CDATA_SECTION_BRACKET;
26912692
pos++;
2693+
} else if (cc === CC_NULL) {
2694+
// U+0000 NULL is an unexpected-null-character parse error.
2695+
reportError("unexpected-null-character", pos, pos + 1, "warning");
2696+
pos++;
26922697
} else {
2693-
// Anything else: emit the input character. Fast-forwards to the next
2694-
// `]`, the only code point with an arc of its own, in one scan.
2695-
pos = input.indexOf("]", pos + 1);
2696-
if (pos === -1) pos = len;
2698+
// Anything else: emit the input character. Fast-forwards to the
2699+
// next `]` or NUL, the code points with arcs of their own, in one
2700+
// scan each — both memoized, so neither rescans per character.
2701+
pos++;
2702+
if (nextBracket < pos) {
2703+
nextBracket = input.indexOf("]", pos);
2704+
if (nextBracket === -1) nextBracket = len;
2705+
}
2706+
if (nextNul < pos) {
2707+
nextNul = input.indexOf("\0", pos);
2708+
if (nextNul === -1) nextNul = len;
2709+
}
2710+
pos = Math.min(nextBracket, nextNul);
26972711
}
26982712
break;
26992713

@@ -4824,10 +4838,12 @@ const _resetAstColumns = () => {
48244838
_doctypeSystemId = null;
48254839
};
48264840

4827-
// The typed-array columns grow to the largest document parsed, and are shrunk
4828-
// on release above this capacity so one pathological file cannot pin tens of MB
4829-
// at module level for the process lifetime.
4841+
// WHY: the columns grow to the largest document parsed and are shrunk on
4842+
// release above this capacity, so one pathological file cannot pin tens of MB
4843+
// for the process lifetime. Strictly above: `_growNodeColumns` clamps a doubling
4844+
// to exactly this capacity, so releasing at it re-allocates every column again.
48304845
const _COLUMN_SHRINK_CAPACITY = 65536;
4846+
const _SPARSE_MARKUP_LIMIT = 512;
48314847

48324848
// Release the side arrays' heap references (strings, attribute names/values)
48334849
// once a walk has consumed the tree, so the retained columns don't pin the
@@ -8022,6 +8038,7 @@ const vetoesContentMode = (name) => {
80228038
/** @type {(input: string, code: string) => void} */
80238039
const handleParseError = (input, code) => {
80248040
if (code === "eof-in-tag") eofInTag = true;
8041+
else if (code === "unexpected-null-character") inputHasNul = true;
80258042
};
80268043
/**
80278044
* @param {string} input input
@@ -8144,7 +8161,7 @@ const handleCommentToken = (input, start, end, dataStart, dataEnd) => {
81448161
const data = input.slice(dataStart, dataEnd).replace(/\r\n?/g, "\n");
81458162
if (data !== "") {
81468163
tok.type = TOKEN_CHAR;
8147-
tok.data = data;
8164+
tok.data = inputHasNul ? data.replace(/\0/g, "�") : data;
81488165
tok.start = start;
81498166
tok.end = end;
81508167
dispatch();
@@ -8371,7 +8388,9 @@ const PARSE_CALLBACKS = {
83718388
const parseHtml = (input, pos = 0, options = {}) => {
83728389
source = input;
83738390
inputHasCr = input.includes("\r");
8374-
inputHasNul = input.includes("\0");
8391+
// The tokenizer reports every literal NUL before emitting its token, letting
8392+
// the parse-error callback set this flag without a separate full-input scan.
8393+
inputHasNul = false;
83758394
// Only an unknown name's entry is a slice of the last document; one naming a
83768395
// known element is a shared constant, and stays warm across parses.
83778396
_dropSourceBackedNames(TAG_NAME_INTERN);
@@ -8399,7 +8418,27 @@ const parseHtml = (input, pos = 0, options = {}) => {
83998418
// `HtmlParser` uses, drops roughly half the nodes and so estimates `len/24`;
84008419
// attributes are untouched by it.
84018420
let nodeEstimate = (input.length / (skipText ? 24 : 12)) | 0;
8402-
const attrEstimate = (input.length / 48) | 0;
8421+
let attrEstimate = (input.length / 48) | 0;
8422+
// A long inline style/script or comment has very few markup delimiters, so
8423+
// estimating columns from its byte length wastes memory. Probe only until the
8424+
// document proves dense; `indexOf` skips the long raw-text spans natively.
8425+
if (
8426+
nodeEstimate > (_nodeCapacity || 4096) ||
8427+
attrEstimate > (_attrCapacity || 4096)
8428+
) {
8429+
let markupCount = 0;
8430+
let searchFrom = 0;
8431+
while (markupCount <= _SPARSE_MARKUP_LIMIT) {
8432+
const next = input.indexOf("<", searchFrom);
8433+
if (next < 0) break;
8434+
markupCount++;
8435+
searchFrom = next + 1;
8436+
}
8437+
if (markupCount <= _SPARSE_MARKUP_LIMIT) {
8438+
nodeEstimate = Math.min(nodeEstimate, markupCount * 2 + 16);
8439+
attrEstimate = Math.min(attrEstimate, markupCount * 8 + 16);
8440+
}
8441+
}
84038442
if (_streamEligible) {
84048443
// Streaming recycles node ids, so a document past the threshold peaks at
84058444
// the activation point plus a batch of headroom rather than at the whole

‎test/benchmarkCases/html-printer-document-unit/index.bench.mjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ const FIXTURES = [
136136
return s;
137137
})(),
138138
{}
139+
],
140+
// Critical CSS inlined into `<head>`: megabytes behind a handful of markup
141+
// delimiters. Every other fixture here runs about 12 bytes per `<`, so this is
142+
// the only one whose columns are sized from length rather than from markup.
143+
[
144+
"sparse inline style",
145+
(() => {
146+
const rule = ".a{color:red;margin:0;padding:0;border:0}\n";
147+
const css = rule.repeat(Math.ceil((2 * 1024 * 1024) / rule.length));
148+
return `<!DOCTYPE html><html><head><style>${css}</style></head><body><p>hello</p></body></html>`;
149+
})(),
150+
{}
139151
]
140152
];
141153

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7170,6 +7170,19 @@ describe("parseHtml — tree-construction edge cases (SoA columns)", () => {
71707170
expect(small[0].attributes[0].value).toBe("c");
71717171
});
71727172

7173+
it("handles a large structurally sparse document", () => {
7174+
// Exercise both byte-length estimate caps without needing enough nodes or
7175+
// attributes to fill the speculative columns.
7176+
const nodes = body(`<!--${"x".repeat(3200000)}--><p>x</p>`);
7177+
expect(nodes).toHaveLength(1);
7178+
expect(nodes[0]).toEqual(
7179+
expect.objectContaining({ tagName: "p", children: [expect.any(Object)] })
7180+
);
7181+
expect(body("<b>y</b>")[0]).toEqual(
7182+
expect.objectContaining({ tagName: "b" })
7183+
);
7184+
});
7185+
71737186
it("merges texts left adjacent by a skipped comment", () => {
71747187
const nodes = bodyOf("a<!--c-->b", { comments: true });
71757188
expect(nodes).toEqual([
@@ -9099,6 +9112,24 @@ describe("tokenize — content modes, CDATA and NUL arcs", () => {
90999112
{ code: "eof-in-cdata", slice: "", severity: "error" }
91009113
]);
91019114
});
9115+
9116+
it("reports and replaces NUL characters", () => {
9117+
const source = `<svg><![CDATA[a${NUL}b${NUL}c]]></svg>`;
9118+
expect(errorsOf(source, foreign)).toEqual([
9119+
{
9120+
code: "unexpected-null-character",
9121+
slice: NUL,
9122+
severity: "warning"
9123+
},
9124+
{
9125+
code: "unexpected-null-character",
9126+
slice: NUL,
9127+
severity: "warning"
9128+
}
9129+
]);
9130+
const svg = body(source)[0];
9131+
expect(/** @type {MatText} */ (svg.children[0]).data).toBe("a�b�c");
9132+
});
91029133
});
91039134

91049135
it("reports unexpected-null-character from each state that consumes one", () => {

0 commit comments

Comments
 (0)