Skip to content

Commit 654d196

Browse files
committed
streams: make pipe data fast path state lazy
1 parent 9f0dfce commit 654d196

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

lib/internal/streams/readable.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ const kFlowing = 1 << 24;
132132
const kHasPaused = 1 << 25;
133133
const kPaused = 1 << 26;
134134
const kDataListening = 1 << 27;
135+
const kHasPipeData = 1 << 28;
135136

136137
// TODO(benjamingr) it is likely slower to do it this way than with free functions
137138
function makeBitMapDescriptor(bit) {
@@ -290,7 +291,6 @@ function ReadableState(options, stream, isDuplex) {
290291
this.bufferIndex = 0;
291292
this.length = 0;
292293
this.pipes = [];
293-
this[kPipeData] = null;
294294

295295
// Should close be emitted on destroy. Defaults to true.
296296
if (options && options.emitClose === false) this[kState] &= ~kEmitClose;
@@ -799,7 +799,8 @@ Readable.prototype.read = function(n) {
799799

800800
function emitData(stream, state, chunk) {
801801
state[kState] |= kDataEmitted;
802-
if (stream._events.data === state[kPipeData]) {
802+
if ((state[kState] & kHasPipeData) !== 0 &&
803+
stream._events.data === state[kPipeData]) {
803804
state[kPipeData](chunk);
804805
} else {
805806
stream.emit('data', chunk);
@@ -986,7 +987,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
986987
src.removeListener('end', unpipe);
987988
src.removeListener('data', ondata);
988989
if (state[kPipeData] === ondata)
989-
state[kPipeData] = null;
990+
state[kState] &= ~kHasPipeData;
990991

991992
cleanedUp = true;
992993

@@ -1027,8 +1028,10 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
10271028
}
10281029

10291030
src.on('data', ondata);
1030-
if (src._events.data === ondata)
1031+
if (src._events.data === ondata) {
10311032
state[kPipeData] = ondata;
1033+
state[kState] |= kHasPipeData;
1034+
}
10321035
function ondata(chunk) {
10331036
debug('ondata');
10341037
try {

0 commit comments

Comments
 (0)