From 600c08d197a8d60c469ac12955620646f6ca3371 Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:17:34 +0300 Subject: [PATCH] stream: improve WebStreams performance PR-URL: https://github.com/nodejs/node/pull/49089 Reviewed-By: Matteo Collina Reviewed-By: Yagiz Nizipli Reviewed-By: Benjamin Gruenbaum --- lib/internal/webstreams/readablestream.js | 66 ++++++++-------------- lib/internal/webstreams/transformstream.js | 20 +++---- lib/internal/webstreams/writablestream.js | 17 ++---- 3 files changed, 39 insertions(+), 64 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index a994a33f8b12f3..28a1a60b1dac2f 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -140,6 +140,7 @@ const kChunk = Symbol('kChunk'); const kError = Symbol('kError'); const kPull = Symbol('kPull'); const kRelease = Symbol('kRelease'); +const kSkipThrow = Symbol('kSkipThrow'); let releasedError; let releasingError; @@ -676,8 +677,10 @@ TransferredReadableStream.prototype[kDeserialize] = () => {}; class ReadableStreamBYOBRequest { [kType] = 'ReadableStreamBYOBRequest'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -759,17 +762,14 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, { }); function createReadableStreamBYOBRequest(controller, view) { - return ReflectConstruct( - function() { - this[kType] = 'ReadableStreamBYOBRequest'; - this[kState] = { - controller, - view, - }; - }, - [], - ReadableStreamBYOBRequest, - ); + const stream = new ReadableStreamBYOBRequest(kSkipThrow); + + stream[kState] = { + controller, + view, + }; + + return stream; } class DefaultReadRequest { @@ -1019,9 +1019,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, { class ReadableStreamDefaultController { [kType] = 'ReadableStreamDefaultController'; + [kState] = {}; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -1077,22 +1080,14 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name), }); -function createReadableStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'ReadableStreamDefaultController'; - this[kState] = {}; - }, - [], - ReadableStreamDefaultController, - ); -} - class ReadableByteStreamController { [kType] = 'ReadableByteStreamController'; + [kState] = {}; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -1203,17 +1198,6 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name), }); -function createReadableByteStreamController() { - return ReflectConstruct( - function() { - this[kType] = 'ReadableByteStreamController'; - this[kState] = {}; - }, - [], - ReadableByteStreamController, - ); -} - function createTeeReadableStream(start, pull, cancel) { return ReflectConstruct( function() { @@ -2416,7 +2400,7 @@ function setupReadableStreamDefaultControllerFromSource( source, highWaterMark, sizeAlgorithm) { - const controller = createReadableStreamDefaultController(); + const controller = new ReadableStreamDefaultController(kSkipThrow); const start = source?.start; const pull = source?.pull; const cancel = source?.cancel; @@ -3214,7 +3198,7 @@ function setupReadableByteStreamControllerFromSource( stream, source, highWaterMark) { - const controller = createReadableByteStreamController(); + const controller = new ReadableByteStreamController(kSkipThrow); const start = source?.start; const pull = source?.pull; const cancel = source?.cancel; diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index 3a7fbebc042a22..bcccf90af91a0f 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -8,6 +8,7 @@ const { PromiseResolve, ReflectConstruct, SymbolToStringTag, + Symbol, } = primordials; const { @@ -65,6 +66,8 @@ const { const assert = require('internal/assert'); +const kSkipThrow = Symbol('kSkipThrow'); + const getNonWritablePropertyDescriptor = (value) => { return { __proto__: null, @@ -269,8 +272,10 @@ TransferredTransformStream.prototype[kDeserialize] = () => {}; class TransformStreamDefaultController { [kType] = 'TransformStreamDefaultController'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -331,15 +336,6 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name), }); -function createTransformStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'TransformStreamDefaultController'; - }, - [], - TransformStreamDefaultController); -} - const isTransformStream = isBrandCheck('TransformStream'); const isTransformStreamDefaultController = @@ -454,7 +450,7 @@ function setupTransformStreamDefaultController( function setupTransformStreamDefaultControllerFromTransformer( stream, transformer) { - const controller = createTransformStreamDefaultController(); + const controller = new TransformStreamDefaultController(kSkipThrow); const transform = transformer?.transform || defaultTransformAlgorithm; const flush = transformer?.flush || nonOpFlush; const transformAlgorithm = diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index b0eb5f20abb80e..44810d533c7092 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -81,6 +81,7 @@ const assert = require('internal/assert'); const kAbort = Symbol('kAbort'); const kCloseSentinel = Symbol('kCloseSentinel'); const kError = Symbol('kError'); +const kSkipThrow = Symbol('kSkipThrow'); let releasedError; @@ -523,8 +524,10 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, { class WritableStreamDefaultController { [kType] = 'WritableStreamDefaultController'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } [kAbort](reason) { @@ -570,14 +573,6 @@ ObjectDefineProperties(WritableStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name), }); -function createWritableStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'WritableStreamDefaultController'; - }, - [], WritableStreamDefaultController); -} - const isWritableStream = isBrandCheck('WritableStream'); const isWritableStreamDefaultWriter = @@ -1234,7 +1229,7 @@ function setupWritableStreamDefaultControllerFromSink( sink, highWaterMark, sizeAlgorithm) { - const controller = createWritableStreamDefaultController(); + const controller = new WritableStreamDefaultController(kSkipThrow); const start = sink?.start; const write = sink?.write; const close = sink?.close;