Skip to content

Commit d585d13

Browse files
rluvatontargos
authored andcommitted
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 acd9872 commit d585d13

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
@@ -138,6 +138,7 @@ const kChunk = Symbol('kChunk');
138138
const kError = Symbol('kError');
139139
const kPull = Symbol('kPull');
140140
const kRelease = Symbol('kRelease');
141+
const kSkipThrow = Symbol('kSkipThrow');
141142

142143
let releasedError;
143144
let releasingError;
@@ -670,8 +671,10 @@ TransferredReadableStream.prototype[kDeserialize] = () => {};
670671
class ReadableStreamBYOBRequest {
671672
[kType] = 'ReadableStreamBYOBRequest';
672673

673-
constructor() {
674-
throw new ERR_ILLEGAL_CONSTRUCTOR();
674+
constructor(skipThrowSymbol = undefined) {
675+
if (skipThrowSymbol !== kSkipThrow) {
676+
throw new ERR_ILLEGAL_CONSTRUCTOR();
677+
}
675678
}
676679

677680
/**
@@ -753,17 +756,14 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, {
753756
});
754757

755758
function createReadableStreamBYOBRequest(controller, view) {
756-
return ReflectConstruct(
757-
function() {
758-
this[kType] = 'ReadableStreamBYOBRequest';
759-
this[kState] = {
760-
controller,
761-
view,
762-
};
763-
},
764-
[],
765-
ReadableStreamBYOBRequest,
766-
);
759+
const stream = new ReadableStreamBYOBRequest(kSkipThrow);
760+
761+
stream[kState] = {
762+
controller,
763+
view,
764+
};
765+
766+
return stream;
767767
}
768768

769769
class DefaultReadRequest {
@@ -1013,9 +1013,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, {
10131013

10141014
class ReadableStreamDefaultController {
10151015
[kType] = 'ReadableStreamDefaultController';
1016+
[kState] = {};
10161017

1017-
constructor() {
1018-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1018+
constructor(skipThrowSymbol = undefined) {
1019+
if (skipThrowSymbol !== kSkipThrow) {
1020+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1021+
}
10191022
}
10201023

10211024
/**
@@ -1071,22 +1074,14 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, {
10711074
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name),
10721075
});
10731076

1074-
function createReadableStreamDefaultController() {
1075-
return ReflectConstruct(
1076-
function() {
1077-
this[kType] = 'ReadableStreamDefaultController';
1078-
this[kState] = {};
1079-
},
1080-
[],
1081-
ReadableStreamDefaultController,
1082-
);
1083-
}
1084-
10851077
class ReadableByteStreamController {
10861078
[kType] = 'ReadableByteStreamController';
1079+
[kState] = {};
10871080

1088-
constructor() {
1089-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1081+
constructor(skipThrowSymbol = undefined) {
1082+
if (skipThrowSymbol !== kSkipThrow) {
1083+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1084+
}
10901085
}
10911086

10921087
/**
@@ -1197,17 +1192,6 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, {
11971192
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name),
11981193
});
11991194

1200-
function createReadableByteStreamController() {
1201-
return ReflectConstruct(
1202-
function() {
1203-
this[kType] = 'ReadableByteStreamController';
1204-
this[kState] = {};
1205-
},
1206-
[],
1207-
ReadableByteStreamController,
1208-
);
1209-
}
1210-
12111195
function createTeeReadableStream(start, pull, cancel) {
12121196
return ReflectConstruct(
12131197
function() {
@@ -2357,7 +2341,7 @@ function setupReadableStreamDefaultControllerFromSource(
23572341
source,
23582342
highWaterMark,
23592343
sizeAlgorithm) {
2360-
const controller = createReadableStreamDefaultController();
2344+
const controller = new ReadableStreamDefaultController(kSkipThrow);
23612345
const start = source?.start;
23622346
const pull = source?.pull;
23632347
const cancel = source?.cancel;
@@ -3155,7 +3139,7 @@ function setupReadableByteStreamControllerFromSource(
31553139
stream,
31563140
source,
31573141
highWaterMark) {
3158-
const controller = createReadableByteStreamController();
3142+
const controller = new ReadableByteStreamController(kSkipThrow);
31593143
const start = source?.start;
31603144
const pull = source?.pull;
31613145
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)