Skip to content

Commit 4015bbd

Browse files
perf(css): write selectors, An+B and more values in their shortest spelling (#21664)
* perf(css): reduce more transforms and name the full keyframe `to` `scale()` with a factor of 1 on one axis is that axis's own function, and a translation's zero component is zero in either length or percentage. A keyframe selector of 100% is the `to` CSS Animations 1 §4 names it. * perf(css): write a selector list in one order, without repeats A selector list is a set: the same selectors in any order match the same elements at the same specificity. Writing one canonical order lets equal lists compress alike and lets two rules naming one set join, and a repeat in it matches nothing the first already did. The equivalence harness compares a run of selectors reaching the same block under the same conditions as the set it is, which is the invariant joining them into one list already rests on. * fix(css): part a sibling combinator from a class without a comment CSS Syntax 3 §4.3.10 starts a number at `+` only before a digit, or before a `.` that itself has one. Treating every `+.` as a number made `.a+.m` print as `.a+/**/.m`, longer than the input it came from and different again on a second pass. A join also drops a selector the other side already names. * perf(css): write An+B, a 3D matrix and a zero time in their shortest spelling `An+B` carries its own whitespace, signs and case, an index under 1 matches nothing so a forward step starts where it does, and a step selecting exactly one child is the child Selectors 4 §14 names. A 3D matrix whose third row and column are the identity's is the 2D matrix of the six values it leaves, and a 2D pair of 1 leaves the z scale. A zero time keeps a unit where a zero length drops one, so it takes the shorter of the two it can carry. The equivalence harness reads each An+B as the sequence it selects: the engine folds `even` / `odd` itself but hands `0n+3` back as written. * perf(css): drop the implied universal inside a selector function A selector function's argument is a selector like the prelude's, so the universal a compound implies says nothing there either — `:not(*.g)` is `:not(.g)`. A lone universal is the selector, a parted one is a combinator away, and a namespaced one is not implied. * refactor(css): derive the An+B named equivalents, and split a list only when it is one The first/last counterpart of each An+B pseudo-class was a table typed into the printer; it is now read out of the selector table in the generator — the An+B half by its grammar, the counterpart by the name the pair builds, kept only where the table has it as a selector too. The two selector-list helpers were one function apart, and both split every prelude to find out there was nothing to do: 50,434 of the corpus's 55,055 hold no comma at all, so a list is only built where one can exist. `scale` and `rotate3d` asked whether a component was one in two different spellings; both now use the same test, which also reads `1.0`. * perf(css): index a rule's declarations once for all three merge tables Every rule with two declarations was offered to three shorthand tables, and each built the same property index again: 48,204 Map and Set allocations across the corpus where one reused pair does. The table scan asked for its longhands through a closure per entry — 401,700 of them — which is now a loop. And `_isJoinablePrelude` split every prelude into a list to walk it, where 50,434 of the corpus's 55,055 hold one selector and need no list at all. Counted, not timed: the output is byte-identical on all 18 fixtures, and the wall-clock difference on this machine is inside its own run-to-run spread. * perf(css): drop a component that spells the property's own initial `grid-auto-flow:row dense` is the `dense` omitting its group already means. The keyword is read out of the property table and kept only where it really is the initial and really is one alternative of a top-level `||` group; which of those groups falls back to it is the judgement stated in the generator, since `aspect-ratio:auto 3/2` is a ratio with a fallback rather than the ratio alone. A rule is also offered to the three merge tables only when two of its properties are longhands one of them names — 6,238 of the corpus's 8,034 candidate rules are not, which is 311,900 table-entry checks that no longer run. The union is built in the generator out of the very arrays it emits, so it cannot drift. * chore: fold the branch's changeset into one entry * fix(css): leave an An+B past the safe integer range as written `_minifyNth` read an unbounded digit run, so a `B` above `Number.MAX_SAFE_INTEGER` made `b += a` a fixed point and the normalization loop never exited. Guard both terms and normalize by modulo instead of by stepping. Also spell the two new words for cspell, keep the `PAIR_LONGHANDS` comment with its table, assert the omittable initial keyword is still one mdn-data states, and canonicalize a nested rule's selector list in the equivalence harness without rewriting text inside an attribute. * 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. * docs(css): trim two generator comments to the guide's length * perf(css): read a value's children where they lie rather than listing them A declaration's value is one component 92 times in 100 (38151 of 41387 on the Tailwind fixture), and the printer materialized the whole child list to read its length and first entry. `_valueComponents` and the four shorthand collapses now walk the child span off the node, so the list is built only where one is really needed — a custom property's text, a `unicode-range`, an attribute selector. Young-generation collections for one Tailwind minify drop from 173 to 165 under a fixed semi-space, and the emitted bytes are unchanged. * chore(css): undo an unrelated reformat and cover the omittable-keyword guard A stale Prettier reformatted eighteen template interpolations the branch never touched; Prettier 3 leaves interpolations alone, so `fmt:check` passed either way and the churn stayed in the diff. The guard that makes a spec change fail generation now takes its tables the way the other collectors do, so a test can drive it.
1 parent bc65f6b commit 4015bbd

16 files changed

Lines changed: 872 additions & 152 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Write CSS selector lists, An+B and more values in their shortest spelling.

‎cspell.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
"metacharacter",
245245
"metacharacters",
246246
"microdata",
247+
"microsyntax",
247248
"microtask",
248249
"microtasks",
249250
"middleware",
@@ -284,6 +285,7 @@
284285
"opencollective",
285286
"opensource",
286287
"opuuus",
288+
"omittable",
287289
"overridable",
288290
"overridables",
289291
"parallelism",

‎lib/css/data.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ const MERGE_LONGHANDS = new Set([
611611
"top"
612612
]);
613613

614+
// The initial keyword each of these may drop when another component stands
615+
// beside it: omitting the group it belongs to leaves exactly that keyword.
616+
const OMITTABLE_INITIAL_KEYWORDS = new Map([["grid-auto-flow", "row"]]);
617+
614618
// What each of those longhands accepts as a whole value: the keywords it names,
615619
// and the value classes it reaches. A value acceptable to a second slot is what
616620
// makes the merge ambiguous, and `FAMILY_SLOT_CLASSES` names a type the printer
@@ -879,6 +883,15 @@ const NTH_PSEUDO_FUNCTIONS = new Set([
879883
"nth-of-type"
880884
]);
881885

886+
// Each An+B pseudo-class whose one-element case has a name of its own:
887+
// `:nth-child(1)` is what `:first-child` selects, in fewer bytes.
888+
const NTH_NAMED_EQUIVALENTS = new Map([
889+
["nth-child", "first-child"],
890+
["nth-last-child", "last-child"],
891+
["nth-last-of-type", "last-of-type"],
892+
["nth-of-type", "first-of-type"]
893+
]);
894+
882895
// The properties taking a color and never an identifier of the author's own, so
883896
// a named color written in one is that color and may be spelled the shortest way.
884897
const COLOR_ONLY_PROPERTIES = new Set([
@@ -3195,7 +3208,9 @@ module.exports.MATH_FUNCTION_SUM_ARGUMENTS = MATH_FUNCTION_SUM_ARGUMENTS;
31953208
module.exports.MERGEABLE_AT_RULES = MERGEABLE_AT_RULES;
31963209
module.exports.MERGE_LONGHANDS = MERGE_LONGHANDS;
31973210
module.exports.NEGATIVE_ACCEPTING_PROPERTIES = NEGATIVE_ACCEPTING_PROPERTIES;
3211+
module.exports.NTH_NAMED_EQUIVALENTS = NTH_NAMED_EQUIVALENTS;
31983212
module.exports.NTH_PSEUDO_FUNCTIONS = NTH_PSEUDO_FUNCTIONS;
3213+
module.exports.OMITTABLE_INITIAL_KEYWORDS = OMITTABLE_INITIAL_KEYWORDS;
31993214
module.exports.ONE_VALUE_PAIR_SHORTHANDS = ONE_VALUE_PAIR_SHORTHANDS;
32003215
module.exports.PAIR_LONGHANDS = PAIR_LONGHANDS;
32013216
module.exports.POSITION_PROPERTIES = POSITION_PROPERTIES;

0 commit comments

Comments
 (0)