Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/single-candidate-variant-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Canonicalization: improve performance in projects with many functional variant values (e.g. a `matchVariant` plugin with thousands of values)
34 changes: 33 additions & 1 deletion packages/tailwindcss/src/canonicalize-candidates.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomUUID } from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import { __unstable__loadDesignSystem } from '.'
import { intersection } from './canonicalize-candidates'
import { cartesian } from './cartesian'
Expand Down Expand Up @@ -2002,6 +2002,38 @@ describe('regressions', () => {
),
).toEqual(['link', 'mt-4'])
})

// Canonicalization compiles every known variant on its own, so sorting all the
// parsed variants each time made it quadratic in the number of known variants.
test('does not sort the parsed variants when compiling a single candidate', async () => {
let designSystem = await __unstable__loadDesignSystem(
css`
@tailwind utilities;
@plugin "my-plugin";
`,
{
async loadModule() {
return {
path: '',
base: '',
module: plugin(({ matchVariant }) => {
let values = Object.fromEntries(
Array.from({ length: 250 }, (_, i) => [`value-${i}`, `.value-${i}`]),
)
matchVariant('foo', (value) => `&:has(${value})`, { values })
}),
}
},
},
)

using compare = vi.spyOn(designSystem.variants, 'compare')

expect(designSystem.canonicalizeCandidates(['foo-[.value-42]:flex'])).toEqual([
'foo-value-42:flex',
])
expect(compare).not.toHaveBeenCalled()
})
})

describe('intersection', () => {
Expand Down
12 changes: 9 additions & 3 deletions packages/tailwindcss/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export function compileCandidates(
flags |= CompileAstFlags.RespectImportant
}

let variantOrderMap = designSystem.getVariantOrder()
// The variant order is only used to sort the rules of different candidates, so
// a single candidate doesn't need it. Sorting all the parsed variants is
// expensive, and canonicalization compiles every known variant on its own, each
// of which adds a parsed variant that would invalidate the previous order.
let variantOrderMap = matches.size > 1 ? designSystem.getVariantOrder() : null

// Create the AST
for (let [rawCandidate, candidates] of matches) {
Expand All @@ -62,8 +66,10 @@ export function compileCandidates(
// variant. This allows us to sort the rules based on the order of
// variants used.
let variantOrder = 0n
for (let variant of candidate.variants) {
variantOrder |= 1n << BigInt(variantOrderMap.get(variant)!)
if (variantOrderMap !== null) {
for (let variant of candidate.variants) {
variantOrder |= 1n << BigInt(variantOrderMap.get(variant)!)
}
}

nodeSorting.set(node, {
Expand Down
Loading