Skip to content

Commit 600c08d

Browse files
rluvatonRafaelGSS
authored andcommittedAug 17, 2023
stream: improve WebStreams performance
PR-URL: #49089 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent eb12158 commit 600c08d

File tree

3 files changed

+39
-64
lines changed

3 files changed

+39
-64
lines changed
 

‎lib/internal/webstreams/readablestream.js

+25-41
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const kChunk = Symbol('kChunk');
140140
const kError = Symbol('kError');
141141
const kPull = Symbol('kPull');
142142
const kRelease = Symbol('kRelease');
143+
const kSkipThrow = Symbol('kSkipThrow');
143144

144145
let releasedError;
145146
let releasingError;
@@ -676,8 +677,10 @@ TransferredReadableStream.prototype[kDeserialize] = () => {};
676677
class ReadableStreamBYOBRequest {
677678
[kType] = 'ReadableStreamBYOBRequest';
678679

679-
constructor() {
680-
throw new ERR_ILLEGAL_CONSTRUCTOR();
680+
constructor(skipThrowSymbol = undefined) {
681+
if (skipThrowSymbol !== kSkipThrow) {
682+
throw new ERR_ILLEGAL_CONSTRUCTOR();
683+
}
681684
}
682685

683686
/**
@@ -759,17 +762,14 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, {
759762
});
760763

761764
function createReadableStreamBYOBRequest(controller, view) {
762-
return ReflectConstruct(
763-
function() {
764-
this[kType] = 'ReadableStreamBYOBRequest';
765-
this[kState] = {
766-
controller,
767-
view,
768-
};
769-
},
770-
[],
771-
ReadableStreamBYOBRequest,
772-
);
765+
const stream = new ReadableStreamBYOBRequest(kSkipThrow);
766+
767+
stream[kState] = {
768+
controller,
769+
view,
770+
};
771+
772+
return stream;
773773
}
774774

775775
class DefaultReadRequest {
@@ -1019,9 +1019,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, {
10191019

10201020
class ReadableStreamDefaultController {
10211021
[kType] = 'ReadableStreamDefaultController';
1022+
[kState] = {};
10221023

1023-
constructor() {
1024-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1024+
constructor(skipThrowSymbol = undefined) {
1025+
if (skipThrowSymbol !== kSkipThrow) {
1026+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1027+
}
10251028
}
10261029

10271030
/**
@@ -1077,22 +1080,14 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, {
10771080
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name),
10781081
});
10791082

1080-
function createReadableStreamDefaultController() {
1081-
return ReflectConstruct(
1082-
function() {
1083-
this[kType] = 'ReadableStreamDefaultController';
1084-
this[kState] = {};
1085-
},
1086-
[],
1087-
ReadableStreamDefaultController,
1088-
);
1089-
}
1090-
10911083
class ReadableByteStreamController {
10921084
[kType] = 'ReadableByteStreamController';
1085+
[kState] = {};
10931086

1094-
constructor() {
1095-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1087+
constructor(skipThrowSymbol = undefined) {
1088+
if (skipThrowSymbol !== kSkipThrow) {
1089+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1090+
}
10961091
}
10971092

10981093
/**
@@ -1203,17 +1198,6 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, {
12031198
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name),
12041199
});
12051200

1206-
function createReadableByteStreamController() {
1207-
return ReflectConstruct(
1208-
function() {
1209-
this[kType] = 'ReadableByteStreamController';
1210-
this[kState] = {};
1211-
},
1212-
[],
1213-
ReadableByteStreamController,
1214-
);
1215-
}
1216-
12171201
function createTeeReadableStream(start, pull, cancel) {
12181202
return ReflectConstruct(
12191203
function() {
@@ -2416,7 +2400,7 @@ function setupReadableStreamDefaultControllerFromSource(
24162400
source,
24172401
highWaterMark,
24182402
sizeAlgorithm) {
2419-
const controller = createReadableStreamDefaultController();
2403+
const controller = new ReadableStreamDefaultController(kSkipThrow);
24202404
const start = source?.start;
24212405
const pull = source?.pull;
24222406
const cancel = source?.cancel;
@@ -3214,7 +3198,7 @@ function setupReadableByteStreamControllerFromSource(
32143198
stream,
32153199
source,
32163200
highWaterMark) {
3217-
const controller = createReadableByteStreamController();
3201+
const controller = new ReadableByteStreamController(kSkipThrow);
32183202
const start = source?.start;
32193203
const pull = source?.pull;
32203204
const cancel = source?.cancel;

‎lib/internal/webstreams/transformstream.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
PromiseResolve,
99
ReflectConstruct,
1010
SymbolToStringTag,
11+
Symbol,
1112
} = primordials;
1213

1314
const {
@@ -65,6 +66,8 @@ const {
6566

6667
const assert = require('internal/assert');
6768

69+
const kSkipThrow = Symbol('kSkipThrow');
70+
6871
const getNonWritablePropertyDescriptor = (value) => {
6972
return {
7073
__proto__: null,
@@ -269,8 +272,10 @@ TransferredTransformStream.prototype[kDeserialize] = () => {};
269272
class TransformStreamDefaultController {
270273
[kType] = 'TransformStreamDefaultController';
271274

272-
constructor() {
273-
throw new ERR_ILLEGAL_CONSTRUCTOR();
275+
constructor(skipThrowSymbol = undefined) {
276+
if (skipThrowSymbol !== kSkipThrow) {
277+
throw new ERR_ILLEGAL_CONSTRUCTOR();
278+
}
274279
}
275280

276281
/**
@@ -331,15 +336,6 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, {
331336
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name),
332337
});
333338

334-
function createTransformStreamDefaultController() {
335-
return ReflectConstruct(
336-
function() {
337-
this[kType] = 'TransformStreamDefaultController';
338-
},
339-
[],
340-
TransformStreamDefaultController);
341-
}
342-
343339
const isTransformStream =
344340
isBrandCheck('TransformStream');
345341
const isTransformStreamDefaultController =
@@ -454,7 +450,7 @@ function setupTransformStreamDefaultController(
454450
function setupTransformStreamDefaultControllerFromTransformer(
455451
stream,
456452
transformer) {
457-
const controller = createTransformStreamDefaultController();
453+
const controller = new TransformStreamDefaultController(kSkipThrow);
458454
const transform = transformer?.transform || defaultTransformAlgorithm;
459455
const flush = transformer?.flush || nonOpFlush;
460456
const transformAlgorithm =

‎lib/internal/webstreams/writablestream.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const assert = require('internal/assert');
8181
const kAbort = Symbol('kAbort');
8282
const kCloseSentinel = Symbol('kCloseSentinel');
8383
const kError = Symbol('kError');
84+
const kSkipThrow = Symbol('kSkipThrow');
8485

8586
let releasedError;
8687

@@ -523,8 +524,10 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, {
523524
class WritableStreamDefaultController {
524525
[kType] = 'WritableStreamDefaultController';
525526

526-
constructor() {
527-
throw new ERR_ILLEGAL_CONSTRUCTOR();
527+
constructor(skipThrowSymbol = undefined) {
528+
if (skipThrowSymbol !== kSkipThrow) {
529+
throw new ERR_ILLEGAL_CONSTRUCTOR();
530+
}
528531
}
529532

530533
[kAbort](reason) {
@@ -570,14 +573,6 @@ ObjectDefineProperties(WritableStreamDefaultController.prototype, {
570573
[SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name),
571574
});
572575

573-
function createWritableStreamDefaultController() {
574-
return ReflectConstruct(
575-
function() {
576-
this[kType] = 'WritableStreamDefaultController';
577-
},
578-
[], WritableStreamDefaultController);
579-
}
580-
581576
const isWritableStream =
582577
isBrandCheck('WritableStream');
583578
const isWritableStreamDefaultWriter =
@@ -1234,7 +1229,7 @@ function setupWritableStreamDefaultControllerFromSink(
12341229
sink,
12351230
highWaterMark,
12361231
sizeAlgorithm) {
1237-
const controller = createWritableStreamDefaultController();
1232+
const controller = new WritableStreamDefaultController(kSkipThrow);
12381233
const start = sink?.start;
12391234
const write = sink?.write;
12401235
const close = sink?.close;

0 commit comments

Comments
 (0)
Please sign in to comment.