perf: reduce retained memory in module dependencies#21183
Conversation
🦋 Changeset detectedLatest commit: 79ae437 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 |
Dependency already stores loc as four numbers; the setter additionally cached the passed SourceLocation (with its two Position objects), so every dependency retained them even when its loc is never read. Drop the cache and let `get loc` rebuild lazily from the numbers. Output and source maps are byte-identical. https://claude.ai/code/session_01Y5YJB5N9QzFts3duRm8znr
a9afd3c to
79ae437
Compare
|
This PR is packaged and the instant preview is available (7adde75). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@7adde75
yarn add -D webpack@https://pkg.pr.new/webpack@7adde75
pnpm add -D webpack@https://pkg.pr.new/webpack@7adde75 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21183 +/- ##
=======================================
Coverage 92.69% 92.69%
=======================================
Files 588 588
Lines 64024 64024
Branches 17760 17760
=======================================
Hits 59345 59345
Misses 4679 4679
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 improve performance by ×2.5
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Memory | benchmark "lodash", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
859.4 KB | 125.2 KB | ×6.9 |
| ⚡ | Memory | benchmark "asset-modules-inline", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
1,299.2 KB | 320.1 KB | ×4.1 |
| ⚡ | Memory | benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
330 KB | 152.3 KB | ×2.2 |
| ⚡ | Memory | benchmark "asset-modules-bytes", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
327.5 KB | 247.6 KB | +32.28% |
| ⚡ | Memory | benchmark "devtool-source-map", scenario '{"name":"mode-production","mode":"production"}' |
7.8 MB | 6.4 MB | +21.34% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/reduce-dependency-memory (79ae437) with main (b04e811)
Summary
Dependencyalready stores its source location as four numbers, but thelocsetter also cached the originalSourceLocation(and its twoPositionobjects), so every dependency retained them even whenlocis never read. The setter now drops that cache andget locrebuilds it lazily from the numbers — the same thingsetLocandserialize/deserializealready do (filesystem-cached dependencies already run this way). Measured −5.9 MB retained heap (−1.4%) on a 6,000-module build; peak heap unchanged, output byte-identical.What kind of change does this PR introduce?
perf
Did you add tests for your changes?
No new tests — it is a byte-identical refactor covered by existing suites; verified full StatsTestCases (131/131), ConfigCacheTestCases error/warning cases (cold + warm-from-cache), and byte-identical bundles + source maps.
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 profile heap usage, identify the redundant
locretention, implement the change, and verify byte-identical output across the test suites.