Skip to content

Canonicalization: reuse dynamic utility lookups across candidate collapse groups - #20698

Merged
RobinMalfait merged 2 commits into
tailwindlabs:mainfrom
rudy-07:fix-canonicalize-dynamic-utilities-cache
Nov 20, 2026
Merged

RobinMalfait merged 2 commits into
tailwindlabs:mainfrom
rudy-07:fix-canonicalize-dynamic-utilities-cache

Conversation

@rudy-07

@rudy-07 rudy-07 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #20634

Summary

In canonicalizeCandidates, collapseGroup previously initialized a new dynamicUtilities map for each variant group being collapsed. For inputs containing multiple candidate groups, this caused redundant lookups across functional utility roots for identical candidates.

This change caches the dynamic utility lookups on designSystem.storage keyed by signatureOptions, matching the pattern used by UTILITY_PROPERTIES_KEY and STATIC_UTILITIES_KEY.

Test plan

  • Ran the test suite via pnpm exec vitest run --project tailwindcss packages/tailwindcss/src/canonicalize-candidates.test.ts (1,898 passed).
  • Added a test case verifying that candidate collapsing across multiple variant groups produces identical results while reusing lookups.
  • Verified code style via pnpm prettier --check packages/tailwindcss/src/canonicalize-candidates.ts packages/tailwindcss/src/canonicalize-candidates.test.ts.

[ci-all]

@rudy-07
rudy-07 requested a review from a team as a code owner November 10, 2026 18:21
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no actionable issues were found.

Reviews (9) · Last reviewed commit: "Add a changelog entry" · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The design system now caches dynamic utility matches by signature options and candidate. During collapsing, the code uses those matches, generated by checking other functional roots and recording relevant property/value matches. A regression test checks that margin candidates collapse across base, hover, and focus variants, and that later collapse work reuses utility-key lookups.

Merge Risk: Minimal · previous review 4f6bf

This change reuses dynamic utility lookups when candidates are collapsed, which improves canonicalization performance. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: dynamic utility lookups are reused across candidate collapse groups in canonicalization. The raw summary confirms that the lookup moved to DesignSystem.storage and col…
Description check Passed The description states the problem (collapseGroup rebuilding dynamicUtilities per variant group), the fix (caching lookups on designSystem.storage keyed by signatureOptions), and a test plan. These ma…
Linked Issues check Passed Issue #20634 asks that dynamic-utility alternatives be reused across collapse groups, scoped to the design system, SignatureOptions, and candidate string. At the reviewed head (4f6bfe3), canonicalize-…
Out of Scope Changes check Passed The whole-PR change set, per the supplied summary, touches canonicalize-candidates.ts, canonicalize-candidates.test.ts, and a changelog entry. The source change moves the dynamic-utilities factory int…
Full details: Title check

Explanation

The title names the main change: dynamic utility lookups are reused across candidate collapse groups in canonicalization. The raw summary confirms that the lookup moved to DesignSystem.storage and collapseGroup reads the cached lookup. The title is 80 characters, which is long, but it stays specific and accurate.

Full details: Description check

Explanation

The description states the problem (collapseGroup rebuilding dynamicUtilities per variant group), the fix (caching lookups on designSystem.storage keyed by signatureOptions), and a test plan. These match the changes in canonicalize-candidates.ts and its test file.

Full details: Linked Issues check

Explanation

Issue #20634 asks that dynamic-utility alternatives be reused across collapse groups, scoped to the design system, SignatureOptions, and candidate string. At the reviewed head (4f6bfe3), canonicalize-candidates.ts declares DYNAMIC_UTILITIES_KEY in DesignSystem.storage as a DefaultMap keyed by SignatureOptions and then by candidate. prepareDesignSystemStorage initializes it with createDynamicUtilitiesCache(designSystem). collapseGroup reads it once per signature group through designSystem.storage[DYNAMIC_UTILITIES_KEY].get(signatureOptions). The cache lives on the design-system object, so it is not process-global. The change does not alter the property intersection, linked-candidate, combination, or signature-equivalence logic. The added test in canonicalize-candidates.test.ts covers reuse across variant groups. The issue's persistent-editor, reload, and autofix validation notes are validation recommendations, not coding requirements of this PR. Verification was limited to the head file read; the changed-blob diff was unavailable in this checkout, so the prior whole-PR diff result is carried forward and consistent with the current file content.

Full details: Out of Scope Changes check

Explanation

The whole-PR change set, per the supplied summary, touches canonicalize-candidates.ts, canonicalize-candidates.test.ts, and a changelog entry. The source change moves the dynamic-utilities factory into a storage-backed cache initialized in prepareDesignSystemStorage and read in collapseGroup. The test covers reuse of that cache. The changelog entry describes the same performance fix. The head file read shows no prefix-handling, stylesheet-invalidation, or process-global cache changes in the inspected sections. The full diff could not be read in this checkout, so unchanged regions outside the inspected sections rely on the supplied summary.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 00041f50-de2b-4b10-9e30-8160813917a1
Commits

Reviewing files that changed from the base of the PR and between 648ab6b and 4f6bfe3.

Files selected for processing: 1

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

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comments: 1

packages/tailwindcss/src/canonicalize-candidates.test.ts: 1

2038-2055: Trivial · Maintainability & Code Quality · Low value

The test checks output only. It does not check that lookups are reused.

The test name claims reuse of dynamic utility lookups. The assertion verifies only the collapsed result. A regression that stops caching would still pass.

Optionally, assert on the cache directly, for example by checking that designSystem.storage holds the dynamic cache entry for the signature options after the call. Alternatively, rename the test to describe the behavior it verifies.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 93efce6d-8837-4fcb-80a6-5cdd6f4471ad
Commits

Reviewing files that changed from the base of the PR and between 1615f72 and 32275ac.

Files selected for processing: 2

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

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@rudy-07
rudy-07 force-pushed the fix-canonicalize-dynamic-utilities-cache branch from 32275ac to fb399a2 Compare November 10, 2026 20:14

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comments: 1

packages/tailwindcss/src/canonicalize-candidates.test.ts: 1

2064-2065: Trivial · Maintainability & Code Quality · Quick win

Require the first collapse to exercise dynamic lookups.

The precomputed lookup can supply my-2 without dynamic enumeration. It also calls keys('functional'), so a positive count alone is not sufficient. Warm the precomputed lookup before starting the spy, then require a positive count.

Suggested fix

-    using keysSpy = vi.spyOn(designSystem.utilities, 'keys')
-
     let options: CanonicalizeOptions = {
       collapse: true,
       logicalToPhysical: true,
       rem: 16,
     }
 
+    // Warm the precomputed utility lookup without collapsing a group.
+    designSystem.canonicalizeCandidates(['mt-2'], options)
+    using keysSpy = vi.spyOn(designSystem.utilities, 'keys')
+
@@
     let functionalKeysCalls = keysSpy.mock.calls.filter((args) => args[0] === 'functional').length
+    expect(functionalKeysCalls).toBeGreaterThan(0)
     expect(functionalKeysCalls).toBeLessThanOrEqual(3)
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a6cc4328-17c5-4454-ac4b-ef1473902e11
Commits

Reviewing files that changed from the base of the PR and between 32275ac and fb399a2.

Files selected for processing: 1

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

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@RobinMalfait
RobinMalfait self-requested a review November 16, 2026 21:32
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
@RobinMalfait
RobinMalfait force-pushed the fix-canonicalize-dynamic-utilities-cache branch from fb399a2 to 0191860 Compare November 16, 2026 21:34
@RobinMalfait

Copy link
Copy Markdown
Contributor

I rebased this PR on the latest main and force pushed it.

Sent by an AI assistant on behalf of @RobinMalfait.

@rudy-07
rudy-07 force-pushed the fix-canonicalize-dynamic-utilities-cache branch from 648ab6b to 4f6bfe3 Compare November 17, 2026 13:22
rudy-07 and others added 2 commits November 20, 2026 16:17
…apse groups

# Conflicts:
#	packages/tailwindcss/src/canonicalize-candidates.test.ts
@RobinMalfait
RobinMalfait force-pushed the fix-canonicalize-dynamic-utilities-cache branch from 4f6bfe3 to 470a106 Compare November 20, 2026 15:18
@RobinMalfait
RobinMalfait enabled auto-merge (squash) November 20, 2026 15:18

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Moving the dynamic utility lookups into the design system storage is a clean fix, and it follows the existing cache pattern nicely.

Sent by an AI assistant on behalf of @RobinMalfait.

@RobinMalfait
RobinMalfait merged commit 388a9da into tailwindlabs:main Nov 20, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: reuse dynamic utility lookups across canonicalizeCandidates collapse groups

2 participants