Skip to content

Commit f509ff7

Browse files
perf(html): avoid redundant syntax parser scans
1 parent 0657a1f commit f509ff7

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎.changeset/040-css-html-syntax-performance.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
"webpack": patch
33
---
44

5-
Reduce HTML syntax processing time and memory use, and avoid a duplicate CSS
6-
ident-like-token lookahead.
5+
Reduce HTML syntax processing time and memory use.

‎lib/css/syntax.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,8 @@ const _identNamesUrl = (input, start, end) => {
11761176
function consumeAnIdentLikeToken(input, pos, out) {
11771177
const start = pos;
11781178
pos = _consumeAnIdentSequence(input, pos);
1179-
const nextCodePoint = input.charCodeAt(pos);
11801179
if (
1181-
nextCodePoint === CC_LEFT_PARENTHESIS &&
1180+
input.charCodeAt(pos) === CC_LEFT_PARENTHESIS &&
11821181
_identNamesUrl(input, start, pos)
11831182
) {
11841183
pos++;
@@ -1203,7 +1202,7 @@ function consumeAnIdentLikeToken(input, pos, out) {
12031202
}
12041203
return consumeAUrlToken(input, pos, start, out);
12051204
}
1206-
if (nextCodePoint === CC_LEFT_PARENTHESIS) {
1205+
if (input.charCodeAt(pos) === CC_LEFT_PARENTHESIS) {
12071206
pos++;
12081207
return fill(out, TT_FUNCTION, start, pos);
12091208
}

‎lib/html/syntax.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8023,6 +8023,7 @@ const vetoesContentMode = (name) => {
80238023
/** @type {(input: string, code: string) => void} */
80248024
const handleParseError = (input, code) => {
80258025
if (code === "eof-in-tag") eofInTag = true;
8026+
else if (code === "unexpected-null-character") inputHasNul = true;
80268027
};
80278028
/**
80288029
* @param {string} input input
@@ -8372,7 +8373,9 @@ const PARSE_CALLBACKS = {
83728373
const parseHtml = (input, pos = 0, options = {}) => {
83738374
source = input;
83748375
inputHasCr = input.includes("\r");
8375-
inputHasNul = input.includes("\0");
8376+
// The tokenizer reports every literal NUL before emitting its token, letting
8377+
// the parse-error callback set this flag without a separate full-input scan.
8378+
inputHasNul = false;
83768379
// Only an unknown name's entry is a slice of the last document; one naming a
83778380
// known element is a shared constant, and stays warm across parses.
83788381
_dropSourceBackedNames(TAG_NAME_INTERN);
@@ -8404,7 +8407,10 @@ const parseHtml = (input, pos = 0, options = {}) => {
84048407
// A long inline style/script or comment has very few markup delimiters, so
84058408
// estimating columns from its byte length wastes memory. Probe only until the
84068409
// document proves dense; `indexOf` skips the long raw-text spans natively.
8407-
if (nodeEstimate > _COLUMN_SHRINK_CAPACITY) {
8410+
if (
8411+
nodeEstimate > (_nodeCapacity || 4096) ||
8412+
attrEstimate > (_attrCapacity || 4096)
8413+
) {
84088414
let markupCount = 0;
84098415
let searchFrom = 0;
84108416
while (markupCount <= _SPARSE_MARKUP_LIMIT) {

0 commit comments

Comments
 (0)