Skip to content

perf: hash a string-backed source without materializing its buffer - #286

Merged
alexander-akait merged 3 commits into
mainfrom
perf/hash-string-without-buffer
Sep 24, 2026
Merged

alexander-akait merged 3 commits into
mainfrom
perf/hash-string-without-buffer

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Summary

RawSource.updateHash and OriginalSource.updateHash call buffer(), which encodes the string to UTF-8 and memoizes the result — so every source that is hashed keeps its content twice, as a string and as a buffer, for as long as it lives. Node hashes a string as UTF-8, the same bytes buffer() produces, so the string is hashed directly instead and no digest changes. webpack hits this once per module in NormalModule._initBuildHash, which is where it was found.

Measured on a webpack build of three.js + lodash-es (1033 modules, 2.71 MB of module source): retained arrayBuffers after the build drop from 8.74–8.76 MB to 5.81 MB with devtool: false, and from 9.35–12.11 MB to 6.42 MB with devtool: "source-map" — disjoint over six and five alternating runs. 1029 Buffer.from calls per build are removed, one per module. CPU is unchanged and I make no speed claim: hashing those same sources costs 9.23 ms via the buffer against 8.18 ms via the string, ~1 ms per build, which no whole-build timing can resolve.

What kind of change does this PR introduce?

perf

Did you add tests for your changes?

Yes — test/RawSource.js and test/OriginalSource.js gain a digest check against the buffer-hashed bytes (ASCII, non-BMP and multi-line) plus an assertion that hashing leaves no buffer cached; the latter two fail without the lib/ change.

Does this PR introduce a breaking change?

No. Digests are unchanged — the existing string-vs-buffer equality tests across md5, md4 and xxhash64 still pass, and a webpack build against these files produces identical module build hashes, identical emitted bytes and an identical compilation hash.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a

Use of AI

AI was used to find the site, write the change and measure it. The memory numbers are counted and retained-heap readings over alternating fresh processes, not timings; the CPU figure is stated as a bound precisely because whole-build timing could not resolve it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SATR3eY1u1eFNvdcuz5Tgu


Generated by Claude Code

Summary by CodeRabbit

  • Performance
    • Source hashing is more memory-efficient for text-based content, avoiding unnecessary temporary copies during builds. This can reduce memory use and conversion work, especially in builds that process many text sources.
    • Hash results remain unchanged for plain-text and Unicode content, preserving consistent build behavior.
    • Text sources continue to be available in their original form after hashing.

RawSource.updateHash and OriginalSource.updateHash called buffer(), which
encodes the string to utf8 and memoizes the result, so every hashed source
kept its content twice for as long as it lived.

Node hashes a string as utf8 — the same bytes buffer() produces — so hash
the string directly. No digest changes.
@changeset-bot

changeset-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c8b7131

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack-sources Patch

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

@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.29%. Comparing base (164c9e2) to head (c8b7131).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #286   +/-   ##
=======================================
  Coverage   98.29%   98.29%           
=======================================
  Files          25       25           
  Lines        2283     2289    +6     
  Branches      731      735    +4     
=======================================
+ Hits         2244     2250    +6     
  Misses         37       37           
  Partials        2        2           
Flag Coverage Δ
integration 98.29% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.71%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 19 improved benchmarks
❌ 12 regressed benchmarks
✅ 181 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory prefix-source memory: new PrefixSource() 1.4 KB 193.2 KB -99.27%
❌ Memory source-map-source memory: new SourceMapSource(simple) 1 KB 17.4 KB -94.15%
❌ Memory replace-source memory: construct + 100 insertions 35.8 KB 101.5 KB -64.73%
❌ Memory original-source memory: new OriginalSource() 1.3 KB 2.3 KB -40.63%
❌ Memory source-map-source memory: sourceAndMap({ columns: true }) (combined inner) 1.7 MB 2.6 MB -34.56%
❌ Memory original-source memory: sourceAndMap({ columns: true }) 850.4 KB 1,277.9 KB -33.45%
❌ Memory clear-cache memory: shared modules (visited set — single allocation) 584 B 864 B -32.41%
❌ Memory source-map-source memory: new SourceMapSource(with inner map) 784 B 1,040 B -24.62%
❌ Memory original-source memory: map({ columns: true }) builds full mappings 845.9 KB 1,094 KB -22.68%
❌ Memory concat-source memory: source() concatenates children 147.8 KB 180.4 KB -18.08%
❌ Memory source-map-source memory: map({ columns: true }) 784 B 912 B -14.04%
❌ Memory original-source memory: map({ columns: false }) line-only mappings 106.5 KB 123.8 KB -14.02%
⚡ Memory cached-source memory: warm sourceAndMap() returns cached references 13,440 B 465 B ×29
⚡ Memory raw-source memory: updateHash() populates _cachedHashUpdate 1,617.6 KB 200.7 KB ×8.1
⚡ Memory clear-cache memory: shared modules (no visited set — allocates per chunk) 19.8 KB 6.7 KB ×3
⚡ Memory cached-source memory: getCachedData() allocates BufferedMap 840 B 432 B +94.44%
⚡ Memory replace-source memory: source() concatenates result 71.6 KB 45.3 KB +58.17%
⚡ Memory concat-source memory: new ConcatSource(...children) 3.4 KB 2.3 KB +49.31%
⚡ Memory compat-source memory: CompatSource.from() short-circuits on Source 584 B 392 B +48.98%
⚡ Memory raw-source memory: new RawSource(string) 1,168 B 784 B +48.98%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/hash-string-without-buffer (c8b7131) with main (164c9e2)

Open in CodSpeed

@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Warning

Review limit reached

Next included review available in 26 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 5ea2fe88-d474-482e-a592-a78da858eb7a

📥 Commits

Reviewing files that changed from the base of the PR and between dddcd06 and c8b7131.

📒 Files selected for processing (1)
  • test/CachedSource.js

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 78e424b1-56eb-485e-9d54-d99a8a17e896

📥 Commits

Reviewing files that changed from the base of the PR and between 86e406b and dddcd06.

📒 Files selected for processing (2)
  • lib/OriginalSource.js
  • lib/RawSource.js

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


Walkthrough

RawSource.updateHash and OriginalSource.updateHash now hash a cached buffer or, when no buffer is cached, a cached string. Tests verify that hashing string-backed sources does not populate the buffer cache and produces the expected UTF-8 digests. The changeset declares a patch release and reports build measurements.

Merge Risk: ⚪ Minimal · up to dddcd

String-backed sources are hashed without materializing buffers, and the supplied tests check digest equivalence and cache behavior. No actionable merge risk is identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: hashing string-backed sources without creating a buffer.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

One of the two cached forms is always set — the constructor sets whichever
matches its input, and clearCache only ever drops the derived one — so the
buffer fallback was dead code and showed up as uncovered diff.
…ches

CachedSource's hash tracker accumulates short strings and flushes them when
a Buffer arrives. RawSource.updateHash used to reach that flush by pushing
"RawSource" and then a Buffer; now it pushes a string, so nothing in the
suite exercised it. Drive it directly instead.
@alexander-akait
alexander-akait merged commit 399bc8c into main Sep 24, 2026
36 of 37 checks passed
@alexander-akait
alexander-akait deleted the perf/hash-string-without-buffer branch September 24, 2026 16:11
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