stream: optimize creation · nodejs/node@6206957 · GitHub
Skip to content

Commit 6206957

Browse files
ronagUlisesGascon
authored andcommitted
stream: optimize creation
Refs: nodejs/performance#79 PR-URL: #50337 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4aaaff4 commit 6206957

4 files changed

Lines changed: 83 additions & 57 deletions

File tree

lib/internal/streams/duplex.js

Lines changed: 40 additions & 2 deletions

lib/internal/streams/readable.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171
AbortError,
7272
} = require('internal/errors');
7373
const { validateObject } = require('internal/validators');
74+
const { kOnConstructed } = require('internal/streams/utils');
7475

7576
const kState = Symbol('kState');
7677

@@ -251,20 +252,14 @@ ObjectDefineProperties(ReadableState.prototype, {
251252

252253

253254
function ReadableState(options, stream, isDuplex) {
254-
// Duplex streams are both readable and writable, but share
255-
// the same options object.
256-
// However, some cases require setting options to different
257-
// values for the readable and the writable sides of the duplex stream.
258-
// These options can be provided separately as readableXXX and writableXXX.
259-
if (typeof isDuplex !== 'boolean')
260-
isDuplex = stream instanceof Stream.Duplex;
261-
262255
// Bit map field to store ReadableState more effciently with 1 bit per field
263256
// instead of a V8 slot per field.
264257
this[kState] = kEmitClose | kAutoDestroy | kConstructed | kSync;
258+
265259
// Object stream flag. Used to make read(n) ignore n and to
266260
// make all the buffer merging and length checks go away.
267-
if (options && options.objectMode) this[kState] |= kObjectMode;
261+
if (options && options.objectMode)
262+
this[kState] |= kObjectMode;
268263

269264
if (isDuplex && options && options.readableObjectMode)
270265
this[kState] |= kObjectMode;
@@ -310,16 +305,17 @@ function ReadableState(options, stream, isDuplex) {
310305
}
311306
}
312307

308+
ReadableState.prototype[kOnConstructed] = function onConstructed(stream) {
309+
if ((this[kState] & kNeedReadable) !== 0) {
310+
maybeReadMore(stream, this);
311+
}
312+
};
313313

314314
function Readable(options) {
315315
if (!(this instanceof Readable))
316316
return new Readable(options);
317317

318-
// Checking for a Stream.Duplex instance is faster here instead of inside
319-
// the ReadableState constructor, at least with V8 6.5.
320-
const isDuplex = this instanceof Stream.Duplex;
321-
322-
this._readableState = new ReadableState(options, this, isDuplex);
318+
this._readableState = new ReadableState(options, this, false);
323319

324320
if (options) {
325321
if (typeof options.read === 'function')
@@ -331,17 +327,17 @@ function Readable(options) {
331327
if (typeof options.construct === 'function')
332328
this._construct = options.construct;
333329

334-
if (options.signal && !isDuplex)
330+
if (options.signal)
335331
addAbortSignal(options.signal, this);
336332
}
337333

338334
Stream.call(this, options);
339335

340-
destroyImpl.construct(this, () => {
341-
if (this._readableState.needReadable) {
342-
maybeReadMore(this, this._readableState);
343-
}
344-
});
336+
if (this._construct != null) {
337+
destroyImpl.construct(this, () => {
338+
this._readableState[kOnConstructed](this);
339+
});
340+
}
345341
}
346342

347343
Readable.prototype.destroy = destroyImpl.destroy;

lib/internal/streams/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
SymbolAsyncIterator,
55
SymbolIterator,
66
SymbolFor,
7+
Symbol,
78
} = primordials;
89

910
// We need to use SymbolFor to make these globally available
@@ -16,6 +17,8 @@ const kIsReadable = SymbolFor('nodejs.stream.readable');
1617
const kIsWritable = SymbolFor('nodejs.stream.writable');
1718
const kIsDisturbed = SymbolFor('nodejs.stream.disturbed');
1819

20+
const kOnConstructed = Symbol('kOnConstructed');
21+
1922
const kIsClosedPromise = SymbolFor('nodejs.webstream.isClosedPromise');
2023
const kControllerErrorFunction = SymbolFor('nodejs.webstream.controllerErrorFunction');
2124

@@ -303,6 +306,7 @@ function isErrored(stream) {
303306
}
304307

305308
module.exports = {
309+
kOnConstructed,
306310
isDestroyed,
307311
kIsDestroyed,
308312
isDisturbed,

lib/internal/streams/writable.js

Lines changed: 23 additions & 35 deletions

0 commit comments

Comments
 (0)