Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions benchmark/streams/pipe-backpressure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const { Readable, Writable } = require('stream');

const bench = common.createBenchmark(main, {
n: [5e6],
});

function main({ n }) {
const b = Buffer.alloc(1024);
const r = new Readable();
const w = new Writable();

let i = 0;

r._read = () => r.push(i++ === n ? null : b);
w._write = (data, enc, cb) => queueMicrotask(cb);

bench.start();

r.pipe(w);
w.on('finish', () => bench.end(n));
}
25 changes: 14 additions & 11 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@

if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) &&
(state[kState] & kDataListening) !== 0) {
src.resume();
resume(stream, state, dest.listenerCount('drain') <= 1);

Check failure on line 1094 in lib/internal/streams/readable.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'stream' is not defined
}
};
}
Expand Down Expand Up @@ -1232,7 +1232,10 @@
// pause() and resume() are remnants of the legacy readable stream API
// If the user uses them, then switch into old mode.
Readable.prototype.resume = function() {
const state = this._readableState;
return resume(this, this._readableState, false);
};

function resume(stream, state, sync) {
if ((state[kState] & kFlowing) === 0) {
debug('resume');
// We flow only if there is no one listening
Expand All @@ -1244,18 +1247,18 @@
} else {
state[kState] &= ~kFlowing;
}
resume(this, state);
if ((state[kState] & kResumeScheduled) === 0) {
if (sync) {
resume_(stream, state, false);
} else {
state[kState] |= kResumeScheduled;
process.nextTick(resume_, stream, state);
}
}
}
state[kState] |= kHasPaused;
state[kState] &= ~kPaused;
return this;
};

function resume(stream, state) {
if ((state[kState] & kResumeScheduled) === 0) {
state[kState] |= kResumeScheduled;
process.nextTick(resume_, stream, state);
}
return stream;
}

function resume_(stream, state) {
Expand Down
Loading