Skip to content

Commit

Permalink
stream: Short-circuit buffer pushes when flowing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs committed Aug 8, 2013
1 parent 967b5db commit c0e7035
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,24 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
if (state.decoder && !addToFront && !encoding)
chunk = state.decoder.write(chunk);

// update the buffer info.
state.length += state.objectMode ? 1 : chunk.length;
if (addToFront) {
state.buffer.unshift(chunk);
} else {
if (!addToFront)
state.reading = false;
state.buffer.push(chunk);
}

if (state.needReadable)
emitReadable(stream);
// if we want the data now, just emit it.
if (state.flowing && state.length === 0 && !state.sync) {
stream.emit('data', chunk);
stream.read(0);
} else {
// update the buffer info.
state.length += state.objectMode ? 1 : chunk.length;
if (addToFront)
state.buffer.unshift(chunk);
else
state.buffer.push(chunk);

if (state.needReadable)
emitReadable(stream);
}

maybeReadMore(stream, state);
}
Expand Down

0 comments on commit c0e7035

Please sign in to comment.