Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
streams: optimize creation#50337
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
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
nxtedition:stream-creation-perf-1Oct 25, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
streams: optimize creation #50337
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -44,6 +44,7 @@ const EE = require('events'); | ||
| const Stream = require('internal/streams/legacy').Stream; | ||
| const { Buffer } = require('buffer'); | ||
| const destroyImpl = require('internal/streams/destroy'); | ||
| const { kOnConstructed } = require('internal/streams/utils'); | ||
| const { | ||
| addAbortSignal, | ||
| @@ -290,20 +291,15 @@ ObjectDefineProperties(WritableState.prototype, { | ||
| }); | ||
| 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; | ||
| // Bit map field to store WritableState more effciently with 1 bit per field | ||
| // instead of a V8 slot per field. | ||
| this[kState] = kSync | kConstructed | kEmitClose | kAutoDestroy; | ||
| if (options && options.objectMode) this[kState] |= kObjectMode; | ||
| if (isDuplex && options && options.writableObjectMode) this[kState] |= kObjectMode; | ||
| if (options && options.objectMode) | ||
| this[kState] |= kObjectMode; | ||
| if (isDuplex && options && options.writableObjectMode) | ||
| this[kState] |= kObjectMode; | ||
ronag marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // The point at which write() starts returning false | ||
| // Note: 0 is a valid value, means that we always return false if | ||
| @@ -323,7 +319,7 @@ function WritableState(options, stream, isDuplex) { | ||
| // Crypto is kind of old and crusty. Historically, its default string | ||
| // encoding is 'binary' so we have to make this configurable. | ||
| // Everything else in the universe uses 'utf8', though. | ||
| const defaultEncoding = options?.defaultEncoding; | ||
| const defaultEncoding = options ? options.defaultEncoding : null; | ||
ronag marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (defaultEncoding == null || defaultEncoding === 'utf8' || defaultEncoding === 'utf-8') { | ||
| this[kState] |= kDefaultUTF8Encoding; | ||
| } else if (Buffer.isEncoding(defaultEncoding)) { | ||
| @@ -372,23 +368,21 @@ ObjectDefineProperty(WritableState.prototype, 'bufferedRequestCount', { | ||
| }, | ||
| }); | ||
| function Writable(options) { | ||
| // Writable ctor is applied to Duplexes, too. | ||
| // `realHasInstance` is necessary because using plain `instanceof` | ||
| // would return false, as no `_writableState` property is attached. | ||
| // Trying to use the custom `instanceof` for Writable here will also break the | ||
| // Node.js LazyTransform implementation, which has a non-trivial getter for | ||
| // `_writableState` that would lead to infinite recursion. | ||
| WritableState.prototype[kOnConstructed] = function onConstructed(stream) { | ||
| if ((this[kState] & kWriting) === 0) { | ||
| clearBuffer(stream, this); | ||
| } | ||
| // Checking for a Stream.Duplex instance is faster here instead of inside | ||
| // the WritableState constructor, at least with V8 6.5. | ||
| const isDuplex = (this instanceof Stream.Duplex); | ||
| if ((this[kState] & kEnding) !== 0) { | ||
| finishMaybe(stream, this); | ||
| } | ||
| }; | ||
| if (!isDuplex && !FunctionPrototypeSymbolHasInstance(Writable, this)) | ||
| function Writable(options) { | ||
| if (!(this instanceof Writable)) | ||
| return new Writable(options); | ||
| this._writableState = new WritableState(options, this, isDuplex); | ||
| this._writableState = new WritableState(options, this, false); | ||
| if (options) { | ||
| if (typeof options.write === 'function') | ||
| @@ -412,17 +406,11 @@ function Writable(options) { | ||
| Stream.call(this, options); | ||
| destroyImpl.construct(this, () => { | ||
| const state = this._writableState; | ||
| if ((state[kState] & kWriting) === 0) { | ||
| clearBuffer(this, state); | ||
| } | ||
| if ((state[kState] & kEnding) !== 0) { | ||
| finishMaybe(this, state); | ||
| } | ||
| }); | ||
| if (this._construct != null) { | ||
| destroyImpl.construct(this, () => { | ||
| this._writableState[kOnConstructed](this); | ||
| }); | ||
| } | ||
| } | ||
| ObjectDefineProperty(Writable, SymbolHasInstance, { | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.