perf(ModuleConcatenationPlugin): cache root chunks and per-module runtimes#21115
Conversation
…times Reuse the root module's chunk list across all candidate checks and memoize each module's merged chunk-runtime across roots. Cuts concatenation-phase CPU (~9%) and transient heap (~8%) on large builds with no behavior change. Also corrects the stale O(n^2)->O(n) reuse TODO: configurations cannot be reused across roots because a module shared by several chunks is concatenated into each consumer (module duplication). https://claude.ai/code/session_017RvJfqwz9rfYoBXAry12vu
🦋 Changeset detectedLatest commit: c7cd8e7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is packaged and the instant preview is available (6d8b131). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@6d8b131
yarn add -D webpack@https://pkg.pr.new/webpack@6d8b131
pnpm add -D webpack@https://pkg.pr.new/webpack@6d8b131 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21115 +/- ##
=======================================
Coverage 91.99% 91.99%
=======================================
Files 581 581
Lines 61433 61448 +15
Branches 16787 16791 +4
=======================================
+ Hits 56516 56531 +15
Misses 4917 4917
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will degrade performance by 46.91%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Memory | benchmark "asset-modules-inline", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
220.4 KB | 1,231.2 KB | -82.1% |
| ❌ | Memory | benchmark "asset-modules-bytes", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
246.7 KB | 855.8 KB | -71.18% |
| ❌ | Memory | benchmark "context-esm", scenario '{"name":"mode-production","mode":"production"}' |
7.4 MB | 10.3 MB | -28.49% |
| ⚡ | Memory | benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
330.1 KB | 153.3 KB | ×2.2 |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/module-concatenation-runtime-cache (c7cd8e7) with main (e861a84)
…visited set Cache each inner module's getExportsType result on its per-codeGeneration info object (the "dynamic" case walks the module graph and is queried once per reference), and only allocate the circular-reexport guard set when the reexport-following recursion is actually reached. No behavior change. https://claude.ai/code/session_017RvJfqwz9rfYoBXAry12vu
Summary
Module concatenation re-derived per-module data on hot paths: it re-materialized the root module's chunk list for every candidate check, and re-merged each module's chunk-runtime on every
_tryToAddcall. This caches the root's chunks once per configuration and memoizes each module's merged runtime across roots. On a 4k-module concatenation-heavy build this cuts the concatenation-phase CPU by ~9% and its transient heap by ~8%, with no behavior change (zeroStatsTestCasessnapshot churn).It also corrects a stale
O(n^2) -> O(n)"reuse existing configuration" TODO: configurations cannot be reused across roots, because a module shared by several chunks is concatenated into each consumer (module duplication), so a module legitimately belongs to multiple configurations.What kind of change does this PR introduce?
perf
Did you add tests for your changes?
No — the change is behavior-preserving; it is covered by the existing
StatsTestCasessnapshots andconcatenate-modulesconfig cases, which pass unchanged.Does this PR introduce a breaking change?
No.
If relevant, what needs to be documented once your changes are merged or what have you already documented?
n/a
Use of AI
AI (Claude) was used to investigate the TODO, prototype and benchmark alternatives, and draft the code and this PR description. An initial reuse-based rewrite was discarded after it regressed module deduplication; the final behavior-preserving change and all results were verified against the test suite.
Generated by Claude Code