-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -537,16 +529,18 @@ function clearBuffer(stream, state) { | |
return; | ||
} | ||
|
||
state.bufferProcessing = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why this is better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary length operation was done before There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
@@ -565,12 +559,12 @@ function clearBuffer(stream, state) { | |
|
||
resetBuffer(state); | ||
} else { | ||
do { | ||
for (; i < buffered.length && !state.writing; i++) { | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary change |
||
|
||
if (i === buffered.length) { | ||
resetBuffer(state); | ||
|
Uh oh!
There was an error while loading. Please reload this page.