Skip to content

perf(vite): cache normalized inspect module ids - #619

Open
webfansplz wants to merge 1 commit into
mainfrom
perf/inspect-cache-public-module-id
Open

webfansplz wants to merge 1 commit into
mainfrom
perf/inspect-cache-public-module-id

Conversation

@webfansplz

Copy link
Copy Markdown
Member

Summary

Every recorded hook call normalizes its module id through getPublicModuleId, so the same id is recomputed once per plugin per hook. For dependency ids this includes a path.relative call, which made it the most expensive part of the inspect hot path in projects with many node_modules modules. This PR caches the result per environment context.

Changes

  • getPublicModuleId stores its result in a per-env Map<id, publicId> and returns the cached value on later calls.
  • The cache is cleared when the env's root changes, because the result depends on it.
  • The cache is capped at 10,000 entries and cleared when full. Ids can carry HMR timestamps, so without a cap it would keep growing over a long dev session. After a clear, ids are simply recomputed.

Impact

Measured locally with a synthetic project (5,000 modules × 200 lines, half of them under node_modules, 20 plugins), median of 10 runs:

Before After
Transform time 964 ms (+39% vs no inspect) 822 ms (+19% vs no inspect)
recordTransform (CPU profile) 167 ms 21 ms
normalizeModuleId (CPU profile) 154 ms 9 ms

This roughly halves inspect's overhead on transforms when a project has many dependency modules. With no node_modules modules the saving is small (~20 ms), since only the string replacements are skipped.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 09:48

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@pkg-pr-new

pkg-pr-new Bot commented Oct 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vitejs/devtools

npm i https://pkg.pr.new/@vitejs/devtools@619

@vitejs/devtools-kit

npm i https://pkg.pr.new/@vitejs/devtools-kit@619

@vitejs/devtools-oxc

npm i https://pkg.pr.new/@vitejs/devtools-oxc@619

@vitejs/devtools-rolldown

npm i https://pkg.pr.new/@vitejs/devtools-rolldown@619

@vitejs/devtools-vite

npm i https://pkg.pr.new/@vitejs/devtools-vite@619

@vitejs/devtools-vitest

npm i https://pkg.pr.new/@vitejs/devtools-vitest@619

commit: 19f2aa6

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