Skip to content

perf(kit): drop hook events early and measure messages without copies - #1206

Merged
webfansplz merged 1 commit into
mainfrom
perf-kit-hook-transport
Dec 2, 2026
Merged

webfansplz merged 1 commit into
mainfrom
perf-kit-hook-transport

Conversation

@webfansplz

Copy link
Copy Markdown
Member

Summary

Vue calls the DevTools hook on every component render, add, remove, and emit, even while no DevTools panel is open. Before this PR, the kit built a full runtime event for each call and only then dropped it, so apps paid for DevTools they were not using. With a panel open, every outgoing message was serialized and then copied into a TextEncoder buffer just to measure its size. Tree patch events were serialized three times.

  • Drop hook events early: when no client is attached, component, emit, and perf hook events return at the hook before normalization. App init and unmount, plugin setup, and Vapor child tracking still run, so the tree reads correctly once a panel connects. receivedEvents and droppedEvents now also count events from unregistered or hidden apps while inactive.
  • Cache hidden DevTools apps: whether an app belongs to DevTools itself is cached per app once it is mounted. Before mount, Vue has not set _instance or _container yet, so nothing is cached.
  • Measure messages without copies: UTF-8 bytes are counted directly from the JSON string, with an ASCII fast path, and match TextEncoder. Tree patch events are serialized once, and the result gives both the event's own size and its size when merged into a buffered batch. Keyed events that replace their buffered entry reuse their measured size.

Results

On a local benchmark (not included): Vue 3.5 in happy-dom with 5,000 components, before and after runs interleaved:

  • 100k component:updated hook calls with DevTools closed: ~8.2 ms → ~2.1 ms
  • Kit overhead per full re-render with DevTools closed, over an empty hook: ~3.4 ms → ~0.7–2.0 ms
  • Enqueueing 1,000 timeline and state events: ~2.0 ms → ~1.6 ms

Testing

  • inactive-collection-hook.test.ts: with real Vue components, events are dropped while inactive and the current tree is read after a client connects; apps still register and unregister while inactive; apps hidden by devtools.hide or mounted in a vite-devtools-* shadow root stay hidden from mount onward. Breaking the cache or the lifecycle passthrough makes these tests fail.
  • event-buffer.test.ts: byte counts match TextEncoder for every UTF-8 width and for lone surrogates. The existing legacy-equivalence tests still pass.
  • Manual run in playground/basic with the panel opened from an iframe: routing with the panel closed and then opening it, live tree patches, state refresh, timeline, and Pinia. The flow had no regressions compared with main.
  • pnpm lint && pnpm test && pnpm typecheck: pass (84 files / 552 tests). The Vite and Chrome smoke tests pass.

- Return at the hook when no client is attached, before building the event.
  App lifecycle, plugin setup, and Vapor child tracking still run.
- Cache whether an app belongs to DevTools itself once it is mounted.
- Count UTF-8 bytes directly instead of encoding a copy, serialize tree
  patch events once, and reuse the size of keyed events that replace
  their entry.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 07:14
@netlify

netlify Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vue-devtools-docs canceled.

Name Link
🔨 Latest commit 8f203b8
🔍 Latest deploy log https://app.netlify.com/projects/vue-devtools-docs/deploys/6b0fc54f60ab923d8cd3127d

@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@vue/devtools-agent@1206
npm i https://pkg.pr.new/@vue/devtools-api@1206
npm i https://pkg.pr.new/@vue/devtools-kit@1206
npm i https://pkg.pr.new/vite-plugin-vue-devtools@1206

commit: 8f203b8

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the DevTools overhead reductions.

No open issues 🎉

What changed in this PR

Reduces shared DevTools kit overhead while collection is inactive and when measuring outgoing messages.

Changes:

  • Drops inactive hook events before normalization while preserving lifecycle and Vapor tracking.
  • Caches hidden-app detection after mount.
  • Counts UTF-8 bytes without encoded copies and reuses tree-event measurements.
File Description
tests/​unit/​rpc/​event-buffer.test.ts Checks byte counts against TextEncoder.
tests/​integration/​runtime/​inactive-collection-hook.test.ts Covers inactive collection, lifecycle handling, and hidden apps.
packages/​kit/​src/​runtime/​components.ts Uses shared message measurement for component metadata.
packages/​kit/​src/​rpc/​event-buffer.ts Avoids encoding copies and reuses event-size calculations.
packages/​kit/​src/​hook/​normalize.ts Caches hidden-app detection after mount.
packages/​kit/​src/​hook/​install.ts Skips normalization for inactive collection events.

Review effort: Balanced

@webfansplz
webfansplz merged commit 7f20565 into main Dec 2, 2026
9 checks passed
@webfansplz
webfansplz deleted the perf-kit-hook-transport branch December 2, 2026 10:23
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.

2 participants