Performance: reuse dynamic utility lookups across canonicalizeCandidates collapse groups
Summary
In Tailwind CSS 4.3.3, canonicalizeCandidates(..., { collapse: true, logicalToPhysical: true }) repeatedly builds the same dynamic-utility alternatives when called for many class groups in a lint run.
A small experimental cache scoped to the design-system object + SignatureOptions object + candidate string reduced a canonical-rule-only lint workload from 12.3s to 10.7s, and 12.1s to 10.5s, in sequential A/B/A/B runs. This is a modest application-specific improvement, not a claim of a general 2x speedup or an end-to-end build improvement.
Source hotspot
Tagged source: https://github.com/tailwindlabs/tailwindcss/blob/v4.3.3/packages/tailwindcss/src/canonicalize-candidates.ts#L339-L375
Inside collapseCandidates → collapseGroup, a fresh dynamicUtilities = new DefaultMap(...) is created for every group. Its factory:
- Looks up the candidate's relevant properties.
- Iterates the functional utility roots.
- Compiles/looks up replacements with the same value/modifier and builds property/value → replacement sets.
The underlying utility-property lookups are cached, but rebuilding and iterating this alternatives map is repeated. The factory appears to depend on the design system, signature options, and candidate—not the other candidates in the current group.
Measurement setup
- Tailwind CSS 4.3.3; eslint-plugin-better-tailwindcss 4.9.1; Oxlint 1.87.0.
- Linux containers under Dagger, Node 26.12.1-slim.
- 769 web files. Native syntax lint remained enabled; only the Tailwind canonical-class rule was enabled among the Tailwind rules. Type-aware lint was disabled for this experiment.
- Default canonical rule options: collapse=true, logicalToPhysical=true, rem unspecified.
- Fresh Node/linter processes, same application source and configuration, sequential baseline/candidate/baseline/candidate in one container invocation.
- Both variants used the same instrumentation, including cumulative high-resolution timers and thread CPU counters. Absolute timings include instrumentation overhead and are not a rigorous general-purpose benchmark.
- The application corpus is not a standalone public reproducer; the source hotspot and regression examples below are the actionable parts of this report.
| Measurement | Baseline 1 | Candidate 1 | Baseline 2 | Candidate 2 |
|---|---|---|---|---|
| Oxlint-reported whole lint wall time | 12.3s | 10.7s | 12.1s | 10.5s |
| Canonical worker total, wall | 9.449s | 7.939s | 9.252s | 7.825s |
| canonicalizeCandidates calls, wall | 9.011s | 7.511s | 8.842s | 7.429s |
| canonicalizeCandidates calls, thread user+system CPU | 6.628s | 6.052s | 6.605s | 5.900s |
| Dynamic-alternatives factory evaluations | 5,937 | 905 | 5,938 | 905 |
| Dynamic factory, wall | 3.759s | 2.236s | 3.545s | 2.285s |
| Dynamic factory, thread user+system CPU | 2.137s | 1.622s | 1.983s | 1.615s |
All four full-corpus runs produced zero warnings/errors. Worker group counts varied slightly (~1,423–1,443) because the caller has a shared LRU and parallel file scheduling changes eviction order.
Additional attribution from an instrumented baseline:
- CSS/design-system loading was ~79ms, once—not the multi-second bottleneck.
- The first lazy canonicalization call took ~3.0s. That initialization cost remains with the candidate.
- Per-candidate canonicalization/cache lookup total was ~4.17s wall; full group collapsing accounted for another ~4.48s.
- The caller made 2,468 operation-cache lookups, with ~1,024–1,039 hits and only 14–29 eviction misses in representative runs. This was not mainly a too-small caller cache.
- Prefix IPC was measured separately (~0.4s wall in that baseline). The candidate does not optimize prefix handling.
Minimal optimization sketch
Add one storage slot to the canonicalization DesignSystem interface:
[DYNAMIC_UTILITIES_KEY]: WeakMap<
SignatureOptions,
DefaultMap<string, DefaultMap<string, DefaultMap<string, Set<string>>>>
>
Then reuse the existing factory instead of changing its algorithm:
const DYNAMIC_UTILITIES_KEY = Symbol()
// Inside collapseGroup, after obtaining signatureOptions and the existing lookups:
const cache = (designSystem.storage[DYNAMIC_UTILITIES_KEY] ??= new WeakMap())
let dynamicUtilities = cache.get(signatureOptions)
if (!dynamicUtilities) {
dynamicUtilities = new DefaultMap(/* existing candidate factory, unchanged */)
cache.set(signatureOptions, dynamicUtilities)
}
// Keep the existing property intersections, linked candidates, combinations,
// signature equivalence tests, and complete group collapse algorithm unchanged.
The benchmark candidate made the equivalent narrow change to the installed ESM distribution in a temporary container. No application patch or rule removal is proposed.
Semantics and cache lifetime
This is not a per-class canonicalization shortcut. Two individually canonical classes can still collapse together (e.g. px-2 + py-2 → p-2), and every complete group still goes through the existing collapse algorithm.
- Cache separation follows the same SignatureOptions identity as the existing property/signature caches, preserving distinctions such as rem, property expansion, and logical-to-physical handling.
- Cache storage belongs to the actual design-system object. Replacing/reloading that object yields an empty cache, just like the existing internal canonicalization caches.
- This does not introduce a new process-global stylesheet cache, and does not claim to fix the caller's existing stylesheet dependency invalidation. Persistent-editor/CSS reload behavior should receive upstream tests before adopting it.
- The new map retains alternatives until the design system is discarded; memory impact has not been quantified.
Diagnostic equivalence checked
I compared the actual linter's complete JSON diagnostics before/after the candidate for fixtures containing:
<div className="w-[1rem]" />
<div className="px-2 py-2" />
<div className="sm:px-2 sm:py-2" />
<div className="px-2! py-2!" />
<div className="flex-grow" />
<div className="ps-2 pe-2" />
<div className={"px-2 py-2 " + suffix} />
Both variants emitted the same 10 diagnostics, including single-candidate w-[1rem] → w-4 and flex-grow → grow, two-input collapse, variant/important handling, and concatenated-literal handling. The entire test was repeated with a real prefix(tw) stylesheet and tw:-prefixed classes: again 10 identical diagnostics. JSON equality included messages, severities, file names and source spans. Autofix output and the full Tailwind upstream test suite were not tested.
Would a design-system-scoped cache for this particular lookup be appropriate upstream? The observed gain is useful but limited (~1.6s/~13% in this lint workload); the remaining lazy initialization and other canonicalization costs are deliberately not presented as solved.
Hi! I'd like to take a look at implementing and testing this optimization.
I'll start by setting up the benchmark environment locally, applying the proposed design-system scoped cache, and running the full upstream test suite to ensure there are no regressions or memory side effects. I'll follow up with a PR once verified.