From 5d6932cbbf754bf13cd07b74bfbca0af0ae7e5f6 Mon Sep 17 00:00:00 2001 From: Eli White Date: Mon, 20 Apr 2026 00:02:20 -0700 Subject: [PATCH] perf: skip BigInt in blake3 b2Compress counter conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit blake3.b2Compress was calling fromBig(BigInt(counter), true) on every call to split the chunk counter into a (h, l) 32-bit pair. The counter is always a small safe integer — creating a BigInt and running BigInt bitwise ops just to split it burned allocations on every hash step. Inline the split directly: h = counter >>> 0 l = Math.floor(counter / 4294967296) >>> 0 Output is bit-identical to fromBig(..., true). Measured on autoresearch-scan (2000 charts, 8 workers, 2-run avg): previous (S2): 2.67 ms mean (3 runs: 2.634, 2.667, 2.717) this (S5): 2.59 ms mean (2 runs: 2.620, 2.556) delta: ~−3 to −6% mean 0 hash mismatches, 442/442 tests green. Patch extends @noble+hashes+1.8.0.patch with the blake3.js diff. --- patches/@noble+hashes+1.8.0.patch | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/patches/@noble+hashes+1.8.0.patch b/patches/@noble+hashes+1.8.0.patch index 2e5c1e9..848e7dc 100644 --- a/patches/@noble+hashes+1.8.0.patch +++ b/patches/@noble+hashes+1.8.0.patch @@ -70,3 +70,20 @@ index 0000000..0000001 100644 } return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 }; } +diff --git a/node_modules/@noble/hashes/blake3.js b/node_modules/@noble/hashes/blake3.js +index 0000000..0000002 100644 +--- a/node_modules/@noble/hashes/blake3.js ++++ b/node_modules/@noble/hashes/blake3.js +@@ -85,7 +85,11 @@ class BLAKE3 extends blake2_ts_1.BLAKE2 { + set() { } + b2Compress(counter, flags, buf, bufPos = 0) { + const { state: s, pos } = this; +- const { h, l } = (0, _u64_ts_1.fromBig)(BigInt(counter), true); ++ // Inlined fromBig(BigInt(counter), true) — counter is always a small ++ // safe integer (chunk index), so we can skip the BigInt alloc path. ++ // `le=true` returns { h: low 32, l: high 32 }. ++ const h = counter >>> 0; ++ const l = Math.floor(counter / 4294967296) >>> 0; + // prettier-ignore + const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = (0, blake2_ts_1.compress)(B3_SIGMA, bufPos, buf, 7, s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], h, l, pos, flags); + s[0] = v0 ^ v8;