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; } } }