From b6069c16fafe111bb1acf6106a9bff7190a9daf6 Mon Sep 17 00:00:00 2001 From: rudy-07 Date: Tue, 17 Nov 2026 18:49:44 +0530 Subject: [PATCH 1/2] Canonicalization: reuse dynamic utility lookups across candidate collapse groups # Conflicts: # packages/tailwindcss/src/canonicalize-candidates.test.ts --- .../src/canonicalize-candidates.test.ts | 46 ++++++++++ .../src/canonicalize-candidates.ts | 90 +++++++++++-------- 2 files changed, 99 insertions(+), 37 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 85dc07c40a94..9bdbe825762d 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2230,6 +2230,52 @@ describe('regressions', () => { designSystem.canonicalizeCandidates(['[@scope_(.foo\\2b_.bar)_to_(.limit)]:flex']), ).toEqual(['in-foo-hex-plus-bar:flex']) }) + + test('reuses dynamic utility lookups across candidate collapse groups', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + @theme { + --spacing: 0.25rem; + } + `, + { base: __dirname }, + ) + + let options: CanonicalizeOptions = { + collapse: true, + logicalToPhysical: true, + rem: 16, + } + + // Warm the pre-computed utilities, which walk the functional utilities + // once. A single candidate doesn't collapse, so this doesn't compute any + // dynamic utility lookups yet. + designSystem.canonicalizeCandidates(['mt-2'], options) + + using keysSpy = vi.spyOn(designSystem.utilities, 'keys') + + expect( + designSystem.canonicalizeCandidates( + ['mt-2', 'mb-2', 'hover:mt-2', 'hover:mb-2', 'focus:mt-2', 'focus:mb-2'], + options, + ), + ).toEqual(['my-2', 'hover:my-2', 'focus:my-2']) + + // One walk for each distinct candidate (`mt-2` and `mb-2`), not one for each + // variant group. + let functionalKeysCalls = keysSpy.mock.calls.filter((args) => args[0] === 'functional').length + expect(functionalKeysCalls).toBe(2) + + // Re-running with repeated candidates across another variant group reuses the cached lookups + // without invoking `utilities.keys('functional')` again. + let countBefore = keysSpy.mock.calls.length + expect(designSystem.canonicalizeCandidates(['active:mt-2', 'active:mb-2'], options)).toEqual([ + 'active:my-2', + ]) + let countAfter = keysSpy.mock.calls.length + expect(countAfter).toBe(countBefore) + }) }) describe('normalizeEscapes', () => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index ba760a878d4b..c14960c484d0 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -106,6 +106,10 @@ interface DesignSystem extends BaseDesignSystem { SignatureOptions, DefaultMap>> > + [DYNAMIC_UTILITIES_KEY]: DefaultMap< + SignatureOptions, + DefaultMap>>> + > [PRE_COMPUTED_UTILITIES_KEY]: DefaultMap> [VARIANT_SIGNATURE_KEY]: DefaultMap> [PRE_COMPUTED_VARIANTS_KEY]: DefaultMap> @@ -130,6 +134,7 @@ export function prepareDesignSystemStorage( createUtilityThemeVariablesCache(designSystem) designSystem.storage[STATIC_UTILITIES_KEY] ??= createStaticUtilitiesCache() designSystem.storage[UTILITY_PROPERTIES_KEY] ??= createUtilityPropertiesCache(designSystem) + designSystem.storage[DYNAMIC_UTILITIES_KEY] ??= createDynamicUtilitiesCache(designSystem) designSystem.storage[PRE_COMPUTED_UTILITIES_KEY] ??= createPreComputedUtilitiesCache(designSystem) designSystem.storage[VARIANT_SIGNATURE_KEY] ??= createVariantSignatureCache(designSystem) designSystem.storage[PRE_COMPUTED_VARIANTS_KEY] ??= createPreComputedVariantsCache(designSystem) @@ -276,6 +281,7 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions) let staticUtilities = designSystem.storage[STATIC_UTILITIES_KEY].get(signatureOptions) let themeVariables = designSystem.storage[UTILITY_THEME_VARIABLES_KEY].get(signatureOptions) + let dynamicUtilities = designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions) // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem` // @@ -339,43 +345,6 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st } } - let dynamicUtilities = new DefaultMap((candidate: string) => { - let result = new DefaultMap( - (_property: string) => new DefaultMap((_value: string) => new Set()), - ) - - let relevantProperties = new Set(computeUtilitiesPropertiesLookup.get(candidate).keys()) - if (relevantProperties.size === 0) return result - - for (let parsedCandidate of parseCandidate(designSystem, candidate)) { - if (parsedCandidate.kind !== 'functional' || parsedCandidate.value === null) { - continue - } - - for (let root of designSystem.utilities.keys('functional')) { - if (root === parsedCandidate.root) continue // Skip self - - let replacement = printUnprefixedCandidate(designSystem, { - ...cloneCandidate(parsedCandidate), - root, - }) - - let propertyValues = computeUtilitiesPropertiesLookup.get(replacement) - for (let [property, values] of propertyValues) { - if (!relevantProperties.has(property)) continue // Skip properties that are not relevant for the current candidate - - for (let value of values) { - result.get(property).get(value).add(replacement) - } - } - } - - return result - } - - return result - }) - // For each property, lookup other utilities that also set this property and // this exact value. If multiple properties are used, use the intersection of // each property. @@ -3315,6 +3284,53 @@ function createUtilityPropertiesCache( }) } +const DYNAMIC_UTILITIES_KEY = Symbol() +function createDynamicUtilitiesCache( + designSystem: DesignSystem, +): DesignSystem['storage'][typeof DYNAMIC_UTILITIES_KEY] { + return new DefaultMap((signatureOptions: SignatureOptions) => { + let computeUtilitiesPropertiesLookup = + designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions) + + return new DefaultMap((candidate: string) => { + let result = new DefaultMap( + (_property: string) => new DefaultMap((_value: string) => new Set()), + ) + + let relevantProperties = new Set(computeUtilitiesPropertiesLookup.get(candidate).keys()) + if (relevantProperties.size === 0) return result + + for (let parsedCandidate of parseCandidate(designSystem, candidate)) { + if (parsedCandidate.kind !== 'functional' || parsedCandidate.value === null) { + continue + } + + for (let root of designSystem.utilities.keys('functional')) { + if (root === parsedCandidate.root) continue // Skip self + + let replacement = printUnprefixedCandidate(designSystem, { + ...cloneCandidate(parsedCandidate), + root, + }) + + let propertyValues = computeUtilitiesPropertiesLookup.get(replacement) + for (let [property, values] of propertyValues) { + if (!relevantProperties.has(property)) continue // Skip properties that are not relevant for the current candidate + + for (let value of values) { + result.get(property).get(value).add(replacement) + } + } + } + + return result + } + + return result + }) + }) +} + // For all static utilities in the system, compute a lookup table that maps the // utility signature to the utility name. This is used to find the utility name // for a given utility signature. From 470a106e2561e6750a47edc81f25b3bedfa24d8f Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 16 Nov 2026 23:09:00 +0100 Subject: [PATCH 2/2] Add a changelog entry --- .changelog/canonicalize-reuse-collapse-lookups.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/canonicalize-reuse-collapse-lookups.md diff --git a/.changelog/canonicalize-reuse-collapse-lookups.md b/.changelog/canonicalize-reuse-collapse-lookups.md new file mode 100644 index 000000000000..e8439a61d3f7 --- /dev/null +++ b/.changelog/canonicalize-reuse-collapse-lookups.md @@ -0,0 +1,3 @@ +### Fixed + +- Canonicalization: improve performance of collapsing utilities (e.g. `mt-2 mb-2` → `my-2`) when the same utilities appear in many class lists or variants