From 67688cc65178273cd150d23cb41fcb722018c687 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 5 Jul 2026 14:42:17 -0400 Subject: [PATCH] stream: hoist repeated loads in readable paths fromList() re-read state.length and each chunk's length several times per call and re-loaded buffer[idx] around every copy, and read() loaded state.length three times in its read(0) check; the engine cannot fold these loads across the intervening copy and slice calls. Cache them in locals instead. Ported from Bun's fork of the same functions (src/js/internal/streams/readable.ts fromList/read), which carries these hoists on top of the shared readable-stream lineage. benchmark/compare.js against the unmodified baseline (60-run capture plus an independent 30-run repeat, Welch t-test): streams/readable-unevenread +0.65% (p=4.3e-4) / +0.59% (p=5.9e-3) and streams/pipe.js +0.74% (p=2.0e-4) / +0.52% (p=8.8e-3), with no significant regression across the captured streams benchmarks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/streams/readable.ts Signed-off-by: Yagiz Nizipli Assisted-by: Grok (Grok Build) PR-URL: https://github.com/nodejs/node/pull/64312 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/streams/readable.js | 117 +++++++++++++++++-------------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index 36e731c0d6a..1c496a28f66 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -673,15 +673,17 @@ Readable.prototype.read = function(n) { // If we're doing read(0) to trigger a readable event, but we // already have a bunch of data in the buffer, then just trigger - // the 'readable' event and move on. + // the 'readable' event and move on. `state.length` cannot change + // within this block, so it is loaded once instead of three times. + const stateLength = state.length; if (n === 0 && (state[kState] & kNeedReadable) !== 0 && ((state.highWaterMark !== 0 ? - state.length >= state.highWaterMark : - state.length > 0) || + stateLength >= state.highWaterMark : + stateLength > 0) || (state[kState] & kEnded) !== 0)) { debug('read: emitReadable'); - if (state.length === 0 && (state[kState] & kEnded) !== 0) + if (stateLength === 0 && (state[kState] & kEnded) !== 0) endReadable(this); else emitReadable(this); @@ -1624,8 +1626,14 @@ Readable._fromList = fromList; // This function is designed to be inlinable, so please take care when making // changes to the function body. function fromList(n, state) { + // `state.length` cannot change while this function runs (only the + // caller updates it, after this returns) and the chunk lengths feeding + // the copy loops cannot change across the copy calls, so every + // repeated property load below is hoisted into a local. + const stateLength = state.length; + // nothing buffered. - if (state.length === 0) + if (stateLength === 0) return null; let idx = state.bufferIndex; @@ -1637,7 +1645,7 @@ function fromList(n, state) { if ((state[kState] & kObjectMode) !== 0) { ret = buf[idx]; buf[idx++] = null; - } else if (!n || n >= state.length) { + } else if (!n || n >= stateLength) { // Read it all, truncate the list. if ((state[kState] & kDecoder) !== 0) { ret = ''; @@ -1651,61 +1659,68 @@ function fromList(n, state) { ret = buf[idx]; buf[idx++] = null; } else { - ret = Buffer.allocUnsafe(state.length); + ret = Buffer.allocUnsafe(stateLength); let i = 0; while (idx < len) { - TypedArrayPrototypeSet(ret, buf[idx], i); - i += buf[idx].length; + const data = buf[idx]; + TypedArrayPrototypeSet(ret, data, i); + i += data.length; buf[idx++] = null; } } - } else if (n < buf[idx].length) { - // `slice` is the same for buffers and strings. - ret = buf[idx].slice(0, n); - buf[idx] = buf[idx].slice(n); - } else if (n === buf[idx].length) { - // First chunk is a perfect match. - ret = buf[idx]; - buf[idx++] = null; - } else if ((state[kState] & kDecoder) !== 0) { - ret = ''; - while (idx < len) { - const str = buf[idx]; - if (n > str.length) { - ret += str; - n -= str.length; - buf[idx++] = null; - } else { - if (n === str.length) { - ret += str; - buf[idx++] = null; - } else { - ret += str.slice(0, n); - buf[idx] = str.slice(n); - } - break; - } - } } else { - ret = Buffer.allocUnsafe(n); - - const retLen = n; - while (idx < len) { - const data = buf[idx]; - if (n > data.length) { - TypedArrayPrototypeSet(ret, data, retLen - n); - n -= data.length; - buf[idx++] = null; - } else { - if (n === data.length) { - TypedArrayPrototypeSet(ret, data, retLen - n); + const first = buf[idx]; + const firstLength = first.length; + if (n < firstLength) { + // `slice` is the same for buffers and strings. + ret = first.slice(0, n); + buf[idx] = first.slice(n); + } else if (n === firstLength) { + // First chunk is a perfect match. + ret = first; + buf[idx++] = null; + } else if ((state[kState] & kDecoder) !== 0) { + ret = ''; + while (idx < len) { + const str = buf[idx]; + const strLength = str.length; + if (n > strLength) { + ret += str; + n -= strLength; buf[idx++] = null; } else { - TypedArrayPrototypeSet(ret, new FastBuffer(data.buffer, data.byteOffset, n), retLen - n); - buf[idx] = new FastBuffer(data.buffer, data.byteOffset + n, data.length - n); + if (n === strLength) { + ret += str; + buf[idx++] = null; + } else { + ret += str.slice(0, n); + buf[idx] = str.slice(n); + } + break; + } + } + } else { + ret = Buffer.allocUnsafe(n); + + const retLen = n; + while (idx < len) { + const data = buf[idx]; + const dataLength = data.length; + if (n > dataLength) { + TypedArrayPrototypeSet(ret, data, retLen - n); + n -= dataLength; + buf[idx++] = null; + } else { + if (n === dataLength) { + TypedArrayPrototypeSet(ret, data, retLen - n); + buf[idx++] = null; + } else { + TypedArrayPrototypeSet(ret, new FastBuffer(data.buffer, data.byteOffset, n), retLen - n); + buf[idx] = new FastBuffer(data.buffer, data.byteOffset + n, dataLength - n); + } + break; } - break; } } }