Skip to content

Commit d566dbd

Browse files
authored
Improve performance of rebuilds (#16283)
This PR introduces a performance improvement we noticed while working on on: #16211 We noticed that `substituteFunctions` was being called on every `node` after the `compileAstNodes` was done. However, the `compileAstNodes` is heavily cached. By moving the `substituteFunctions` we into the cached `compileAstNodes` we sped up performance for Catalyst rebuilds from ~15ms to ~10ms. | Before | After | | --- | --- | | <img width="710" alt="image" src="https://github.com/user-attachments/assets/eaf110d9-2f88-447c-9b10-c77d47bd99a5" /> | <img width="696" alt="image" src="https://github.com/user-attachments/assets/c5a2ff4c-d75e-4e35-a2b6-d896598810f5" /> |
1 parent 837e240 commit d566dbd

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

packages/tailwindcss/src/compile.ts

-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type StyleRule,
1010
} from './ast'
1111
import { type Candidate, type Variant } from './candidate'
12-
import { substituteFunctions } from './css-functions'
1312
import { type DesignSystem } from './design-system'
1413
import GLOBAL_PROPERTY_ORDER from './property-order'
1514
import { asColor, type Utility } from './utilities'
@@ -55,22 +54,6 @@ export function compileCandidates(
5554
let rules = designSystem.compileAstNodes(candidate)
5655
if (rules.length === 0) continue
5756

58-
// Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary
59-
// properties (`[--my-var:theme(--color-red-500)]`) can contain function
60-
// calls so we need evaluate any functions we find there that weren't in
61-
// the source CSS.
62-
try {
63-
substituteFunctions(
64-
rules.map(({ node }) => node),
65-
designSystem,
66-
)
67-
} catch (err) {
68-
// If substitution fails then the candidate likely contains a call to
69-
// `theme()` that is invalid which may be because of incorrect usage,
70-
// invalid arguments, or a theme key that does not exist.
71-
continue
72-
}
73-
7457
found = true
7558

7659
for (let { node, propertySort } of rules) {

packages/tailwindcss/src/design-system.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { optimizeAst, toCss } from './ast'
22
import { parseCandidate, parseVariant, type Candidate, type Variant } from './candidate'
33
import { compileAstNodes, compileCandidates } from './compile'
4+
import { substituteFunctions } from './css-functions'
45
import { getClassList, getVariants, type ClassEntry, type VariantEntry } from './intellisense'
56
import { getClassOrder } from './sort'
67
import type { Theme, ThemeKey } from './theme'
@@ -41,9 +42,27 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
4142
let parsedCandidates = new DefaultMap((candidate) =>
4243
Array.from(parseCandidate(candidate, designSystem)),
4344
)
44-
let compiledAstNodes = new DefaultMap<Candidate>((candidate) =>
45-
compileAstNodes(candidate, designSystem),
46-
)
45+
let compiledAstNodes = new DefaultMap<Candidate>((candidate) => {
46+
let ast = compileAstNodes(candidate, designSystem)
47+
48+
// Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary
49+
// properties (`[--my-var:theme(--color-red-500)]`) can contain function
50+
// calls so we need evaluate any functions we find there that weren't in
51+
// the source CSS.
52+
try {
53+
substituteFunctions(
54+
ast.map(({ node }) => node),
55+
designSystem,
56+
)
57+
} catch (err) {
58+
// If substitution fails then the candidate likely contains a call to
59+
// `theme()` that is invalid which may be because of incorrect usage,
60+
// invalid arguments, or a theme key that does not exist.
61+
return []
62+
}
63+
64+
return ast
65+
})
4766

4867
let designSystem: DesignSystem = {
4968
theme,

0 commit comments

Comments
 (0)