Skip to content

Commit fb399a2

Browse files
committed
Canonicalization: reuse dynamic utility lookups across candidate collapse groups
1 parent b10c862 commit fb399a2

2 files changed

Lines changed: 92 additions & 37 deletions

File tree

‎packages/tailwindcss/src/canonicalize-candidates.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,45 @@ describe('regressions', () => {
20342034
])
20352035
expect(compare).not.toHaveBeenCalled()
20362036
})
2037+
2038+
test('reuses dynamic utility lookups across candidate collapse groups', async () => {
2039+
let designSystem = await __unstable__loadDesignSystem(
2040+
css`
2041+
@tailwind utilities;
2042+
@theme {
2043+
--spacing: 0.25rem;
2044+
}
2045+
`,
2046+
{ base: __dirname },
2047+
)
2048+
2049+
using keysSpy = vi.spyOn(designSystem.utilities, 'keys')
2050+
2051+
let options: CanonicalizeOptions = {
2052+
collapse: true,
2053+
logicalToPhysical: true,
2054+
rem: 16,
2055+
}
2056+
2057+
expect(
2058+
designSystem.canonicalizeCandidates(
2059+
['mt-2', 'mb-2', 'hover:mt-2', 'hover:mb-2', 'focus:mt-2', 'focus:mb-2'],
2060+
options,
2061+
),
2062+
).toEqual(['my-2', 'hover:my-2', 'focus:my-2'])
2063+
2064+
let functionalKeysCalls = keysSpy.mock.calls.filter((args) => args[0] === 'functional').length
2065+
expect(functionalKeysCalls).toBeLessThanOrEqual(3)
2066+
2067+
// Re-running with repeated candidates across another variant group reuses the cached lookups
2068+
// without invoking `utilities.keys('functional')` again.
2069+
let countBefore = keysSpy.mock.calls.length
2070+
expect(designSystem.canonicalizeCandidates(['active:mt-2', 'active:mb-2'], options)).toEqual([
2071+
'active:my-2',
2072+
])
2073+
let countAfter = keysSpy.mock.calls.length
2074+
expect(countAfter).toBe(countBefore)
2075+
})
20372076
})
20382077

20392078
describe('intersection', () => {

‎packages/tailwindcss/src/canonicalize-candidates.ts‎

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ interface DesignSystem extends BaseDesignSystem {
106106
SignatureOptions,
107107
DefaultMap<string, DefaultMap<string, Set<string>>>
108108
>
109+
[DYNAMIC_UTILITIES_KEY]: DefaultMap<
110+
SignatureOptions,
111+
DefaultMap<string, DefaultMap<string, DefaultMap<string, Set<string>>>>
112+
>
109113
[PRE_COMPUTED_UTILITIES_KEY]: DefaultMap<SignatureOptions, DefaultMap<string, string[]>>
110114
[VARIANT_SIGNATURE_KEY]: DefaultMap<RemValue, DefaultMap<string, string | symbol>>
111115
[PRE_COMPUTED_VARIANTS_KEY]: DefaultMap<RemValue, DefaultMap<string, string[]>>
@@ -130,6 +134,7 @@ export function prepareDesignSystemStorage(
130134
createUtilityThemeVariablesCache(designSystem)
131135
designSystem.storage[STATIC_UTILITIES_KEY] ??= createStaticUtilitiesCache()
132136
designSystem.storage[UTILITY_PROPERTIES_KEY] ??= createUtilityPropertiesCache(designSystem)
137+
designSystem.storage[DYNAMIC_UTILITIES_KEY] ??= createDynamicUtilitiesCache(designSystem)
133138
designSystem.storage[PRE_COMPUTED_UTILITIES_KEY] ??= createPreComputedUtilitiesCache(designSystem)
134139
designSystem.storage[VARIANT_SIGNATURE_KEY] ??= createVariantSignatureCache(designSystem)
135140
designSystem.storage[PRE_COMPUTED_VARIANTS_KEY] ??= createPreComputedVariantsCache(designSystem)
@@ -276,6 +281,7 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st
276281
designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions)
277282
let staticUtilities = designSystem.storage[STATIC_UTILITIES_KEY].get(signatureOptions)
278283
let themeVariables = designSystem.storage[UTILITY_THEME_VARIABLES_KEY].get(signatureOptions)
284+
let dynamicUtilities = designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions)
279285

280286
// For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem`
281287
//
@@ -339,43 +345,6 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st
339345
}
340346
}
341347

342-
let dynamicUtilities = new DefaultMap((candidate: string) => {
343-
let result = new DefaultMap(
344-
(_property: string) => new DefaultMap((_value: string) => new Set<string>()),
345-
)
346-
347-
let relevantProperties = new Set(computeUtilitiesPropertiesLookup.get(candidate).keys())
348-
if (relevantProperties.size === 0) return result
349-
350-
for (let parsedCandidate of parseCandidate(designSystem, candidate)) {
351-
if (parsedCandidate.kind !== 'functional' || parsedCandidate.value === null) {
352-
continue
353-
}
354-
355-
for (let root of designSystem.utilities.keys('functional')) {
356-
if (root === parsedCandidate.root) continue // Skip self
357-
358-
let replacement = printUnprefixedCandidate(designSystem, {
359-
...cloneCandidate(parsedCandidate),
360-
root,
361-
})
362-
363-
let propertyValues = computeUtilitiesPropertiesLookup.get(replacement)
364-
for (let [property, values] of propertyValues) {
365-
if (!relevantProperties.has(property)) continue // Skip properties that are not relevant for the current candidate
366-
367-
for (let value of values) {
368-
result.get(property).get(value).add(replacement)
369-
}
370-
}
371-
}
372-
373-
return result
374-
}
375-
376-
return result
377-
})
378-
379348
// For each property, lookup other utilities that also set this property and
380349
// this exact value. If multiple properties are used, use the intersection of
381350
// each property.
@@ -2951,6 +2920,53 @@ function createUtilityPropertiesCache(
29512920
})
29522921
}
29532922

2923+
const DYNAMIC_UTILITIES_KEY = Symbol()
2924+
function createDynamicUtilitiesCache(
2925+
designSystem: DesignSystem,
2926+
): DesignSystem['storage'][typeof DYNAMIC_UTILITIES_KEY] {
2927+
return new DefaultMap((signatureOptions: SignatureOptions) => {
2928+
let computeUtilitiesPropertiesLookup =
2929+
designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions)
2930+
2931+
return new DefaultMap((candidate: string) => {
2932+
let result = new DefaultMap(
2933+
(_property: string) => new DefaultMap((_value: string) => new Set<string>()),
2934+
)
2935+
2936+
let relevantProperties = new Set(computeUtilitiesPropertiesLookup.get(candidate).keys())
2937+
if (relevantProperties.size === 0) return result
2938+
2939+
for (let parsedCandidate of parseCandidate(designSystem, candidate)) {
2940+
if (parsedCandidate.kind !== 'functional' || parsedCandidate.value === null) {
2941+
continue
2942+
}
2943+
2944+
for (let root of designSystem.utilities.keys('functional')) {
2945+
if (root === parsedCandidate.root) continue // Skip self
2946+
2947+
let replacement = printUnprefixedCandidate(designSystem, {
2948+
...cloneCandidate(parsedCandidate),
2949+
root,
2950+
})
2951+
2952+
let propertyValues = computeUtilitiesPropertiesLookup.get(replacement)
2953+
for (let [property, values] of propertyValues) {
2954+
if (!relevantProperties.has(property)) continue // Skip properties that are not relevant for the current candidate
2955+
2956+
for (let value of values) {
2957+
result.get(property).get(value).add(replacement)
2958+
}
2959+
}
2960+
}
2961+
2962+
return result
2963+
}
2964+
2965+
return result
2966+
})
2967+
})
2968+
}
2969+
29542970
// For all static utilities in the system, compute a lookup table that maps the
29552971
// utility signature to the utility name. This is used to find the utility name
29562972
// for a given utility signature.

0 commit comments

Comments
 (0)