Skip to content

Commit c0e7035

Browse files
committed
stream: Short-circuit buffer pushes when flowing
When a stream is flowing, and not in the middle of a sync read, and the read buffer currently has a length of 0, we can just emit a 'data' event rather than push it onto the array, emit 'readable', and then automatically call read(). As it happens, this is quite a frequent occurrence! Making this change brings the HTTP benchmarks back into a good place after the removal of the .ondata/.onend socket kludge methods.
1 parent 967b5db commit c0e7035

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/_stream_readable.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,24 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
147147
if (state.decoder && !addToFront && !encoding)
148148
chunk = state.decoder.write(chunk);
149149

150-
// update the buffer info.
151-
state.length += state.objectMode ? 1 : chunk.length;
152-
if (addToFront) {
153-
state.buffer.unshift(chunk);
154-
} else {
150+
if (!addToFront)
155151
state.reading = false;
156-
state.buffer.push(chunk);
157-
}
158152

159-
if (state.needReadable)
160-
emitReadable(stream);
153+
// if we want the data now, just emit it.
154+
if (state.flowing && state.length === 0 && !state.sync) {
155+
stream.emit('data', chunk);
156+
stream.read(0);
157+
} else {
158+
// update the buffer info.
159+
state.length += state.objectMode ? 1 : chunk.length;
160+
if (addToFront)
161+
state.buffer.unshift(chunk);
162+
else
163+
state.buffer.push(chunk);
164+
165+
if (state.needReadable)
166+
emitReadable(stream);
167+
}
161168

162169
maybeReadMore(stream, state);
163170
}

0 commit comments

Comments
 (0)