Skip to content

Commit 0b5c4e9

Browse files
perf(css): scope duplicate rules, and reuse interned names and minified style attributes (#21832)
* perf: reuse interned names and minified style attributes across a print Three per-invocation costs on the HTML and CSS hot paths, none of which changes a byte of output — the whole wpt corpus (49,571 documents, minified and beautified) hashes identically before and after. Keep the name intern memo warm across parses. Only an unknown name's entry is a slice of the document that asked for it; one naming a known element is a shared constant, so clearing all 1,024 slots per parse threw away a warm cache to drop the few that needed dropping. Tracking the source-backed slots takes the clear from 1,024 entries to 0.7 per parse and the share of intern calls reaching the hash walk from 39.4% to 6.5% (520,442 fewer walks). Memoize a `style` attribute's minified text for the print. Minifying one is a pure function of the value and the CSS options, which are fixed for the print, and 41.8% of the corpus's style attributes repeat inside their own document — so the CSS minifier runs once per distinct value instead of once per attribute. Dropped in the teardown with the input it holds slices of. Reset only the stream frames a parse used. Nesting is one level deep in almost every sheet, but the teardown cleared all 64 slots of six arrays. Measured over the corpus, interleaved, n=9: HTML minification ~9% faster, beautification ~8% (which reaches no CSS, so it is the interning alone). * perf(css): scope duplicate rules instead of re-keying them by their whole nesting Finding a rule an identical later one makes dead was 17% of minifying a stylesheet, and only a seventh of that was the cutting. The rest was the bookkeeping: a rule's key was the text it printed to with every enclosing opener glued in front, rebuilt at each level it travelled outward through, and every one of them kept in a single sheet-wide map. A rule is now identified by the pair it always was - what encloses it, and the text it printed to - rather than by those two joined. Each set of enclosing conditions gets a scope holding its own rules by their text, and a span carries a reference to one rather than a string. Travelling outward replaces the reference; neither the text nor a scope's prefix is ever rewritten. Scopes are canonical by their joined prefix, so the same conditions reached a level at a time and reached in a single step are the one scope - which is what lets the collected and the streamed path still agree on what a duplicate is. The prefix is joined once per block, memoized on the scope it extends. Over the 14 framework stylesheets `yarn benchmark:css-minifiers` runs on (6.72 MB): 34,020 string joins become 851, key hashing drops from 6.8M to 5.3M characters, and the sheet-wide map of long keys is gone. Dedup's own cost falls from 138 ms to 68 ms and minifying them from 786 ms to 748 ms; measured as ten alternating single-process pairs, all ten favour this, median ~7%, against a before-vs-before control centred on zero. Parsing and beautifying are unchanged, beautifying being the control that reaches no dedup at all. Output is byte-identical over 43,587 inputs - those stylesheets, every wpt style body and style attribute, and the wpt css corpus - in both print modes. * fix(html): note a name memo slot once, however many names land in it The list of slots to drop before the next parse grew by one entry per unknown name written, not per slot dirtied — so a document of custom elements or data-* attributes carried a list far longer than the 512 slots it can possibly have dirtied: 50,000 custom elements left over 10,000 entries where at most 512 are reachable. Marking a slot when it first enters the list bounds it, and the flags are cleared with the slots they stand for rather than all at once, so a parse still pays for what it dirtied. Output is unchanged over the whole wpt corpus in both print modes. * test(html): repeat the raw value the style-attribute memo is keyed on The case named for a repeated `style` attribute wrote two different values, so the second never found the first and the memoized return was never taken — instrumenting the cache counts 0 hits as it was written and 1 as it is now. Also brings the changeset entry back under 80 characters. * test(html): dirty every memo slot, not five of them A slot is picked by a name's first character and length, so 20,000 names all starting `x-el` reached about five of the 512 — leaving the branch that drops the memo whole unrun, which is the patch coverage the report flagged. Names of 512 consecutive lengths reach every slot; the region is covered with them. * fix(test): type the memo test's walk, and shorten two comments `lint:types-test` reads `test/` with `noImplicitAny`, and the walk added with the memo test annotated neither its accumulator nor its parameter. Types them the way the file's other refs are typed, and brings the two comments the same commits added down to two lines. * fix(html): never memoize a caller's renderer for a style attribute `renderEmbeddedSource` is handed every `style=""` a document holds, so what it returns is not a function of the value alone — a renderer keeping a counter, or reading anything outside the text, gets called once for a value the document repeats and its first answer stands in for the rest. The memo now covers only the built-in minifier, which is a pure function of the value and the options. The two paths only met when this branch rebased onto the renderer, so nothing failed: instrumenting the cache counts one renderer call for two equal attributes before, and two after.
1 parent 8750c1f commit 0b5c4e9

5 files changed

Lines changed: 307 additions & 28 deletions

File tree

‎.changeset/072-css-html-minify-hot-paths.md‎

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

5-
Speed up CSS and HTML minification, cut allocations; select output with `mode`.
5+
Speed up CSS and HTML parsing and minification; select output with `mode`.

‎lib/css/syntax.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4279,6 +4279,9 @@ let _streamWalked = null;
42794279
// numeric: typed columns keep a write off the GC's books.
42804280
const _STREAM_MAX_DEPTH = 64;
42814281
let _depth = 0;
4282+
// The deepest frame this parse wrote, so the reset clears what was used rather
4283+
// than all `_STREAM_MAX_DEPTH` slots — nesting is 1 deep in almost every sheet.
4284+
let _frameHighWater = 0;
42824285
/** @type {Int32Array} node id of the open rule */
42834286
const _frameRule = new Int32Array(_STREAM_MAX_DEPTH);
42844287
const _frameMark = new Int32Array(_STREAM_MAX_DEPTH);
@@ -4326,7 +4329,14 @@ const _frameSeenRules = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
43264329
/** @type {(string | null)[]} */
43274330
const _frameChainKey = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
43284331
/**
4329-
* @typedef {{ key: string, at: number, len: number }} RuleSpan
4332+
* A rule the printer has written, at `[at, at + len)` of the piece holding it.
4333+
* `key` is the text it printed to and `scope` what encloses it; a later rule
4334+
* with both the same is this one written again. Neither is rewritten as the span
4335+
* travels outward — only `scope` is replaced, by one standing for more — so the
4336+
* collected and the streamed path split a rule the same way, which is what makes
4337+
* the two agree on what a duplicate is.
4338+
* @typedef {{ scope: RuleScope, key: string, at: number, len: number }} RuleSpan
4339+
* @typedef {{ prefix: string, rules: Map<string, { taken: TakenPiece, span: RuleSpan }>, inner: Map<string, RuleScope> }} RuleScope
43304340
* @typedef {{ bodyAt: number, prelude: string, keyPrelude: string, qualified: boolean, spans: RuleSpan[] }} BlockSpans
43314341
*/
43324342
// Where each finished block's rules landed in it, youngest last — by print
@@ -4362,10 +4372,62 @@ const _NO_BLOCK_ENTRY_QUALIFIED = Object.freeze({
43624372
/**
43634373
* @typedef {{ piece: number, text: string, spans: RuleSpan[], empties: number }} TakenPiece
43644374
*/
4365-
// Every rule taken so far, sheet-wide, to where it sits. One joined key, not a
4366-
// level per part: only that makes the collected and streamed paths agree.
4367-
/** @type {Map<string, { taken: TakenPiece, span: RuleSpan }> | null} */
4368-
let _seenRuleKeys = null;
4375+
// Every rule taken so far, sheet-wide, by what encloses it and then by the text
4376+
// it printed to — neither of which changes once written (see `RuleSpan`).
4377+
/** @type {Map<string, RuleScope> | null} */
4378+
let _ruleScopes = null;
4379+
/** @type {RuleScope | null} */
4380+
let _rootRuleScope = null;
4381+
4382+
/**
4383+
* The scope for `prefix`, made once and shared — so a chain reached a level at a
4384+
* time and the same one reached in a single step are the one scope.
4385+
* @param {string} prefix what encloses the rules in it, "" at the top level
4386+
* @returns {RuleScope} the scope
4387+
*/
4388+
const _ruleScopeFor = (prefix) => {
4389+
let scopes = _ruleScopes;
4390+
if (scopes === null) {
4391+
scopes = new Map();
4392+
_ruleScopes = scopes;
4393+
}
4394+
let scope = scopes.get(prefix);
4395+
if (scope === undefined) {
4396+
scope = { prefix, rules: new Map(), inner: new Map() };
4397+
scopes.set(prefix, scope);
4398+
}
4399+
return scope;
4400+
};
4401+
4402+
/**
4403+
* The top-level scope, where a rule nothing encloses lands.
4404+
* @returns {RuleScope} the root scope
4405+
*/
4406+
const _rootScope = () => {
4407+
let root = _rootRuleScope;
4408+
if (root === null) {
4409+
root = _ruleScopeFor("");
4410+
_rootRuleScope = root;
4411+
}
4412+
return root;
4413+
};
4414+
4415+
/**
4416+
* The scope a scope's rules land in once `head` encloses them. Memoized on the
4417+
* scope, so the join runs once per block rather than once per rule in it.
4418+
* @param {RuleScope} scope the scope the rules are in now
4419+
* @param {string} head what now encloses them
4420+
* @returns {RuleScope} the enclosing scope
4421+
*/
4422+
const _enclosingRuleScope = (scope, head) => {
4423+
if (head.length === 0) return scope;
4424+
let inner = scope.inner.get(head);
4425+
if (inner === undefined) {
4426+
inner = _ruleScopeFor(`${head}${scope.prefix}`);
4427+
scope.inner.set(head, inner);
4428+
}
4429+
return inner;
4430+
};
43694431
// CSS Cascade 5 §6.4.1: an `!important` declaration is read from the earliest
43704432
// layer, so a copy in a later `@layer {` makes nothing dead.
43714433
let _anonymousLayers = 0;
@@ -4838,6 +4900,7 @@ const _streamConsumeBlock = (ts, rule) => {
48384900
// and writes one only if it could still be read (see `_streamPublishFrame`),
48394901
// so a rule whose block is all declarations costs what it always did.
48404902
_depth = d + 1;
4903+
if (_depth > _frameHighWater) _frameHighWater = _depth;
48414904
consumeABlocksContentsInto(ts, undefined, d, rule);
48424905
_depth = d;
48434906
const streamed = _bcStreamed;
@@ -4934,13 +4997,16 @@ const _drainTopLevelSpans = (node) => {
49344997
const _topLevelSpans = (own, text) => {
49354998
if (own === undefined) return null;
49364999
if (own === _WHOLE_TEXT_SPANS) {
4937-
return [{ key: text, at: 0, len: text.length }];
5000+
return [{ scope: _rootScope(), key: text, at: 0, len: text.length }];
49385001
}
49395002
/** @type {RuleSpan[]} */
4940-
const out = own.qualified ? [{ key: text, at: 0, len: text.length }] : [];
5003+
const out = own.qualified
5004+
? [{ scope: _rootScope(), key: text, at: 0, len: text.length }]
5005+
: [];
49415006
for (const span of own.spans) {
49425007
out.push({
4943-
key: `${own.keyPrelude}${span.key}`,
5008+
scope: _enclosingRuleScope(span.scope, own.keyPrelude),
5009+
key: span.key,
49445010
at: own.bodyAt + span.at,
49455011
len: span.len
49465012
});
@@ -4988,17 +5054,17 @@ const _takeDeduped = (writer, piece, text, spans, own, chain) => {
49885054
* @returns {void}
49895055
*/
49905056
const _noteSpans = (writer, taken, spans, chain) => {
4991-
if (_seenRuleKeys === null) _seenRuleKeys = new Map();
49925057
// Every span joins the piece before any cutting: a cut here moves what
49935058
// `taken.spans` holds, and one not yet pushed would keep its old offset.
49945059
for (const span of spans) {
4995-
if (chain.length !== 0) span.key = `${chain}${span.key}`;
5060+
if (chain.length !== 0) span.scope = _enclosingRuleScope(span.scope, chain);
49965061
taken.spans.push(span);
49975062
}
49985063
for (const span of spans) {
4999-
const before = _seenRuleKeys.get(span.key);
5064+
const rules = span.scope.rules;
5065+
const before = rules.get(span.key);
50005066
if (before !== undefined) _cutSpan(writer, before.taken, before.span);
5001-
_seenRuleKeys.set(span.key, { taken, span });
5067+
rules.set(span.key, { taken, span });
50025068
}
50035069
};
50045070

@@ -5483,14 +5549,17 @@ const grammar = (input, visitors, writer, options) => {
54835549
_depth = 0;
54845550
// A frame is left as the block closed it (nothing reads a closed one), so
54855551
// the last open path's buffers are dropped here rather than per block.
5486-
_frameDecls.fill(null);
5487-
_frameRules.fill(null);
5488-
_frameSeenDeclarations.fill(null);
5489-
_frameSeenRules.fill(null);
5490-
_frameChainKey.fill(null);
5491-
_frameSeenLayers.fill(null);
5552+
const used = _frameHighWater + 1;
5553+
_frameDecls.fill(null, 0, used);
5554+
_frameRules.fill(null, 0, used);
5555+
_frameSeenDeclarations.fill(null, 0, used);
5556+
_frameSeenRules.fill(null, 0, used);
5557+
_frameChainKey.fill(null, 0, used);
5558+
_frameSeenLayers.fill(null, 0, used);
5559+
_frameHighWater = 0;
54925560
_blockSpans.length = 0;
5493-
_seenRuleKeys = null;
5561+
_ruleScopes = null;
5562+
_rootRuleScope = null;
54945563
_anonymousLayers = 0;
54955564
_input = "";
54965565
_locConverter = /** @type {LocConverter} */ (/** @type {unknown} */ (null));
@@ -12690,13 +12759,14 @@ const _collectRuleSpans = (spans, item, text, at, path, inner) => {
1269012759
// it — the parent first, so a cut that takes the whole rule is seen before
1269112760
// anything inside it.
1269212761
if (path.type(item) === T_QUALIFIED_RULE) {
12693-
spans.push({ key: text, at, len: text.length });
12762+
spans.push({ scope: _rootScope(), key: text, at, len: text.length });
1269412763
}
1269512764
if (inner === undefined) return;
1269612765
const base = at + inner.bodyAt;
1269712766
for (const span of inner.spans) {
1269812767
spans.push({
12699-
key: `${inner.keyPrelude}${span.key}`,
12768+
scope: _enclosingRuleScope(span.scope, inner.keyPrelude),
12769+
key: span.key,
1270012770
at: base + span.at,
1270112771
len: span.len
1270212772
});

‎lib/html/syntax.js‎

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,7 +5418,7 @@ _TEXT_SCAN_CLASS[0x0d] = 2;
54185418

54195419
/**
54205420
* @param {Iterable<string>} names lowercase names to intern
5421-
* @returns {{ mask: number, hashes: Int32Array, values: (string | undefined)[], memo: string[] }} open-addressed intern table
5421+
* @returns {{ mask: number, hashes: Int32Array, values: (string | undefined)[], memo: string[], sourceBacked: number[], noted: Uint8Array }} open-addressed intern table
54225422
*/
54235423
const buildNameInternTable = (names) => {
54245424
// Open-addressed table (~25% load): the per-name probe is one or two array
@@ -5441,11 +5441,44 @@ const buildNameInternTable = (names) => {
54415441
}
54425442
// `memo` short-circuits the hash walk for a name seen a moment ago (see
54435443
// `internLowerName`); "" marks an empty slot so the load stays monomorphic.
5444-
return { mask, hashes, values, memo: Array.from({ length: 512 }).fill("") };
5444+
// `sourceBacked` lists the slots holding a slice of the parsed input — the
5445+
// only ones the next parse drops — and `noted` keeps each in it once.
5446+
return {
5447+
mask,
5448+
hashes,
5449+
values,
5450+
memo: Array.from({ length: 512 }).fill(""),
5451+
sourceBacked: [],
5452+
noted: new Uint8Array(512)
5453+
};
54455454
};
54465455

54475456
/** @typedef {ReturnType<typeof buildNameInternTable>} NameInternTable */
54485457

5458+
/**
5459+
* Drop the memo entries holding a slice of the last parsed input, so no parse
5460+
* retains another's source. Past the memo's own size a full clear is cheaper.
5461+
* @param {NameInternTable} table intern table
5462+
* @returns {void}
5463+
*/
5464+
const _dropSourceBackedNames = (table) => {
5465+
const slots = table.sourceBacked;
5466+
if (slots.length === 0) return;
5467+
const { memo, noted } = table;
5468+
// A slot a known name has since claimed is dropped with the rest: the entry is
5469+
// safe to keep, but noting that costs a write on the path every name takes.
5470+
if (slots.length >= memo.length) {
5471+
memo.fill("");
5472+
noted.fill(0);
5473+
} else {
5474+
for (const slot of slots) {
5475+
memo[slot] = "";
5476+
noted[slot] = 0;
5477+
}
5478+
}
5479+
slots.length = 0;
5480+
};
5481+
54495482
/**
54505483
* The lowercased name for `input[start..end)`, returning the shared interned
54515484
* string for known names — skipping the per-tag `slice().toLowerCase()`
@@ -5491,6 +5524,10 @@ const internLowerName = (table, input, start, end) => {
54915524
// Unknown name (custom element, data-* attribute, non-ASCII, …). Only ASCII
54925525
// folds — a Unicode `toLowerCase` maps U+212A onto `k` and U+0130 onto two
54935526
// characters, neither of which the tokenizer does.
5527+
if (table.noted[memoSlot] === 0) {
5528+
table.noted[memoSlot] = 1;
5529+
table.sourceBacked.push(memoSlot);
5530+
}
54945531
return (memo[memoSlot] = _asciiLowerCase(input.slice(start, end)));
54955532
};
54965533

@@ -8428,10 +8465,10 @@ const parseHtml = (input, pos = 0, options = {}) => {
84288465
source = input;
84298466
inputHasCr = input.includes("\r");
84308467
inputHasNul = input.includes("\0");
8431-
// A memo entry for an unknown name is a slice backed by the previous
8432-
// document's source — clear both so no parse retains another's input.
8433-
TAG_NAME_INTERN.memo.fill("");
8434-
ATTR_NAME_INTERN.memo.fill("");
8468+
// Only an unknown name's entry is a slice of the last document; one naming a
8469+
// known element is a shared constant, and stays warm across parses.
8470+
_dropSourceBackedNames(TAG_NAME_INTERN);
8471+
_dropSourceBackedNames(ATTR_NAME_INTERN);
84358472
const { fragmentContext, skip = EMPTY_SKIP } = options;
84368473
skipText = skip.text === true;
84378474
skipComments = skip.comments === true;
@@ -9176,6 +9213,11 @@ let _cssConvertLengthUnits = false;
91769213
// And whether it may shorten a custom property's value, forwarded the same way.
91779214
let _cssRewriteCustomProperties = false;
91789215

9216+
// Minified `style` attributes by raw text — a pure function of the value and
9217+
// the options above, which a document repeats far more often than it adds one.
9218+
/** @type {Map<string, string>} */
9219+
const _styleAttributeCache = new Map();
9220+
91799221
// How far text whitespace may be collapsed (see `HtmlProcessOptions`) — set per
91809222
// print, like `_cssEnvironment`. "none" is off; the rest name how much of the
91819223
// whitespace against a boundary no line box reaches goes with it.
@@ -9487,6 +9529,26 @@ const _escapesStyleWrapper = (value) => {
94879529
* @returns {string} the minified declarations
94889530
*/
94899531
const _minifyStyleAttribute = (raw) => {
9532+
// A caller's renderer is handed every `style=""` the document holds, so it is
9533+
// not a function of the value alone and nothing about it may be reused.
9534+
if (_renderEmbeddedSource !== undefined) {
9535+
return _minifyStyleAttributeUncached(raw);
9536+
}
9537+
const memoized = _styleAttributeCache.get(raw);
9538+
if (memoized !== undefined) return memoized;
9539+
const minified = _minifyStyleAttributeUncached(raw);
9540+
_styleAttributeCache.set(raw, minified);
9541+
return minified;
9542+
};
9543+
9544+
/**
9545+
* `_minifyStyleAttribute` without the memo — every path here runs the CSS
9546+
* minifier, and every return is safe to keep, none of them reading state the
9547+
* print goes on to change.
9548+
* @param {string} raw raw (undecoded) attribute value
9549+
* @returns {string} the minified declarations
9550+
*/
9551+
const _minifyStyleAttributeUncached = (raw) => {
94909552
if (raw.trim() === "" || _escapesStyleWrapper(raw)) return raw;
94919553
// Wrapped here rather than at the renderer, so a caller's minifier is always
94929554
// handed a whole stylesheet and never has to know about this position.
@@ -12186,6 +12248,8 @@ const grammar = (input, visitors, writer, options) => {
1218612248
// Keyed by this parse's interned names, so it must not outlive it.
1218712249
_attributeFrequencies = null;
1218812250
_attributeRanks = null;
12251+
// Keyed by this print's options, and holding slices of its input.
12252+
_styleAttributeCache.clear();
1218912253
_emptyElements.clear();
1219012254
_streamEligible = false;
1219112255
_streaming = false;

‎test/CssSyntax.unittest.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6750,3 +6750,47 @@ describe("SourceProcessor — renderEmbeddedSource over a data: url", () => {
67506750
}
67516751
});
67526752
});
6753+
6754+
describe("CssSyntax minify — what a duplicate rule is scoped to", () => {
6755+
const { SourceProcessor } = require("../lib/css/syntax");
6756+
6757+
/**
6758+
* @param {string} css input stylesheet
6759+
* @returns {string} the minified stylesheet
6760+
*/
6761+
const minify = (css) =>
6762+
new SourceProcessor().process(css, { mode: "minify" }).code;
6763+
6764+
it("takes back a rule an identical later one under the same conditions kills", () => {
6765+
expect(
6766+
minify("@media print{.a{color:red}.b{color:#00f}.a{color:red}}")
6767+
).toBe("@media print{.b{color:#00f}.a{color:red}}");
6768+
});
6769+
6770+
it("keeps both when the conditions differ", () => {
6771+
expect(
6772+
minify("@media print{.a{color:red}}@media screen{.a{color:red}}")
6773+
).toBe("@media print{.a{color:red}}@media screen{.a{color:red}}");
6774+
});
6775+
6776+
it("keeps both when one is nested deeper under the same outer condition", () => {
6777+
expect(
6778+
minify("@media print{@supports (a:b){.a{color:red}}.a{color:red}}")
6779+
).toBe("@media print{@supports (a:b){.a{color:red}}.a{color:red}}");
6780+
});
6781+
6782+
it("folds two blocks that say the same thing into one", () => {
6783+
expect(
6784+
minify(
6785+
"@media print{@supports (a:b){.a{color:red}}}" +
6786+
"@media print{@supports (a:b){.a{color:red}}}"
6787+
)
6788+
).toBe("@media print{@supports (a:b){.a{color:red}}}");
6789+
});
6790+
6791+
it("does not read a rule as one whose text merely starts the same", () => {
6792+
expect(minify("@media print{.a{color:red}.a{color:redd}}")).toBe(
6793+
"@media print{.a{color:red}.a{color:redd}}"
6794+
);
6795+
});
6796+
});

0 commit comments

Comments
 (0)