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
  • Loading branch information
ronag committed Oct 2, 2023
1 parent 85c09f1 commit f7e2c54
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const kWriteCb = 1 << 26;
const kExpectWriteCb = 1 << 27;
const kAfterWriteTickInfo = 1 << 28;
const kAfterWritePending = 1 << 29;
const kIsDuplex = 1 << 30;

// TODO(benjamingr) it is likely slower to do it this way than with free functions
function makeBitMapDescriptor(bit) {
Expand Down Expand Up @@ -286,6 +287,7 @@ function WritableState(options, stream, isDuplex) {

if (options && options.objectMode) this.state |= kObjectMode;
if (isDuplex && options && options.writableObjectMode) this.state |= kObjectMode;
if (isDuplex) this.state |= kIsDuplex;

// The point at which write() starts returning false
// Note: 0 is a valid value, means that we always return false if
Expand Down Expand Up @@ -513,13 +515,7 @@ 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;
}
const prevLen = state.length;

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

const ret = (
((state.state & kIsDuplex) === 0 || stream._readableState.ended !== true) ?
state.length < state.highWaterMark :
prevLen < 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 f7e2c54

Please sign in to comment.