Skip to content

Commit 1c97070

Browse files
fix: hand out a recorded hash update as buffers (#289)
* fix: hand out a recorded hash update as buffers CachedSource.updateHash records what the wrapped source hashed so later calls replay it. Since 3.5.2 a string-backed source hashes its string, so the recording keeps that string, and getCachedData() handed it to callers that serialize it — webpack's pack warns "Serializing big strings" and pays for it on every deserialization. Encode the strings as buffers in getCachedData(), the boundary a caller serializes from, so the recording still holds the source's own string and nothing copies it until it is written out. Hashing reads a string as utf8, so the restored update hashes to the same digest. * Encode the recorded hash update once, in place The first version rebuilt the buffered array on every getCachedData() call, so a source serialized more than once was re-encoded each time: +19.7MB of buffer allocation over the css/large fixture's cache build. Encode in place instead. A source nobody asks cached data of still keeps its own string uncopied, and one that is handed out repeatedly is encoded once.
1 parent 6f9c00e commit 1c97070

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-sources": patch
3+
---
4+
5+
Hand out a recorded hash update as buffers, so cached data carries no big string.

‎lib/CachedSource.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ const mapToBufferedMap = (map) => {
5656
return bufferedMap;
5757
};
5858

59+
/**
60+
* Encodes every string a recorded hash update holds as a buffer, so cached data
61+
* handed to a caller carries no big string: a string goes into the serialized
62+
* payload, where it is copied in and rebuilt on every deserialization, while a
63+
* buffer is carried alongside it. Hashing reads a string as utf8, so the
64+
* encoded update hashes the same. It encodes in place, so a source handed out
65+
* repeatedly is encoded once, and one that never is keeps its string uncopied.
66+
* @param {(string | Buffer)[]=} update recorded hash update
67+
* @returns {(string | Buffer)[]=} the update, holding no string
68+
*/
69+
const bufferHashUpdate = (update) => {
70+
if (update === undefined) return undefined;
71+
for (let index = 0; index < update.length; index++) {
72+
const item = update[index];
73+
if (typeof item === "string") update[index] = Buffer.from(item, "utf8");
74+
}
75+
return update;
76+
};
77+
5978
/**
6079
* @param {null | BufferedMap} bufferedMap buffered map
6180
* @returns {null | RawSourceMap} map
@@ -209,7 +228,7 @@ class CachedSource extends Source {
209228
: undefined,
210229
size: this._cachedSize,
211230
maps: bufferedMaps,
212-
hash: this._cachedHashUpdate,
231+
hash: bufferHashUpdate(this._cachedHashUpdate),
213232
};
214233
}
215234

‎test/CachedSource.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,34 @@ describe.each([
637637
expect(clone.buffer()).toEqual(source.buffer());
638638
expect(clone.size()).toEqual(source.size());
639639
});
640+
641+
it("should hand out a recorded hash update without a big string", () => {
642+
const big = "a".repeat(200000);
643+
const source = new CachedSource(new OriginalSource(big, "big.js"));
644+
const internal = /** @type {{ _cachedHashUpdate: unknown[] }} */ (
645+
/** @type {unknown} */ (source)
646+
);
647+
648+
source.updateHash(crypto.createHash("md5"));
649+
650+
// a source nobody asks cached data of keeps its own string, uncopied
651+
expect(internal._cachedHashUpdate).toContain(big);
652+
653+
const cachedData = source.getCachedData();
654+
expect(cachedData.hash).toBeDefined();
655+
for (const item of /** @type {(string | Buffer)[]} */ (cachedData.hash)) {
656+
expect(typeof item).not.toBe("string");
657+
}
658+
659+
// asking again encodes nothing: the recording already holds buffers
660+
expect(source.getCachedData().hash).toBe(cachedData.hash);
661+
662+
// @ts-expect-error for tests
663+
const clone = new CachedSource(null, cachedData);
664+
const cloneHash = crypto.createHash("md5");
665+
clone.updateHash(cloneHash);
666+
const sourceHash = crypto.createHash("md5");
667+
source.updateHash(sourceHash);
668+
expect(cloneHash.digest("hex")).toBe(sourceHash.digest("hex"));
669+
});
640670
});

0 commit comments

Comments
 (0)