Skip to content

Canonicalization: collapse static utilities regardless of earlier candidates - #20754

Open
Companion wants to merge 6 commits into
tailwindlabs:mainfrom
Companion:fix-collapse-static-utilities
Open

Companion wants to merge 6 commits into
tailwindlabs:mainfrom
Companion:fix-collapse-static-utilities

Conversation

@Companion

@Companion Companion commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

part of #20602

collapsing static utilities like overflow-x-hidden overflow-y-hidden into overflow-hidden only worked after some earlier candidate built the pre-computed utilities since that is what indexes static utilities by property and value
collapse now computes the properties of the static utilities first so the result no longer depends on what was canonicalized before
utilities and variants whose name starts with the prefix like o:outline-[2px] with prefix(o) now canonicalize too
the arbitrary line height cases from the issue comment are left out since they touch the same code as #20603

Test plan

added regression tests that collapse overflow-x-hidden overflow-y-hidden and overflow-x-clip overflow-y-clip and overscroll-x-none overscroll-y-none on a new design system which fail on main and pass now
pnpm vitest run src/canonicalize-candidates.test.ts in packages/tailwindcss passes

@Companion
Companion requested a review from a team as a code owner November 24, 2026 03:40
@RobinMalfait
RobinMalfait self-requested a review November 24, 2026 10:40
@RobinMalfait RobinMalfait self-assigned this Nov 24, 2026
A group with one candidate can never collapse, but it still pre-computed
all utilities (~750 ms with the default theme) to fill the static index.
E.g.: the first `flex hover:underline` took ~760 ms instead of ~6 ms.
Pre-computing all utilities to collapse a group made the first collapse of
a class list with only static utilities take ~800 ms with the default
theme, e.g. `flex items-center`, which took ~2 ms before.

A group of static utilities only needs the static utilities in the index,
e.g. `overflow-x-hidden overflow-y-hidden` → `overflow-hidden`. So compute
the properties of the 889 static utilities instead, once per design
system, which adds them to the index. This takes ~4 ms (~15 ms with a cold
JIT). With the default theme, no group of static utilities collapses into
a utility with a value.

Candidates with a value, like `p-4`, already pre-compute all utilities
while they are canonicalized, so groups with them work as before.
Collapsing a group of static utilities first computes the properties of
all static utilities, so that the result does not depend on the
candidates that were canonicalized before. That costs ~14ms on a fresh
design system, but it isn't always needed:

- A group with fewer than 2 utilities can't be collapsed, e.g.
  `group flex`, where `group` isn't a utility. Return early, before
  computing the static utilities (14ms → 1ms).
- When all utilities were pre-computed already (for a candidate with a
  value like `p-4`), the static utilities are in the index already. With
  a prefix, we computed all of them a second time under their prefixed
  name (3-6ms → 0ms).

The static pass now also skips the static utilities that the class list
skips, like the deprecated `bg-gradient-to-r`. These are not computed
when pre-computing all utilities either, so the index is the same in
both orders, and a collapse never picks a deprecated name.
@RobinMalfait

RobinMalfait commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

I pushed three small commits on top of this PR, to bring the cost of the fix down to a few ms:

  • Collapse now computes the properties of the static utilities only, instead of pre-computing all utilities. The static index needs these for pairs like overflow-x-hidden overflow-y-hidden. It takes ~14 ms once per design system, instead of ~740 ms. Candidates with a value, like p-4, already pre-compute all utilities while they are canonicalized, so groups with them work as before.
  • A group that can't collapse skips this step. Only utilities with the same variants can collapse, and a group needs at least two of them: group flex has only one utility (group isn't one), and flex hover:underline has one utility without a variant and one with hover:.
  • The step is also skipped once all utilities were pre-computed, and it ignores the deprecated aliases that the class list leaves out, like bg-gradient-to-r.

First call on a fresh design system with the default theme, median of 7 runs:

First class list main this PR before my commits this PR with my commits
overflow-x-hidden overflow-y-hidden 2 ms, not collapsed 734 ms 15 ms, collapsed
flex items-center 2 ms 735 ms 14 ms
group flex 1 ms 740 ms 1 ms
flex hover:underline 6 ms 738 ms 5 ms
flex p-4 742 ms 738 ms 739 ms
example.html (194 class lists) 920 ms 878 ms 916 ms

The output is the same as before my commits. The tests also cover a prefix, the result after a candidate with a value, and that these groups don't read the full class list.

Sent by an AI assistant on behalf of @RobinMalfait.

A prefix like `o` is also the start of `overflow-hidden`, so the check
`startsWith(prefix)` skipped adding the prefix. Pre-computing all
utilities then indexed `overflow-hidden` instead of `o:overflow-hidden`,
and a later `o:overflow-x-hidden o:overflow-y-hidden` didn't collapse.
@Companion

Copy link
Copy Markdown
Author

thanks this holds up
i checked it on a fresh design system against pre-computing all utilities
604 of the 902 utilities without a value already pre-compute all utilities on their own like w-full mx-auto and inline-flex
for the other 298 all 44253 pairs and 4366296 triples collapse the same with only the static utilities

the variant signature cache had the same prefix check so with prefix(f) f:[&:focus]:flex stayed as is instead of becoming f:focus:flex
i pushed the same fix for variants with a test and added a changelog line for both prefix fixes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants