Repository navigation
perf: hash a string-backed source without materializing its buffer - #286
Conversation
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 detectedLatest commit: c8b7131 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 |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
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 2.71%
|
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Warning Review limit reachedNext included review available in 26 minutes. View limit detailsLimit 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. Review configuration: ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review. Walkthrough
Merge Risk: ⚪ Minimal · up to 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)
✨ Finishing Touches 💡 1📝 Generate docstrings
🧪 Generate unit tests (beta)
🛠️ Fix failing CI checks 💡
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. Comment |
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.
Summary
RawSource.updateHashandOriginalSource.updateHashcallbuffer(), 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 bytesbuffer()produces, so the string is hashed directly instead and no digest changes. webpack hits this once per module inNormalModule._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
arrayBuffersafter the build drop from 8.74–8.76 MB to 5.81 MB withdevtool: false, and from 9.35–12.11 MB to 6.42 MB withdevtool: "source-map"— disjoint over six and five alternating runs. 1029Buffer.fromcalls 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.jsandtest/OriginalSource.jsgain 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 thelib/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