perf(runtime): 让 cn 复用聚合包装缓存 - #1205
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
为什么 weapp
cn和merge差不多快不是 npm
cn引擎和tailwind-merge差不多。上游cn()在稳定参数上可以快一个数量级。包上 weapp 之后,旧实现每次clsx → unescape → npm cn() → escape,拆掉了参数身份缓存,又没有 merge 那层 256 LRU,热路径反而更慢。改动
wrapRuntimeAggregator(原wrapClassAggregator)@weapp-tailwindcss/cn改为包装cn包的twMerge,复用有界 LRU 和按需 unescape/escape不改冲突规则,不加 rpx 长度归一化。
text-red+text-[80rpx]仍与 merge 不等价。Related to #1200
本地验证
pnpm --filter @weapp-tailwindcss/runtime testpnpm --filter @weapp-tailwindcss/cn testpnpm --filter @weapp-tailwindcss/merge exec vitest run test/twMerge.test.tspnpm --filter @weapp-tailwindcss/runtime tsd/pnpm --filter @weapp-tailwindcss/cn tsd同一 bench 矩阵(Apple M4 Max / Node 24),热路径从「merge 快 2–10×」变成同量级:
cache-miss上 cn 仍快于 merge。顺带对齐了已转义2xl:数字变体的合并(per-token unescape)。