Skip to content

perf(bundler): reduce unplugin transform overhead - #782

Merged
harlan-zw merged 2 commits into
mainfrom
perf/unplugin-cpu
Jun 24, 2026
Merged

harlan-zw merged 2 commits into
mainfrom
perf/unplugin-cpu

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Jun 24, 2026 •

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

None

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

Speeds up the unplugin hot paths by replacing URL parsing in transformInclude with simple id/query parsing and skipping React/Solid/Svelte streaming AST work when files have no head composables.

Adds native code filters for the remaining server-composable and head.ssr transforms, while keeping the existing in-handler guards for bundlers that do not apply transform filters. Includes a Vitest benchmark for the transform filters, create-head wrapping, minify transform, SSR replacement, and streaming skip paths.

📊 Benchmarks

Ran locally with:

pnpm vitest bench bench/unplugin-transform.bench.ts --run

Vitest bench reports mean time per sample. Latest run on perf/unplugin-cpu:

Case Mean
transformInclude mixed ids 0.1281ms
useSeoMetaTransform static calls 1.4683ms
minifyTransform inline script/style 0.2526ms
treeshakeServerComposables many calls 1.3443ms
treeshakeServerComposables skip unrelated code 0.0009ms
ssrStaticReplace many head.ssr reads 0.1709ms
ssrStaticReplace skip unrelated code 0.0009ms
createHeadTransform many createHead calls 0.3197ms
react streaming skip JSX without head calls 0.0106ms
react streaming transform JSX with head calls 0.8371ms
solid streaming skip JSX without head calls 0.0107ms

Headline before/after from the initial baseline in this branch work:

Case Before After Change
transformInclude mixed ids 4.6990ms 0.1281ms 36.7x faster
react streaming skip JSX without head calls 0.9437ms 0.0106ms 89.0x faster
solid streaming skip JSX without head calls 0.9346ms 0.0107ms 87.3x faster
createHeadTransform many createHead calls 0.3569ms 0.3197ms 1.1x faster

@coderabbitai

coderabbitai Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: abb8150e-3403-43f2-94fe-288155441d89

📥 Commits

Reviewing files that changed from the base of the PR and between f505b28 and 6aca478.

📒 Files selected for processing (5)
  • bench/unplugin-transform.bench.ts
  • packages/bundler/src/unplugin/SSRStaticReplace.ts
  • packages/bundler/src/unplugin/TreeshakeServerComposables.ts
  • packages/bundler/test/ssrStaticReplace.test.ts
  • packages/bundler/test/treeshakeServerComposables.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/bundler/src/unplugin/TreeshakeServerComposables.ts
  • bench/unplugin-transform.bench.ts

📝 Walkthrough

Walkthrough

Adds shared transform helpers, updates bundler unplugin transforms to use them, wraps server-side plugins with code filters, adds head-composable early exits in streaming plugins, and introduces a benchmark suite plus matching test updates.

Changes

Transform parsing helpers, plugin filters, and streaming guards

Layer / File(s) Summary
Shared transform helpers
packages/bundler/src/unplugin/utils.ts
Exports splitTransformId, getQueryValue, and isVueScriptRequest as centralized helpers for transform ID parsing and Vue script request detection.
Bundler transforms use shared parsing
packages/bundler/src/unplugin/MinifyTransform.ts, packages/bundler/src/unplugin/UseSeoMetaTransform.ts, packages/bundler/src/unplugin/TreeshakeServerComposables.ts, packages/bundler/src/unplugin/CreateHeadTransform.ts
Switches transform inclusion logic to the shared helpers, caches UseSeoMetaTransform import paths in a Set, and batches CreateHeadTransform import injection into a single prepend.
Server-side plugin code filters
packages/bundler/src/unplugin/SSRStaticReplace.ts, packages/bundler/src/unplugin/TreeshakeServerComposables.ts, packages/bundler/test/ssrStaticReplace.test.ts, packages/bundler/test/treeshakeServerComposables.test.ts
Wraps both plugins with withCodeFilter, and updates tests to exercise handler shapes, transformInclude gating, and the expected filter regexes.
Head-composable early exits
packages/react/src/stream/plugin.ts, packages/solid-js/src/stream/plugin.ts, packages/svelte/src/stream/plugin.ts
Adds hasHeadComposable helpers in the streaming plugins, reuses them in the AST checks, and returns early from the transform hooks when no head composables are present.
Transform benchmark suite
bench/unplugin-transform.bench.ts
Adds benchmark fixtures, plugin invocation helpers, and benchmark cases for inclusion checks, SEO meta, minification, server treeshaking, SSR static replacement, createHead, and React/Solid streaming transforms.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • unjs/unhead#740: Modifies packages/bundler/src/unplugin/SSRStaticReplace.ts and related test coverage around its transform gating.

Poem

🐇 I hopped through helpers, swift and neat,
With filters, guards, and benchmarksీట్.
splitTransformId clears the way,
Head calls rest sooner, light and gay.
Thump thump — the plugins now run bright!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.25% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, conventional, and accurately captures the main performance-focused change.
Description check ✅ Passed The description matches the template sections and provides a clear summary of the performance changes.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/unplugin-cpu

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.

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Bundle Size Gzipped
Client (Minimal) 11.1 kB 4.5 kB
Server (Minimal) 10.7 kB 4.4 kB
Vue Client (Minimal) 12.1 kB 5 kB
Vue Server (Minimal) 11.6 kB 4.8 kB

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

Benchmark notes

Ran locally with:

pnpm vitest bench bench/unplugin-transform.bench.ts --run

Vitest bench reports mean time per sample. Latest run on perf/unplugin-cpu:

Case Mean
transformInclude mixed ids 0.1281ms
useSeoMetaTransform static calls 1.4683ms
minifyTransform inline script/style 0.2526ms
treeshakeServerComposables many calls 1.3443ms
treeshakeServerComposables skip unrelated code 0.0009ms
ssrStaticReplace many head.ssr reads 0.1709ms
ssrStaticReplace skip unrelated code 0.0009ms
createHeadTransform many createHead calls 0.3197ms
react streaming skip JSX without head calls 0.0106ms
react streaming transform JSX with head calls 0.8371ms
solid streaming skip JSX without head calls 0.0107ms

Headline before/after from the initial baseline in this branch work:

Case Before After Change
transformInclude mixed ids 4.6990ms 0.1281ms 36.7x faster
react streaming skip JSX without head calls 0.9437ms 0.0106ms 89.0x faster
solid streaming skip JSX without head calls 0.9346ms 0.0107ms 87.3x faster
createHeadTransform many createHead calls 0.3569ms 0.3197ms 1.1x faster

The biggest practical win is on skip paths: native transform filters avoid entering JS handlers for unrelated modules, and React/Solid streaming avoids AST parsing on JSX files without head composables.

@harlan-zw
harlan-zw merged commit 79106e4 into main Jun 24, 2026
7 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.

1 participant