Improve utility lookup performance when using v4 #1509
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Companion PR to tailwindlabs/tailwindcss#19405
The aim of this PR is to improve the performance around looking up / compiling candidates into utilities when using v4.
It does this in three stages:
There are still some internal APIs that use PostCSS because they interface with multiple versions of Tailwind CSS. These still use PostCSS and we translate our AST into a PostCSS AST for these cases. Eventually these APIs will also be converted to use our internal CSS AST directly — we'll support old versions by converting from PostCSS to our internal AST — that is for a future PR.
For new enough versions, since our AST here mirrors the one in Tailwind CSS, we can use the nodes directly from Tailwind CSS itself instead of generating the code inside Tailwind CSS, serializing to a string, and then finally re-parsing. This is exactly what the
candidatesToAstenables (will ship in Tailwind CSS v4.1.18).A large portion of the candidate compiling perf is spent on color lookups. The above will help with that significantly but there's still a large amount of time analyzing declaration values for colors. This PR replaces our unwieldy color regex to pick out potential colors with a fine tuned parser.
This speedup gives us more headroom to possibly re-enable mask utility color swatches. The overhead from parsing an extra 5k utilities is still there and still fairly significant but it is now much lower than before.
Fixes #1295