Skip to content

perf(tv): memoize slot invocations with simple args#6737

Open
benjamincanac wants to merge 3 commits into
v4from
perf/tv-slot-memo
Open

perf(tv): memoize slot invocations with simple args#6737
benjamincanac wants to merge 3 commits into
v4from
perf/tv-slot-memo

Conversation

@benjamincanac

Copy link
Copy Markdown
Member

🔗 Linked issue

Relates to #6293

❓ Type of change

  • 👌 Enhancement (improving an existing functionality)

📚 Description

Every render re-invokes the tv slot functions (ui.base(), ui.label(), ui.td(), ...), re-running variant resolution and twMerge even when the arguments are identical to the previous call — per table cell, per navigation item, per re-render. tailwind-variants caches nothing across invocations, and CPU profiling showed this as the largest remaining theming cost after the reactivity fixes in #6736.

This memoizes slot invocations in the tv wrapper when the arguments are fully serializable: primitives and arrays of primitives. Anything else, clsx-style object classes and the (defaults) => classes replacer functions from #6562, bails to the existing uncached path, so those semantics are untouched (covered by test/utils/tv.spec.ts). JSON.stringify is used as the cache key, and its dropping of undefined values matches tv's own treatment of undefined variants. The cache lives on the invocation result, so an app.config.ui change or a variant-prop recompute starts fresh, and each slot's cache is capped at 500 entries to guard against pathological dynamic inputs.

Measured with the benchmarks from #6728/#6736: a 100-cell ui.td() loop goes from 5,510 to 73,962 ops/s (13.4x), and a Button re-render from 4,103 to 5,235 ops/s on top of #6736 (2x cumulatively vs v4). Output classes are byte-identical: full suite passes with zero snapshot changes.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@github-actions github-actions Bot added the v4 #4488 label Jul 15, 2026
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ab91b1fb-86d5-444b-8e1c-6916af0da6bd

📥 Commits

Reviewing files that changed from the base of the PR and between 05272d7 and 6dfb300.

📒 Files selected for processing (1)
  • test/utils/tv.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/utils/tv.spec.ts

📝 Walkthrough

Walkthrough

wrapSlots now memoizes slot results when slot props contain cacheable primitive or nested primitive-array values and no replacer is present. It generates JSON-based keys, handles undefined values consistently, stores results per slot, reuses cached strings, and clears caches above 500 entries. Tests cover cache reuse, argument isolation, key ordering, and non-cacheable class inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: memoizing tv slot invocations for simple arguments.
Description check ✅ Passed The description is directly about the slot memoization change and its behavior, tests, and performance impact.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/tv-slot-memo

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


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

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 15, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/@nuxt/ui@6737

commit: 6dfb300

@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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/utils/tv.spec.ts`:
- Around line 162-165: Rename the test case around build to clarify that it
verifies identical output for reordered keys, not memoization cache reuse; leave
the assertion and implementation unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3b50c4c8-f271-457c-b88c-ebc679c4ffdb

📥 Commits

Reviewing files that changed from the base of the PR and between ef66e18 and 05272d7.

📒 Files selected for processing (2)
  • src/runtime/utils/tv.ts
  • test/utils/tv.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/runtime/utils/tv.ts

Comment thread test/utils/tv.spec.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v4 #4488

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant