Skip to content

Commit

Permalink
stream: avoid unnecessary drain for sync stream
Browse files Browse the repository at this point in the history
PR-URL: #50014
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
ronag authored Oct 4, 2023
1 parent 95b8f5d commit 557044a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/internal/streams/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ Transform.prototype._write = function(chunk, encoding, callback) {
this.push(val);
}

if (
if (rState.ended) {
// If user has called this.push(null) we have to
// delay the callback to properly progate the new
// state.
process.nextTick(callback);
} else if (
wState.ended || // Backwards compat.
length === rState.length || // Backwards compat.
rState.length < rState.highWaterMark
Expand Down
14 changes: 6 additions & 8 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,6 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {

state.length += len;

// stream._write resets state.length
const ret = state.length < state.highWaterMark;

// We must ensure that previous needDrain will not be reset to false.
if (!ret) {
state.state |= kNeedDrain;
}

if ((state.state & (kWriting | kErrored | kCorked | kConstructed)) !== kConstructed) {
state.buffered.push({ chunk, encoding, callback });
state.state |= kHasBuffer;
Expand All @@ -544,6 +536,12 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
state.state &= ~kSync;
}

const ret = state.length < state.highWaterMark;

if (!ret) {
state.state |= kNeedDrain;
}

// Return false if errored or destroyed in order to break
// any synchronous while(stream.write(data)) loops.
return ret && (state.state & (kDestroyed | kErrored)) === 0;
Expand Down

0 comments on commit 557044a

Please sign in to comment.