Skip to content

Commit bad274d

Browse files
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 b39cd05 commit bad274d

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
@@ -8324,10 +8324,9 @@ const _joinRuleEntries = (before, entry, owned) => {
83248324
};
83258325
}
83268326
if (beforeBlock !== block || !before.listable || !entry.listable) return null;
8327-
// The seam can repeat a selector the other side already names, which matches
8328-
// nothing twice. Order is left alone: each half is already in its own, and
8329-
// re-sorting across the seam costs more compressed than the bytes it saves.
8330-
const list = _canonicalSelectorList(`${beforePrelude},${prelude}`, false);
8327+
// Concatenated, never canonicalized: a run of joins would then re-read the
8328+
// whole list once per rule, and no `configCases` sheet loses a byte to it.
8329+
const list = `${beforePrelude},${prelude}`;
83318330
return {
83328331
text: list + beforeBlock,
83338332
prelude: list.length,
@@ -8574,25 +8573,20 @@ const _isSorted = (list) => {
85748573
/**
85758574
* A selector list is a set: the same selectors in any order match the same
85768575
* elements at the same specificity, so a repeat inside it says nothing and one
8577-
* canonical order lets equal lists compress alike. `order` is passed only where
8578-
* the whole list is the author's — across a join seam each half already has its
8579-
* own, and re-sorting the two costs more compressed than the bytes it saves.
8576+
* canonical order lets equal lists compress alike. Only ever the author's own
8577+
* list — a join seam concatenates, so a run of them stays linear.
85808578
* @param {string} prelude the printed selector list
8581-
* @param {boolean} order whether to sort it as well as deduplicate it
85828579
* @returns {string} it, or `prelude` when nothing there is shorter
85838580
*/
8584-
const _canonicalSelectorList = (prelude, order) => {
8581+
const _canonicalSelectorList = (prelude) => {
85858582
// A list needs a comma; the great majority of preludes are one selector, and
85868583
// without one there is nothing to split, deduplicate or order.
85878584
if (!prelude.includes(",")) return prelude;
85888585
const list = _splitSelectorList(prelude);
85898586
if (list.length < 2) return prelude;
85908587
const seen = new Set(list);
8591-
const repeated = seen.size !== list.length;
8592-
if (!repeated && (!order || _isSorted(list))) return prelude;
8593-
const out = [...seen];
8594-
if (order) out.sort();
8595-
return out.join(",");
8588+
if (seen.size === list.length && _isSorted(list)) return prelude;
8589+
return [...seen].sort().join(",");
85968590
};
85978591

85988592
/**
@@ -8651,7 +8645,7 @@ const _rulePrelude = (path, writer, minify) => {
86518645
)
86528646
.join(",");
86538647
}
8654-
return _canonicalSelectorList(prelude, true);
8648+
return _canonicalSelectorList(prelude);
86558649
}
86568650
return prelude;
86578651
};

‎test/CssSyntax.unittest.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,10 @@ describe("CssSyntax — block streaming", () => {
849849
// The collected walk emits every declaration and only then every child
850850
// rule; a streamed block emits each child as it finishes, so declarations
851851
// and child rules interleave the way the source has them.
852-
const src = `@media screen{${repeat(1800, (i) => `p${i}:v${i};${rule(i)}`)}}`;
852+
const src = `@media screen{${repeat(
853+
1800,
854+
(i) => `p${i}:v${i};${rule(i)}`
855+
)}}`;
853856
const kinds = walk(src)
854857
.filter(
855858
(e) => e.startsWith("+Declaration|") || e.startsWith("+QualifiedRule|")
@@ -873,7 +876,10 @@ describe("CssSyntax — block streaming", () => {
873876
// The collected walk numbers the two lists independently (see `_walkRule`),
874877
// so a streamed block has to as well — not one counter running across the
875878
// merged source order.
876-
const src = `@media screen{${repeat(1800, (i) => `p${i}:v${i};${rule(i)}`)}}`;
879+
const src = `@media screen{${repeat(
880+
1800,
881+
(i) => `p${i}:v${i};${rule(i)}`
882+
)}}`;
877883
/** @type {string[]} */
878884
const seen = [];
879885
new SourceProcessor()
@@ -966,7 +972,9 @@ describe("CssSyntax — block streaming", () => {
966972
`.root{color:red;${repeat(n, (i) => `& .n${i}{color:red}`)}}`;
967973
/** @type {(n: number) => string} */
968974
const joined = (n) =>
969-
`.root{color:red;${Array.from({ length: n }, (_, i) => `& .n${i}`).join(",")}{color:red}}`;
975+
`.root{color:red;${Array.from({ length: n }, (_, i) => `& .n${i}`).join(
976+
","
977+
)}{color:red}}`;
970978
expect(minify(nesting(4))).toBe(joined(4));
971979
expect(minify(nesting(1800))).toBe(joined(1800));
972980
});
@@ -1042,7 +1050,10 @@ describe("CssSyntax — block streaming", () => {
10421050
// `_mergeBoxLonghands` needs every declaration at once, and only runs in a
10431051
// block with no child rule — so such a block is never streamed, however far
10441052
// past the threshold it grows, and its four longhands still collapse.
1045-
const src = `.root{${repeat(20000, (i) => `--v${i}:${i};`)}margin-top:1px;margin-right:2px;margin-bottom:3px;margin-left:4px}`;
1053+
const src = `.root{${repeat(
1054+
20000,
1055+
(i) => `--v${i}:${i};`
1056+
)}margin-top:1px;margin-right:2px;margin-bottom:3px;margin-left:4px}`;
10461057
/** @type {number | null} */
10471058
let declared = null;
10481059
let seen = false;
@@ -3267,9 +3278,6 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
32673278
["a{& d,& c{top:0}}", "a{& c,& d{top:0}}"],
32683279
// Two rules reaching the same set are one rule once both are in order.
32693280
["a,b{color:red}b,a{top:0}", "a,b{color:red;top:0}"],
3270-
// A join can repeat what the other side already names — the repeat goes,
3271-
// but neither half is reordered across the seam.
3272-
[".b{x:1}.a{x:1}.b{x:1}", ".b,.a{x:1}"],
32733281
["z{color:red}a{color:red}", "z,a{color:red}"]
32743282
])("%s", (css, expected) => {
32753283
expect(minify(css)).toBe(expected);
@@ -3285,6 +3293,12 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
32853293
])("keeps the list where %s", (_name, css) => {
32863294
expect(minify(css)).toBe(css);
32873295
});
3296+
3297+
// A join concatenates: canonicalizing there would re-read the whole list
3298+
// once per rule joined, and no real stylesheet loses a byte to the repeat.
3299+
it("leaves a selector a join seam repeats", () => {
3300+
expect(minify(".b{x:1}.a{x:1}.b{x:1}")).toBe(".b,.a,.b{x:1}");
3301+
});
32883302
});
32893303

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

0 commit comments

Comments
 (0)