Merge pull request #994 from HoneyTyagii/perf/canonical-digest-batching-904

perf(domain): batch canonical digest writes with bounded buffer
This commit is contained in:
Kaoru0822
2026-10-07 09:14:23 -07:00
committed by GitHub
2 changed files with 19 additions and 1 deletions
+7
View File
@@ -62,6 +62,13 @@ describe("incremental canonical digest", () => {
expect(digestCanonicalValue(value)).toBe(legacyDigest(value));
});
it("keeps digests identical when buffered parts cross flush boundaries", () => {
const manyKeys: Record<string, unknown> = {};
for (let i = 0; i < 2000; i += 1) manyKeys[`k${i}`] = i;
const value = { keys: manyKeys, tail: ["€", "😀", "x".repeat(9000)] };
expect(digestCanonicalValue(value)).toBe(legacyDigest(value));
});
it("preserves canonicalize property access and toJSON cycle behavior", () => {
const createAccessor = () => {
let reads = 0;
+12 -1
View File
@@ -6,8 +6,19 @@ export const digestCanonicalValue = (
context = "Comparison",
): string => {
const hash = createHash("sha256");
const encoded = emitCanonical(value, (part) => hash.update(part), new Set());
// Batch small emitted parts to cut hash.update crossings.
// Flush cuts only at part boundaries, so the hashed byte stream is unchanged.
let buffered = "";
const emit = (part: string): void => {
buffered += part;
if (buffered.length >= 8192) {
hash.update(buffered);
buffered = "";
}
};
const encoded = emitCanonical(value, emit, new Set());
if (!encoded) throw new TypeError(`${context} could not canonicalize data`);
if (buffered) hash.update(buffered);
return hash.digest("hex");
};