镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit a81d4f2

Browse files
committed
perf: memoize utility cascade ranking per selector
toCSS re-ranks every rule on every call, and ranking ran three regexes per rule — a watching build repeated that work over the same selectors on each rebuild. Ranking is a pure function of the selector, so cache it per generator and clear it alongside selectorCache on reset. The regexes are hoisted to module scope instead of being recompiled per call.
1 parent af5da84 commit a81d4f2

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

‎packages/crosswind/src/generator.ts‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,33 @@ function processConfig(config: CrosswindConfig): ProcessedConfig {
15981598
/**
15991599
* Generates CSS rules from parsed utility classes
16001600
*/
1601+
/**
1602+
* Selector → cascade rank.
1603+
*
1604+
* 0 — shorthand (`m-*`, `p-*`, `border`, `rounded`, `inset-*`)
1605+
* 1 — axis (`mx-*`, `my-*`, `px-*`, `py-*`, `inset-x-*`, corner radii)
1606+
* 2 — side (`mt-*`, `mr-*`, `pt-*`, `top-*`, `border-t-*`, …)
1607+
*/
1608+
const SELECTOR_CLASS_RE = /\.(?:\\!)?(?:[a-zA-Z0-9_-]*\\?:)*(?:\\!)?([a-zA-Z0-9_-]+)/
1609+
const AXIS_UTILITY_RE = /^(?:mx|my|px|py|inset-x|inset-y|border-x|border-y|scroll-mx|scroll-my|scroll-px|scroll-py|space-x|space-y|gap-x|gap-y)-/
1610+
const AXIS_RADIUS_RE = /^rounded-[tlbr](?:-|$)/
1611+
const SIDE_UTILITY_RE = /^(?:mt|mr|mb|ml|pt|pr|pb|pl|top|right|bottom|left|border-t|border-r|border-b|border-l|scroll-mt|scroll-mr|scroll-mb|scroll-ml|scroll-pt|scroll-pr|scroll-pb|scroll-pl)-/
1612+
const SIDE_RADIUS_RE = /^rounded-(?:tl|tr|bl|br)(?:-|$)/
1613+
1614+
function computeUtilityRank(selector: string): number {
1615+
// The escaped important marker (`.\!m-0`) must be skipped or the class
1616+
// match fails and every !-utility ranked 0, letting `!m-0` emit after
1617+
// (and defeat) `!mx-auto`.
1618+
const m = SELECTOR_CLASS_RE.exec(selector)
1619+
if (!m) return 0
1620+
const cls = m[1]
1621+
if (AXIS_UTILITY_RE.test(cls) || AXIS_RADIUS_RE.test(cls))
1622+
return 1
1623+
if (SIDE_UTILITY_RE.test(cls) || SIDE_RADIUS_RE.test(cls))
1624+
return 2
1625+
return 0
1626+
}
1627+
16011628
export class CSSGenerator {
16021629
private rules: Map<string, CSSRule[]> = new Map()
16031630
private classCache: Set<string> = new Set()
@@ -1609,6 +1636,7 @@ export class CSSGenerator {
16091636
private noMatchCache: Set<string> = new Set()
16101637
private usedKeyframes: Set<string> = new Set()
16111638
private variantHandledCache: Map<string, boolean> = new Map()
1639+
private utilityRankCache: Map<string, number> = new Map()
16121640
private compiledGenerated: Set<string> = new Set()
16131641
private darkStrategy: 'class' | 'media'
16141642
// Fast-path lookup tables, re-based on the user's theme when it diverges
@@ -2797,26 +2825,18 @@ export class CSSGenerator {
27972825
* combinations like `m-0 mx-auto` silently break (the shorthand resets
27982826
* the auto margins). Matches Tailwind's own stylesheet ordering.
27992827
*
2800-
* 0 — shorthand (`m-*`, `p-*`, `border`, `rounded`, `inset-*`)
2801-
* 1 — axis (`mx-*`, `my-*`, `px-*`, `py-*`, `inset-x-*`, corner radii)
2802-
* 2 — side (`mt-*`, `mr-*`, `pt-*`, `top-*`, `border-t-*`, …)
2828+
* See computeUtilityRank for the ranks themselves.
28032829
*/
28042830
private getUtilityRank(selector: string): number {
2805-
// The escaped important marker (`.\!m-0`) must be skipped or the class
2806-
// match fails and every !-utility ranked 0, letting `!m-0` emit after
2807-
// (and defeat) `!mx-auto`.
2808-
const m = selector.match(/\.(?:\\!)?(?:[a-zA-Z0-9_-]*\\?:)*(?:\\!)?([a-zA-Z0-9_-]+)/)
2809-
if (!m) return 0
2810-
const cls = m[1]
2811-
if (/^(?:mx|my|px|py|inset-x|inset-y|border-x|border-y|scroll-mx|scroll-my|scroll-px|scroll-py|space-x|space-y|gap-x|gap-y)-/.test(cls)
2812-
|| /^rounded-(?:[tlbr])(?:-|$)/.test(cls)) {
2813-
return 1
2814-
}
2815-
if (/^(?:mt|mr|mb|ml|pt|pr|pb|pl|top|right|bottom|left|border-t|border-r|border-b|border-l|scroll-mt|scroll-mr|scroll-mb|scroll-ml|scroll-pt|scroll-pr|scroll-pb|scroll-pl)-/.test(cls)
2816-
|| /^rounded-(?:tl|tr|bl|br)(?:-|$)/.test(cls)) {
2817-
return 2
2818-
}
2819-
return 0
2831+
// Ranking is a pure function of the selector, and toCSS re-ranks every
2832+
// rule on every call — a watching build pays for the same three regexes
2833+
// over the same selectors on each rebuild. Memoize per generator.
2834+
const cached = this.utilityRankCache.get(selector)
2835+
if (cached !== undefined)
2836+
return cached
2837+
const rank = computeUtilityRank(selector)
2838+
this.utilityRankCache.set(selector, rank)
2839+
return rank
28202840
}
28212841

28222842
/**
@@ -2937,6 +2957,9 @@ export class CSSGenerator {
29372957
this.noMatchCache.clear()
29382958
this.usedKeyframes.clear()
29392959
this.compiledGenerated.clear()
2960+
// Keyed by selector, so it tracks the same set of classes selectorCache
2961+
// does and would otherwise accumulate across a watch session's rebuilds.
2962+
this.utilityRankCache.clear()
29402963
// variantHandledCache is config-derived, not generation state — it
29412964
// stays valid across resets.
29422965
}

0 commit comments

Comments
 (0)