Skip to content

Commit 8735b62

Browse files
perf(html): speed up HTML minification (#22292)
* perf(html): read a token list once when normalizing it Record each token's span during the collapse check and compare packed keys for repeats, instead of rescanning the list with indexOf. * perf(html): decide scope boundaries once per element An element's namespace and name never change, so whether it stops a "have an element in scope" walk (§13.2.4.2) is a flag set at creation rather than set lookups on every walk step. * perf(css): find a short block's dead writes pair by pair A block holds two declarations on average, so comparing its items directly beats hashing every printed text into maps; blocks over 32 items keep the map pass. Skip the positional at-rule regexp on a text with no `@`.
1 parent 5934de7 commit 8735b62

2 files changed

Lines changed: 259 additions & 120 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 150 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12818,8 +12818,11 @@ const POSITIONAL_AT_RULES = new Set([
1281812818
* @returns {boolean} true where one is named
1281912819
*/
1282012820
const _namesPositionalAtRule = (text) =>
12821-
POSITIONAL_AT_RULE_RE.test(text) ||
12822-
(text.includes("\\") && _namesAtRule(text, POSITIONAL_AT_RULES));
12821+
// An at-keyword is written with a literal `@`, so a text without one names
12822+
// none, and a native scan answers that before the regexp runs.
12823+
text.includes("@") &&
12824+
(POSITIONAL_AT_RULE_RE.test(text) ||
12825+
(text.includes("\\") && _namesAtRule(text, POSITIONAL_AT_RULES)));
1282312826

1282412827
/**
1282512828
* Whether a node is an `@layer` at-rule carrying a name — the only shape a
@@ -13793,6 +13796,85 @@ const _mayBeMergeableName = (node) => {
1379313796
}
1379413797
return ((_mergeableNames()[slot] >>> (length > 31 ? 31 : length)) & 1) !== 0;
1379513798
};
13799+
// Up to this many items a block's dead writes are found by comparing pairs: a
13800+
// block holds two declarations on average, and hashing each text costs more.
13801+
const _PAIRWISE_ITEMS = 32;
13802+
/** @type {string[]} */
13803+
const _pairProperties = [];
13804+
const _pairGone = new Uint8Array(_PAIRWISE_ITEMS);
13805+
13806+
/**
13807+
* The dead-write pass of `_composeBlockBody` for a short block, pair by pair
13808+
* rather than through maps; it marks the same items superseded.
13809+
* @param {CssPath} path the walk's accessors
13810+
* @param {Node[]} items the block's declarations and rules, in source order
13811+
* @param {string[]} texts each item's printed text
13812+
* @param {Set<number>} superseded where a dead item's index is added
13813+
* @param {boolean} keyframe whether the block is a keyframe
13814+
* @returns {void}
13815+
*/
13816+
const _supersedeByPairs = (path, items, texts, superseded, keyframe) => {
13817+
const properties = _pairProperties;
13818+
const gone = _pairGone;
13819+
// Where the run of declarations since the last nested rule starts: a rule
13820+
// parts the ones either side of it into their own places in the cascade.
13821+
let segment = 0;
13822+
for (let i = 0; i < items.length; i++) {
13823+
const text = texts[i];
13824+
properties[i] = "";
13825+
if (path.type(items[i]) !== T_DECLARATION) {
13826+
segment = i + 1;
13827+
// An identical later sibling wins the tie wherever both are read.
13828+
if (text.length !== 0 && !_namesPositionalAtRule(text)) {
13829+
for (let k = 0; k < i; k++) {
13830+
if (texts[k] === text && path.type(items[k]) !== T_DECLARATION) {
13831+
superseded.add(k);
13832+
}
13833+
}
13834+
}
13835+
continue;
13836+
}
13837+
gone[i] = 0;
13838+
for (let k = segment; k < i; k++) {
13839+
if (texts[k] === text) superseded.add(k);
13840+
}
13841+
const colon = text.indexOf(":");
13842+
// A custom property's value is a token stream nothing here reads.
13843+
if (colon <= 0 || text.charCodeAt(1) === CC_HYPHEN_MINUS) continue;
13844+
const property = _printedProperty(text, colon);
13845+
properties[i] = property;
13846+
let value = "";
13847+
let names = false;
13848+
let read = false;
13849+
for (let k = i - 1; k >= segment; k--) {
13850+
if (gone[k] !== 0 || properties[k] !== property) continue;
13851+
if (!read) {
13852+
value = _printedValue(text, colon);
13853+
names = CUSTOM_IDENT_LIST_PROPERTIES.has(property);
13854+
read = true;
13855+
}
13856+
const beforeText = texts[k];
13857+
const written = _printedValue(beforeText, beforeText.indexOf(":"));
13858+
if (
13859+
_laterWriteWins(
13860+
path,
13861+
items[i],
13862+
value,
13863+
items[k],
13864+
written,
13865+
property,
13866+
names,
13867+
keyframe
13868+
)
13869+
) {
13870+
superseded.add(k);
13871+
gone[k] = 1;
13872+
}
13873+
}
13874+
}
13875+
properties.length = 0;
13876+
};
13877+
1379613878
/**
1379713879
* Put one declaration list together: the list-wide transforms (merge a family's
1379813880
* longhands into their shorthand, drop what a later declaration supersedes, add
@@ -13982,69 +14064,76 @@ const _composeBlockBody = (
1398214064
if (deadWrites.has(items[i])) superseded.add(i);
1398314065
}
1398414066
}
13985-
/** @type {Map<string, number>} */
13986-
const seen = new Map();
13987-
/** @type {Map<string, number[]>} */
13988-
const wrote = new Map();
13989-
/** @type {Map<string, number> | null} */
13990-
let seenRules = null;
13991-
for (let i = 0; i < items.length; i++) {
13992-
// A nested rule parts the declarations around it into their own
13993-
// rules, so the one before it is read at its own place in the
13994-
// cascade rather than resolving to the one after.
13995-
if (path.type(items[i]) !== T_DECLARATION) {
13996-
seen.clear();
13997-
wrote.clear();
13998-
const rule = texts[i];
13999-
// An identical later sibling wins the tie wherever both are read, so
14000-
// this one is read for nothing whatever stands between them.
14001-
if (rule.length !== 0 && !_namesPositionalAtRule(rule)) {
14002-
if (seenRules === null) seenRules = new Map();
14003-
const before = seenRules.get(rule);
14004-
if (before !== undefined) superseded.add(before);
14005-
seenRules.set(rule, i);
14067+
if (items.length <= _PAIRWISE_ITEMS) {
14068+
_supersedeByPairs(path, items, texts, superseded, keyframe);
14069+
} else {
14070+
/** @type {Map<string, number>} */
14071+
const seen = new Map();
14072+
/** @type {Map<string, number[]>} */
14073+
const wrote = new Map();
14074+
/** @type {Map<string, number> | null} */
14075+
let seenRules = null;
14076+
for (let i = 0; i < items.length; i++) {
14077+
// A nested rule parts the declarations around it into their own
14078+
// rules, so the one before it is read at its own place in the
14079+
// cascade rather than resolving to the one after.
14080+
if (path.type(items[i]) !== T_DECLARATION) {
14081+
seen.clear();
14082+
wrote.clear();
14083+
const rule = texts[i];
14084+
// An identical later sibling wins the tie wherever both are read, so
14085+
// this one is read for nothing whatever stands between them.
14086+
if (rule.length !== 0 && !_namesPositionalAtRule(rule)) {
14087+
if (seenRules === null) seenRules = new Map();
14088+
const before = seenRules.get(rule);
14089+
if (before !== undefined) superseded.add(before);
14090+
seenRules.set(rule, i);
14091+
}
14092+
continue;
1400614093
}
14007-
continue;
14008-
}
14009-
const text = texts[i];
14010-
const at = seen.get(text);
14011-
if (at !== undefined) superseded.add(at);
14012-
seen.set(text, i);
14013-
const colon = text.indexOf(":");
14014-
// A custom property's value is a token stream nothing here reads, so
14015-
// what an engine makes of it is not knowable from its spelling.
14016-
if (colon <= 0 || text.charCodeAt(1) === CC_HYPHEN_MINUS) continue;
14017-
const property = _printedProperty(text, colon);
14018-
const names = CUSTOM_IDENT_LIST_PROPERTIES.has(property);
14019-
let earlier = wrote.get(property);
14020-
if (earlier === undefined) {
14021-
earlier = [];
14022-
wrote.set(property, earlier);
14023-
} else {
14024-
const value = _printedValue(text, colon);
14025-
for (let j = earlier.length - 1; j >= 0; j--) {
14026-
const before = earlier[j];
14027-
const beforeText = texts[before];
14028-
const written = _printedValue(beforeText, beforeText.indexOf(":"));
14029-
if (
14030-
!_laterWriteWins(
14031-
path,
14032-
items[i],
14033-
value,
14034-
items[before],
14035-
written,
14036-
property,
14037-
names,
14038-
keyframe
14039-
)
14040-
) {
14041-
continue;
14094+
const text = texts[i];
14095+
const at = seen.get(text);
14096+
if (at !== undefined) superseded.add(at);
14097+
seen.set(text, i);
14098+
const colon = text.indexOf(":");
14099+
// A custom property's value is a token stream nothing here reads, so
14100+
// what an engine makes of it is not knowable from its spelling.
14101+
if (colon <= 0 || text.charCodeAt(1) === CC_HYPHEN_MINUS) continue;
14102+
const property = _printedProperty(text, colon);
14103+
const names = CUSTOM_IDENT_LIST_PROPERTIES.has(property);
14104+
let earlier = wrote.get(property);
14105+
if (earlier === undefined) {
14106+
earlier = [];
14107+
wrote.set(property, earlier);
14108+
} else {
14109+
const value = _printedValue(text, colon);
14110+
for (let j = earlier.length - 1; j >= 0; j--) {
14111+
const before = earlier[j];
14112+
const beforeText = texts[before];
14113+
const written = _printedValue(
14114+
beforeText,
14115+
beforeText.indexOf(":")
14116+
);
14117+
if (
14118+
!_laterWriteWins(
14119+
path,
14120+
items[i],
14121+
value,
14122+
items[before],
14123+
written,
14124+
property,
14125+
names,
14126+
keyframe
14127+
)
14128+
) {
14129+
continue;
14130+
}
14131+
superseded.add(before);
14132+
earlier.splice(j, 1);
1404214133
}
14043-
superseded.add(before);
14044-
earlier.splice(j, 1);
1404514134
}
14135+
earlier.push(i);
1404614136
}
14047-
earlier.push(i);
1404814137
}
1404914138
}
1405014139
// After the supersedes: a declaration a later one already made dead is

0 commit comments

Comments
 (0)