Browse Source

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.
v0.11.6-release
isaacs 11 years ago
parent
commit
c0e70354db
  1. 15
      lib/_stream_readable.js

15
lib/_stream_readable.js

@ -147,17 +147,24 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
if (state.decoder && !addToFront && !encoding)
chunk = state.decoder.write(chunk);
if (!addToFront)
state.reading = false;
// 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) {
if (addToFront)
state.buffer.unshift(chunk);
} else {
state.reading = false;
else
state.buffer.push(chunk);
}
if (state.needReadable)
emitReadable(stream);
}
maybeReadMore(stream, state);
}

Loading…
Cancel
Save