Skip to content

Commit 78b8574

Browse files
alexander-akaitclaude
authored andcommitted
perf(css): concatenate a join seam instead of canonicalizing it
Deduplicating across the seam re-read the whole accumulated selector list once per rule joined, so a run of adjacent rules printing the same block was quadratic: 6000 of them took 2.8s where the base took 0.10s. It bought nothing measurable — no configCases stylesheet loses a byte without it — so the seam concatenates and the canonical form stays where the list is the author's own.
1 parent db04382 commit 78b8574

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8393,10 +8393,9 @@ const _joinRuleEntries = (before, entry, owned) => {
83938393
};
83948394
}
83958395
if (beforeBlock !== block || !before.listable || !entry.listable) return null;
8396-
// The seam can repeat a selector the other side already names, which matches
8397-
// nothing twice. Order is left alone: each half is already in its own, and
8398-
// re-sorting across the seam costs more compressed than the bytes it saves.
8399-
const list = _canonicalSelectorList(`${beforePrelude},${prelude}`, false);
8396+
// Concatenated, never canonicalized: a run of joins would then re-read the
8397+
// whole list once per rule, and no `configCases` sheet loses a byte to it.
8398+
const list = `${beforePrelude},${prelude}`;
84008399
return {
84018400
text: list + beforeBlock,
84028401
prelude: list.length,
@@ -8643,25 +8642,20 @@ const _isSorted = (list) => {
86438642
/**
86448643
* A selector list is a set: the same selectors in any order match the same
86458644
* elements at the same specificity, so a repeat inside it says nothing and one
8646-
* canonical order lets equal lists compress alike. `order` is passed only where
8647-
* the whole list is the author's — across a join seam each half already has its
8648-
* own, and re-sorting the two costs more compressed than the bytes it saves.
8645+
* canonical order lets equal lists compress alike. Only ever the author's own
8646+
* list — a join seam concatenates, so a run of them stays linear.
86498647
* @param {string} prelude the printed selector list
8650-
* @param {boolean} order whether to sort it as well as deduplicate it
86518648
* @returns {string} it, or `prelude` when nothing there is shorter
86528649
*/
8653-
const _canonicalSelectorList = (prelude, order) => {
8650+
const _canonicalSelectorList = (prelude) => {
86548651
// A list needs a comma; the great majority of preludes are one selector, and
86558652
// without one there is nothing to split, deduplicate or order.
86568653
if (!prelude.includes(",")) return prelude;
86578654
const list = _splitSelectorList(prelude);
86588655
if (list.length < 2) return prelude;
86598656
const seen = new Set(list);
8660-
const repeated = seen.size !== list.length;
8661-
if (!repeated && (!order || _isSorted(list))) return prelude;
8662-
const out = [...seen];
8663-
if (order) out.sort();
8664-
return out.join(",");
8657+
if (seen.size === list.length && _isSorted(list)) return prelude;
8658+
return [...seen].sort().join(",");
86658659
};
86668660

86678661
/**
@@ -8720,7 +8714,7 @@ const _rulePrelude = (path, writer, minify) => {
87208714
)
87218715
.join(",");
87228716
}
8723-
return _canonicalSelectorList(prelude, true);
8717+
return _canonicalSelectorList(prelude);
87248718
}
87258719
return prelude;
87268720
};

‎test/CssSyntax.unittest.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,10 @@ describe("CssSyntax — block streaming", () => {
875875
// The collected walk emits every declaration and only then every child
876876
// rule; a streamed block emits each child as it finishes, so declarations
877877
// and child rules interleave the way the source has them.
878-
const src = `@media screen{${repeat(1800, (i) => `p${i}:v${i};${rule(i)}`)}}`;
878+
const src = `@media screen{${repeat(
879+
1800,
880+
(i) => `p${i}:v${i};${rule(i)}`
881+
)}}`;
879882
const kinds = walk(src)
880883
.filter(
881884
(e) => e.startsWith("+Declaration|") || e.startsWith("+QualifiedRule|")
@@ -899,7 +902,10 @@ describe("CssSyntax — block streaming", () => {
899902
// The collected walk numbers the two lists independently (see `_walkRule`),
900903
// so a streamed block has to as well — not one counter running across the
901904
// merged source order.
902-
const src = `@media screen{${repeat(1800, (i) => `p${i}:v${i};${rule(i)}`)}}`;
905+
const src = `@media screen{${repeat(
906+
1800,
907+
(i) => `p${i}:v${i};${rule(i)}`
908+
)}}`;
903909
/** @type {string[]} */
904910
const seen = [];
905911
new SourceProcessor()
@@ -992,7 +998,9 @@ describe("CssSyntax — block streaming", () => {
992998
`.root{color:red;${repeat(n, (i) => `& .n${i}{color:red}`)}}`;
993999
/** @type {(n: number) => string} */
9941000
const joined = (n) =>
995-
`.root{color:red;${Array.from({ length: n }, (_, i) => `& .n${i}`).join(",")}{color:red}}`;
1001+
`.root{color:red;${Array.from({ length: n }, (_, i) => `& .n${i}`).join(
1002+
","
1003+
)}{color:red}}`;
9961004
expect(minify(nesting(4))).toBe(joined(4));
9971005
expect(minify(nesting(1800))).toBe(joined(1800));
9981006
});
@@ -1068,7 +1076,10 @@ describe("CssSyntax — block streaming", () => {
10681076
// `_mergeBoxLonghands` needs every declaration at once, and only runs in a
10691077
// block with no child rule — so such a block is never streamed, however far
10701078
// past the threshold it grows, and its four longhands still collapse.
1071-
const src = `.root{${repeat(20000, (i) => `--v${i}:${i};`)}margin-top:1px;margin-right:2px;margin-bottom:3px;margin-left:4px}`;
1079+
const src = `.root{${repeat(
1080+
20000,
1081+
(i) => `--v${i}:${i};`
1082+
)}margin-top:1px;margin-right:2px;margin-bottom:3px;margin-left:4px}`;
10721083
/** @type {number | null} */
10731084
let declared = null;
10741085
let seen = false;
@@ -3374,9 +3385,6 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
33743385
["a{& d,& c{top:0}}", "a{& c,& d{top:0}}"],
33753386
// Two rules reaching the same set are one rule once both are in order.
33763387
["a,b{color:red}b,a{top:0}", "a,b{color:red;top:0}"],
3377-
// A join can repeat what the other side already names — the repeat goes,
3378-
// but neither half is reordered across the seam.
3379-
[".b{x:1}.a{x:1}.b{x:1}", ".b,.a{x:1}"],
33803388
["z{color:red}a{color:red}", "z,a{color:red}"]
33813389
])("%s", (css, expected) => {
33823390
expect(minify(css)).toBe(expected);
@@ -3392,6 +3400,12 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
33923400
])("keeps the list where %s", (_name, css) => {
33933401
expect(minify(css)).toBe(css);
33943402
});
3403+
3404+
// A join concatenates: canonicalizing there would re-read the whole list
3405+
// once per rule joined, and no real stylesheet loses a byte to the repeat.
3406+
it("leaves a selector a join seam repeats", () => {
3407+
expect(minify(".b{x:1}.a{x:1}.b{x:1}")).toBe(".b,.a,.b{x:1}");
3408+
});
33953409
});
33963410

33973411
describe("a transform naming the same matrix", () => {

0 commit comments

Comments
 (0)