Skip to content

Commit 9edaaf7

Browse files
ronagdebadree25
authored andcommittedApr 15, 2024
stream: avoid unnecessary drain for sync stream
PR-URL: nodejs#50014 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent c1ed261 commit 9edaaf7

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed
 

‎lib/internal/streams/transform.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ Transform.prototype._write = function(chunk, encoding, callback) {
182182
this.push(val);
183183
}
184184

185-
if (
185+
if (rState.ended) {
186+
// If user has called this.push(null) we have to
187+
// delay the callback to properly progate the new
188+
// state.
189+
process.nextTick(callback);
190+
} else if (
186191
wState.ended || // Backwards compat.
187192
length === rState.length || // Backwards compat.
188193
rState.length < rState.highWaterMark

‎lib/internal/streams/writable.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,6 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
517517

518518
state.length += len;
519519

520-
// stream._write resets state.length
521-
const ret = state.length < state.highWaterMark;
522-
523-
// We must ensure that previous needDrain will not be reset to false.
524-
if (!ret) {
525-
state.state |= kNeedDrain;
526-
}
527-
528520
if ((state.state & (kWriting | kErrored | kCorked | kConstructed)) !== kConstructed) {
529521
state.buffered.push({ chunk, encoding, callback });
530522
state.state |= kHasBuffer;
@@ -544,6 +536,12 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
544536
state.state &= ~kSync;
545537
}
546538

539+
const ret = state.length < state.highWaterMark;
540+
541+
if (!ret) {
542+
state.state |= kNeedDrain;
543+
}
544+
547545
// Return false if errored or destroyed in order to break
548546
// any synchronous while(stream.write(data)) loops.
549547
return ret && (state.state & (kDestroyed | kErrored)) === 0;

0 commit comments

Comments
 (0)