Skip to content

Commit 351217f

Browse files
perf(html): avoid redundant syntax parser scans
1 parent 6c8b79e commit 351217f

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
@@ -1155,9 +1155,8 @@ const _identNamesUrl = (input, start, end) => {
11551155
function consumeAnIdentLikeToken(input, pos, out) {
11561156
const start = pos;
11571157
pos = _consumeAnIdentSequence(input, pos);
1158-
const nextCodePoint = input.charCodeAt(pos);
11591158
if (
1160-
nextCodePoint === CC_LEFT_PARENTHESIS &&
1159+
input.charCodeAt(pos) === CC_LEFT_PARENTHESIS &&
11611160
_identNamesUrl(input, start, pos)
11621161
) {
11631162
pos++;
@@ -1183,7 +1182,7 @@ function consumeAnIdentLikeToken(input, pos, out) {
11831182
}
11841183
return consumeAUrlToken(input, pos, start, out);
11851184
}
1186-
if (nextCodePoint === CC_LEFT_PARENTHESIS) {
1185+
if (input.charCodeAt(pos) === CC_LEFT_PARENTHESIS) {
11871186
pos++;
11881187
return fill(out, TT_FUNCTION, start, pos);
11891188
}

‎lib/html/syntax.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8372,6 +8372,7 @@ const vetoesContentMode = (name) => {
83728372
/** @type {(input: string, code: string) => void} */
83738373
const handleParseError = (input, code) => {
83748374
if (code === "eof-in-tag") eofInTag = true;
8375+
else if (code === "unexpected-null-character") inputHasNul = true;
83758376
};
83768377
/**
83778378
* @param {string} input input
@@ -8724,7 +8725,9 @@ const PARSE_CALLBACKS = {
87248725
const parseHtml = (input, pos = 0, options = {}) => {
87258726
source = input;
87268727
inputHasCr = input.includes("\r");
8727-
inputHasNul = input.includes("\0");
8728+
// The tokenizer reports every literal NUL before emitting its token, letting
8729+
// the parse-error callback set this flag without a separate full-input scan.
8730+
inputHasNul = false;
87288731
// Only an unknown name's entry is a slice of the last document; one naming a
87298732
// known element is a shared constant, and stays warm across parses.
87308733
_dropSourceBackedNames(TAG_NAME_INTERN);
@@ -8758,7 +8761,10 @@ const parseHtml = (input, pos = 0, options = {}) => {
87588761
// A long inline style/script or comment has very few markup delimiters, so
87598762
// estimating columns from its byte length wastes memory. Probe only until the
87608763
// document proves dense; `indexOf` skips the long raw-text spans natively.
8761-
if (nodeEstimate > _COLUMN_SHRINK_CAPACITY) {
8764+
if (
8765+
nodeEstimate > (_nodeCapacity || 4096) ||
8766+
attrEstimate > (_attrCapacity || 4096)
8767+
) {
87628768
let markupCount = 0;
87638769
let searchFrom = 0;
87648770
while (markupCount <= _SPARSE_MARKUP_LIMIT) {

0 commit comments

Comments
 (0)