Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perf: avoid duplicate module resolution in dev transform requests - #23408

Open
dmchoi77 wants to merge 1 commit into
vitejs:mainfrom
dmchoi77:perf/avoid-duplicate-module-resolution
Open

perf: avoid duplicate module resolution in dev transform requests#23408
dmchoi77 wants to merge 1 commit into
vitejs:mainfrom
dmchoi77:perf/avoid-duplicate-module-resolution

Conversation

@dmchoi77

Copy link
Copy Markdown

Summary

Avoid resolving the same URL twice in the development server's cache-miss transform path.

transformRequest now receives both the module lookup result and the resolved result from a single module graph operation, so it can reuse the resolver result instead of invoking resolveId again.

Background

Before this change, the uncached transform request flow was:

  1. moduleGraph.getModuleByUrl(url) resolves the URL and looks up the module graph.
  2. If no module is found, transformRequest calls pluginContainer.resolveId() for the same URL again.
  3. The second resolver result is used to create the module entry.

The first resolved result is already available during the module lookup, but it is discarded instead of being returned to the caller. This duplicate work can accumulate across a large module graph, especially when resolver hooks are expensive or many resolver plugins are configured.

Changes

  • Add EnvironmentModuleGraph._getModuleByUrlAndResolved().
    • Return the resolved result together with the module lookup result.
    • Preserve the existing unresolved URL fast path.
  • Reuse the lookup result in transformRequest.
  • Allow _ensureEntryFromUrl() and _resolveUrl() to reuse a resolved null result explicitly.
  • Add tests covering resolved id and meta preservation and duplicate resolver avoidance.

Related Prior Work

This change is related to #13085, but addresses a different stage of the transform request flow.

  • PR 13085 fixed a race condition where resolving the URL again while creating the module graph entry inside loadAndTransform() could produce a different resolver result. It passed the already computed resolved result to _ensureEntryFromUrl().
  • This change removes duplicate resolution earlier in the transformRequest module lookup path. The new _getModuleByUrlAndResolved() helper returns both the module lookup result and the resolved result, allowing the second resolveId() call for the same URL to be skipped.
  • This complements, rather than replaces, the correctness fix from PR 13085.

Verification

  • pnpm exec vitest run packages/vite/src/node/server/__tests__/moduleGraph.spec.ts packages/vite/src/node/__tests__/dev.spec.ts
    • Test Files: 2 passed
    • Tests: 12 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