GeezerVison is showing github.com — stream: trim TransformStream construction costs · nodejs/node@78707d1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js +36-19Lines changed: 36 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,6 +111,7 @@ const { | 111 | 111 | extractSizeAlgorithm, | 112 | 112 | getNonWritablePropertyDescriptor, | 113 | 113 | isBrandCheck, | | 114 | + kDeferredStart, | 114 | 115 | kEmptyQueue, | 115 | 116 | kParkedAlgorithmResult, | 116 | 117 | kPendingPromise, | @@ -521,10 +522,11 @@ class ReadableStream { | 521 | 522 | [kTransfer]() { | 522 | 523 | if (!isReadableStream(this)) | 523 | 524 | throw new ERR_INVALID_THIS('ReadableStream'); | | 525 | + const transfer = readableStreamTransferState(this); | 524 | 526 | if (this.locked) { | 525 | | - this[kState].transfer.port1?.close(); | 526 | | - this[kState].transfer.port1 = undefined; | 527 | | - this[kState].transfer.port2 = undefined; | | 527 | + transfer.port1?.close(); | | 528 | + transfer.port1 = undefined; | | 529 | + transfer.port2 = undefined; | 528 | 530 | throw new DOMException( | 529 | 531 | 'Cannot transfer a locked ReadableStream', | 530 | 532 | 'DataCloneError'); | @@ -533,24 +535,23 @@ class ReadableStream { | 533 | 535 | const { | 534 | 536 | writable, | 535 | 537 | promise, | 536 | | - } = lazyTransfer().newCrossRealmWritableSink( | 537 | | - this, | 538 | | - this[kState].transfer.port1); | | 538 | + } = lazyTransfer().newCrossRealmWritableSink(this, transfer.port1); | 539 | 539 |
| 540 | | - this[kState].transfer.writable = writable; | 541 | | - this[kState].transfer.promise = promise; | | 540 | + transfer.writable = writable; | | 541 | + transfer.promise = promise; | 542 | 542 |
| 543 | 543 | return { | 544 | | - data: { port: this[kState].transfer.port2 }, | | 544 | + data: { port: transfer.port2 }, | 545 | 545 | deserializeInfo: | 546 | 546 | 'internal/webstreams/readablestream:TransferredReadableStream', | 547 | 547 | }; | 548 | 548 | } | 549 | 549 |
| 550 | 550 | [kTransferList]() { | 551 | 551 | const { port1, port2 } = new MessageChannel(); | 552 | | - this[kState].transfer.port1 = port1; | 553 | | - this[kState].transfer.port2 = port2; | | 552 | + const transfer = readableStreamTransferState(this); | | 553 | + transfer.port1 = port1; | | 554 | + transfer.port2 = port2; | 554 | 555 | return [ port2 ]; | 555 | 556 | } | 556 | 557 |
| @@ -1415,10 +1416,15 @@ class ReadableStreamState { | 1415 | 1416 | state = 'readable'; | 1416 | 1417 | storedError = undefined; | 1417 | 1418 | controller = undefined; | 1418 | | - transfer = new ReadableStreamTransferState(); | | 1419 | + // Materialized on transfer, see readableStreamTransferState(). | | 1420 | + transfer = undefined; | 1419 | 1421 | } | 1420 | 1422 | ObjectSetPrototypeOf(ReadableStreamState.prototype, null); | 1421 | 1423 |
| | 1424 | +function readableStreamTransferState(stream) { | | 1425 | + return stream[kState].transfer ??= new ReadableStreamTransferState(); | | 1426 | +} | | 1427 | + | 1422 | 1428 | function createReadableStreamState() { | 1423 | 1429 | return new ReadableStreamState(); | 1424 | 1430 | } | @@ -2961,7 +2967,7 @@ function readableStreamDefaultControllerError(controller, error) { | 2961 | 2967 |
| 2962 | 2968 | function readableStreamDefaultControllerCancelSteps(controller, reason) { | 2963 | 2969 | resetQueue(controller); | 2964 | | - const result = controller[kState].cancelAlgorithm(reason); | | 2970 | + const result = controller[kState].cancelAlgorithm(reason, controller); | 2965 | 2971 | readableStreamDefaultControllerClearAlgorithms(controller); | 2966 | 2972 | return result; | 2967 | 2973 | } | @@ -3019,10 +3025,16 @@ function setupReadableStreamDefaultController( | 3019 | 3025 | started: false, | 3020 | 3026 | sizeAlgorithm, | 3021 | 3027 | stream, | | 3028 | + // The TransformStream this controller is the source of, if any; its | | 3029 | + // shared source algorithms reach the stream through it. | | 3030 | + transformStream: undefined, | 3022 | 3031 | }; | 3023 | 3032 | stream[kState].controller = controller; | 3024 | 3033 |
| 3025 | 3034 | const startResult = startAlgorithm(); | | 3035 | + if (startResult === kDeferredStart) | | 3036 | + return; | | 3037 | + | 3026 | 3038 | // A non-thenable start result guarantees fulfillment, and no .then | 3027 | 3039 | // lookup on it is observable. | 3028 | 3040 | const startFulfilled = startResult === null || | @@ -3036,12 +3048,7 @@ function setupReadableStreamDefaultController( | 3036 | 3048 | return; | 3037 | 3049 | } | 3038 | 3050 |
| 3039 | | - const started = () => { | 3040 | | - controller[kState].started = true; | 3041 | | - assert(!controller[kState].pulling); | 3042 | | - assert(!controller[kState].pullAgain); | 3043 | | - readableStreamDefaultControllerCallPullIfNeeded(controller); | 3044 | | - }; | | 3051 | + const started = () => readableStreamDefaultControllerStarted(controller); | 3045 | 3052 |
| 3046 | 3053 | if (startFulfilled) { | 3047 | 3054 | // The post-start step runs at the exact microtask position the | @@ -3058,6 +3065,15 @@ function setupReadableStreamDefaultController( | 3058 | 3065 | (error) => readableStreamDefaultControllerError(controller, error)); | 3059 | 3066 | } | 3060 | 3067 |
| | 3068 | +// The post-start steps; transform streams deliver them directly (see | | 3069 | +// transformStreamStart). | | 3070 | +function readableStreamDefaultControllerStarted(controller) { | | 3071 | + controller[kState].started = true; | | 3072 | + assert(!controller[kState].pulling); | | 3073 | + assert(!controller[kState].pullAgain); | | 3074 | + readableStreamDefaultControllerCallPullIfNeeded(controller); | | 3075 | +} | | 3076 | + | 3061 | 3077 | function setupReadableStreamDefaultControllerFromSource( | 3062 | 3078 | stream, | 3063 | 3079 | source, | @@ -4033,6 +4049,7 @@ module.exports = { | 4033 | 4049 | readableStreamDefaultControllerError, | 4034 | 4050 | readableStreamDefaultControllerCancelSteps, | 4035 | 4051 | readableStreamDefaultControllerPullSteps, | | 4052 | + readableStreamDefaultControllerStarted, | 4036 | 4053 | setupReadableStreamDefaultController, | 4037 | 4054 | setupReadableStreamDefaultControllerFromSource, | 4038 | 4055 | readableByteStreamControllerClose, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/transformstream.js +74-34Lines changed: 74 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | FunctionPrototypeCall, | 5 | 5 | ObjectDefineProperties, | 6 | 6 | ObjectSetPrototypeOf, | | 7 | + Promise, | 7 | 8 | PromisePrototypeThen, | 8 | 9 | PromiseReject, | 9 | 10 | PromiseResolve, | @@ -51,6 +52,7 @@ const { | 51 | 52 | createPromiseCallback1Param, | 52 | 53 | createRawCallback2Params, | 53 | 54 | customInspect, | | 55 | + deferredStartAlgorithm, | 54 | 56 | extractHighWaterMark, | 55 | 57 | extractSizeAlgorithm, | 56 | 58 | getNonWritablePropertyDescriptor, | @@ -71,11 +73,14 @@ const { | 71 | 73 | readableStreamDefaultControllerError, | 72 | 74 | readableStreamDefaultControllerGetDesiredSize, | 73 | 75 | readableStreamDefaultControllerHasBackpressure, | | 76 | + readableStreamDefaultControllerStarted, | 74 | 77 | } = require('internal/webstreams/readablestream'); | 75 | 78 |
| 76 | 79 | const { | 77 | 80 | createWritableStream, | 78 | 81 | writableStreamDefaultControllerErrorIfNeeded, | | 82 | + writableStreamDefaultControllerStarted, | | 83 | + writableStreamDefaultControllerStartFailed, | 79 | 84 | } = require('internal/webstreams/writablestream'); | 80 | 85 |
| 81 | 86 | const assert = require('internal/assert'); | @@ -162,28 +167,20 @@ class TransformStream { | 162 | 167 | extractHighWaterMark(writableHighWaterMark, 1); | 163 | 168 | const actualWritableSize = extractSizeAlgorithm(writableSize); | 164 | 169 |
| 165 | | - // Without a start() the start promise is already resolved by the time | 166 | | - // the readable and writable sides adopt it, so the shared resolved | 167 | | - // promise stands in for the record. | 168 | | - const startPromise = start !== undefined ? PromiseWithResolvers() : undefined; | 169 | | - | 170 | 170 | initializeTransformStream( | 171 | 171 | this, | 172 | | - startPromise, | 173 | 172 | actualWritableHighWaterMark, | 174 | 173 | actualWritableSize, | 175 | 174 | actualReadableHighWaterMark, | 176 | 175 | actualReadableSize); | 177 | 176 |
| 178 | 177 | setupTransformStreamDefaultControllerFromTransformer(this, transformer); | 179 | 178 |
| 180 | | - if (start !== undefined) { | 181 | | - startPromise.resolve( | 182 | | - FunctionPrototypeCall( | 183 | | - start, | 184 | | - transformer, | 185 | | - this[kState].controller)); | 186 | | - } | | 179 | + transformStreamStart( | | 180 | + this, | | 181 | + start === undefined ? | | 182 | + undefined : | | 183 | + FunctionPrototypeCall(start, transformer, this[kState].controller)); | 187 | 184 | } | 188 | 185 |
| 189 | 186 | /** | @@ -385,38 +382,35 @@ function defaultTransformAlgorithm(chunk, controller) { | 385 | 382 | transformStreamDefaultControllerEnqueue(controller, chunk); | 386 | 383 | } | 387 | 384 |
| 388 | | -function resolvedStartAlgorithm() { | 389 | | - return kResolvedPromise; | 390 | | -} | 391 | | - | 392 | 385 | function initializeTransformStream( | 393 | 386 | stream, | 394 | | - startPromise, | 395 | 387 | writableHighWaterMark, | 396 | 388 | writableSizeAlgorithm, | 397 | 389 | readableHighWaterMark, | 398 | 390 | readableSizeAlgorithm) { | 399 | 391 |
| 400 | | - const startAlgorithm = startPromise === undefined ? | 401 | | - resolvedStartAlgorithm : | 402 | | - () => startPromise.promise; | 403 | | - | | 392 | + // The sink and source algorithms are shared functions that reach the | | 393 | + // transform stream through their controller, and the post-start step | | 394 | + // is delivered by transformStreamStart, so no per-stream closures or | | 395 | + // start promises are allocated here. | 404 | 396 | const writable = createWritableStream( | 405 | | - startAlgorithm, | 406 | | - (chunk) => transformStreamDefaultSinkWriteAlgorithm(stream, chunk), | 407 | | - () => transformStreamDefaultSinkCloseAlgorithm(stream), | 408 | | - (reason) => transformStreamDefaultSinkAbortAlgorithm(stream, reason), | | 397 | + deferredStartAlgorithm, | | 398 | + transformStreamDefaultSinkWriteAlgorithm, | | 399 | + transformStreamDefaultSinkCloseAlgorithm, | | 400 | + transformStreamDefaultSinkAbortAlgorithm, | 409 | 401 | writableHighWaterMark, | 410 | 402 | writableSizeAlgorithm, | 411 | 403 | ); | | 404 | + writable[kState].controller[kState].transformStream = stream; | 412 | 405 |
| 413 | 406 | const readable = createReadableStream( | 414 | | - startAlgorithm, | 415 | | - () => transformStreamDefaultSourcePullAlgorithm(stream), | 416 | | - (reason) => transformStreamDefaultSourceCancelAlgorithm(stream, reason), | | 407 | + deferredStartAlgorithm, | | 408 | + transformStreamDefaultSourcePullAlgorithm, | | 409 | + transformStreamDefaultSourceCancelAlgorithm, | 417 | 410 | readableHighWaterMark, | 418 | 411 | readableSizeAlgorithm, | 419 | 412 | ); | | 413 | + readable[kState].controller[kState].transformStream = stream; | 420 | 414 |
| 421 | 415 | const state = new TransformStreamState(); | 422 | 416 | state.readable = readable; | @@ -426,6 +420,43 @@ function initializeTransformStream( | 426 | 420 | transformStreamSetBackpressure(stream, true); | 427 | 421 | } | 428 | 422 |
| | 423 | +// Delivers the post-start step of both sides. The spec resolves the start | | 424 | +// promise with the transformer's start result at the end of construction | | 425 | +// and each side then adopts it through a wrapper promise, so with a | | 426 | +// non-thenable start result the started steps run three microtasks after | | 427 | +// construction, and with a thenable one two microtasks after the adopting | | 428 | +// promise settles. The same hops are taken as reactions on one shared | | 429 | +// promise instead of a promise record plus two wrapper promises. | | 430 | +function transformStreamStart(stream, startResult) { | | 431 | + const state = stream[kState]; | | 432 | + const started = () => { | | 433 | + writableStreamDefaultControllerStarted(state.writable[kState].controller); | | 434 | + readableStreamDefaultControllerStarted(state.readable[kState].controller); | | 435 | + }; | | 436 | + if (startResult === null || | | 437 | + (typeof startResult !== 'object' && typeof startResult !== 'function')) { | | 438 | + PromisePrototypeThen( | | 439 | + PromisePrototypeThen( | | 440 | + PromisePrototypeThen(kResolvedPromise, undefined, undefined), | | 441 | + undefined, | | 442 | + undefined), | | 443 | + started); | | 444 | + return; | | 445 | + } | | 446 | + const startPromise = new Promise((resolve) => resolve(startResult)); | | 447 | + PromisePrototypeThen( | | 448 | + PromisePrototypeThen(startPromise, undefined, undefined), | | 449 | + started, | | 450 | + (error) => { | | 451 | + writableStreamDefaultControllerStartFailed( | | 452 | + state.writable[kState].controller, | | 453 | + error); | | 454 | + readableStreamDefaultControllerError( | | 455 | + state.readable[kState].controller, | | 456 | + error); | | 457 | + }); | | 458 | +} | | 459 | + | 429 | 460 | function transformStreamError(stream, error) { | 430 | 461 | const { | 431 | 462 | readable, | @@ -620,7 +651,8 @@ function transformStreamDefaultControllerTerminate(controller) { | 620 | 651 | new ERR_INVALID_STATE.TypeError('TransformStream has been terminated')); | 621 | 652 | } | 622 | 653 |
| 623 | | -function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { | | 654 | +function transformStreamDefaultSinkWriteAlgorithm(chunk, writableController) { | | 655 | + const stream = writableController[kState].transformStream; | 624 | 656 | const state = stream[kState]; | 625 | 657 | const { | 626 | 658 | writable, | @@ -684,7 +716,10 @@ function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { | 684 | 716 | return transformStreamDefaultControllerPerformTransform(controller, chunk); | 685 | 717 | } | 686 | 718 |
| 687 | | -async function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { | | 719 | +async function transformStreamDefaultSinkAbortAlgorithm( | | 720 | + reason, | | 721 | + writableController) { | | 722 | + const stream = writableController[kState].transformStream; | 688 | 723 | const { | 689 | 724 | controller, | 690 | 725 | readable, | @@ -718,7 +753,8 @@ async function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { | 718 | 753 | return controller[kState].finishPromise; | 719 | 754 | } | 720 | 755 |
| 721 | | -function transformStreamDefaultSinkCloseAlgorithm(stream) { | | 756 | +function transformStreamDefaultSinkCloseAlgorithm(writableController) { | | 757 | + const stream = writableController[kState].transformStream; | 722 | 758 | const { | 723 | 759 | readable, | 724 | 760 | controller, | @@ -748,7 +784,8 @@ function transformStreamDefaultSinkCloseAlgorithm(stream) { | 748 | 784 | return controller[kState].finishPromise; | 749 | 785 | } | 750 | 786 |
| 751 | | -function transformStreamDefaultSourcePullAlgorithm(stream) { | | 787 | +function transformStreamDefaultSourcePullAlgorithm(readableController) { | | 788 | + const stream = readableController[kState].transformStream; | 752 | 789 | const state = stream[kState]; | 753 | 790 | assert(state.backpressure); | 754 | 791 | transformStreamSetBackpressure(stream, false); | @@ -760,7 +797,10 @@ function transformStreamDefaultSourcePullAlgorithm(stream) { | 760 | 797 | return kParkedAlgorithmResult; | 761 | 798 | } | 762 | 799 |
| 763 | | -function transformStreamDefaultSourceCancelAlgorithm(stream, reason) { | | 800 | +function transformStreamDefaultSourceCancelAlgorithm( | | 801 | + reason, | | 802 | + readableController) { | | 803 | + const stream = readableController[kState].transformStream; | 764 | 804 | const { | 765 | 805 | controller, | 766 | 806 | writable, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/util.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -357,6 +357,15 @@ const kResolvedPromise = PromiseResolve(); | 357 | 357 | // (see the transform stream source pull algorithm). | 358 | 358 | const kParkedAlgorithmResult = Symbol('kParkedAlgorithmResult'); | 359 | 359 |
| | 360 | +// Start algorithm of internal streams whose creator delivers the | | 361 | +// post-start step itself (see transformStreamStart): the controller setup | | 362 | +// leaves `started` unset when the start result is this sentinel. | | 363 | +const kDeferredStart = Symbol('kDeferredStart'); | | 364 | + | | 365 | +function deferredStartAlgorithm() { | | 366 | + return kDeferredStart; | | 367 | +} | | 368 | + | 360 | 369 | // Wires the (possibly non-thenable) result of an underlying algorithm | 361 | 370 | // callback to its fulfilled/rejected continuations. A non-thenable result | 362 | 371 | // means fulfillment is guaranteed and no then() lookup is observable, so | @@ -461,13 +470,15 @@ module.exports = { | 461 | 470 | createRawCallback2Params, | 462 | 471 | customInspect, | 463 | 472 | defaultSizeAlgorithm, | | 473 | + deferredStartAlgorithm, | 464 | 474 | dequeueValue, | 465 | 475 | enqueueValueWithSize, | 466 | 476 | extractHighWaterMark, | 467 | 477 | extractSizeAlgorithm, | 468 | 478 | getNonWritablePropertyDescriptor, | 469 | 479 | isBrandCheck, | 470 | 480 | isPromisePending, | | 481 | + kDeferredStart, | 471 | 482 | kEmptyQueue, | 472 | 483 | kParkedAlgorithmResult, | 473 | 484 | kPendingPromise, | |
|