Skip to content

stream: refactor clearBuffer and writableState #45682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 7 additions & 13 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ function nop() {}
const kOnFinished = Symbol('kOnFinished');

function WritableState(options, stream, isDuplex) {
// Duplex streams are both readable and writable, but share
// the same options object.
// However, some cases require setting options to different
// values for the readable and the writable sides of the duplex stream,
// e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
if (typeof isDuplex !== 'boolean')
isDuplex = stream instanceof Stream.Duplex;

// Object stream flag to indicate whether or not this stream
// contains buffers or objects.
this.objectMode = !!(options && options.objectMode);
Expand Down Expand Up @@ -537,16 +529,18 @@ function clearBuffer(stream, state) {
return;
}

state.bufferProcessing = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary length operation was done before state.bufferProcessing assignment is done. This change eliminates that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see it. This is actually slower for the case where bufferedLength is 0... since it unnecessarily sets and unsets bufferProcessing.


const { buffered, bufferedIndex, objectMode } = state;
const bufferedLength = buffered.length - bufferedIndex;

if (!bufferedLength) {
if (bufferedLength === 0) {
state.bufferProcessing = false;
return;
}

let i = bufferedIndex;

state.bufferProcessing = true;
if (bufferedLength > 1 && stream._writev) {
state.pendingcb -= bufferedLength - 1;

Expand All @@ -565,12 +559,12 @@ function clearBuffer(stream, state) {

resetBuffer(state);
} else {
do {
for (; i < buffered.length && !state.writing; i++) {
const { chunk, encoding, callback } = buffered[i];
buffered[i++] = null;
buffered[i] = null;
const len = objectMode ? 1 : chunk.length;
doWrite(stream, state, false, len, chunk, encoding, callback);
} while (i < buffered.length && !state.writing);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary change


if (i === buffered.length) {
resetBuffer(state);
Expand Down