| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d286423 commit 9ec9383
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const { | ||
| 4 | + ReadableStream, | ||
| 5 | + TransformStream, | ||
| 6 | + } = require('node:stream/web'); | ||
| 7 | + | ||
| 8 | + const bench = common.createBenchmark(main, { | ||
| 9 | + n: [5e5], | ||
| 10 | + kind: ['default', 'transform'], | ||
| 11 | + }); | ||
| 12 | + | ||
| 13 | + async function main({ n, kind }) { | ||
| 14 | + const b = Buffer.alloc(64); | ||
| 15 | + let i = 0; | ||
| 16 | + const rs = new ReadableStream({ | ||
| 17 | + pull(controller) { | ||
| 18 | + if (i++ < n) { | ||
| 19 | + controller.enqueue(b); | ||
| 20 | + } else { | ||
| 21 | + controller.close(); | ||
| 22 | + } | ||
| 23 | + }, | ||
| 24 | + }); | ||
| 25 | + const ts = kind === 'default' ? | ||
| 26 | + new TransformStream() : | ||
| 27 | + new TransformStream({ | ||
| 28 | + transform(chunk, controller) { controller.enqueue(chunk); }, | ||
| 29 | + }); | ||
| 30 | + | ||
| 31 | + const reader = rs.pipeThrough(ts).getReader(); | ||
| 32 | + bench.start(); | ||
| 33 | + for (;;) { | ||
| 34 | + const { done } = await reader.read(); | ||
| 35 | + if (done) break; | ||
| 36 | + } | ||
| 37 | + bench.end(n); | ||
| 38 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,8 @@ const { | |||
| 5 | 5 | ObjectDefineProperties, | |
| 6 | 6 | ObjectSetPrototypeOf, | |
| 7 | 7 | PromisePrototypeThen, | |
| 8 | + PromiseReject, | ||
| 9 | + PromiseResolve, | ||
| 8 | 10 | PromiseWithResolvers, | |
| 9 | 11 | Symbol, | |
| 10 | 12 | SymbolToStringTag, | |
@@ -44,12 +46,14 @@ const { | |||
| 44 | 46 | ||
| 45 | 47 | const { | |
| 46 | 48 | createPromiseCallback1Param, | |
| 47 | - createPromiseCallback2Params, | ||
| 49 | + createRawCallback2Params, | ||
| 48 | 50 | customInspect, | |
| 49 | 51 | extractHighWaterMark, | |
| 50 | 52 | extractSizeAlgorithm, | |
| 51 | 53 | getNonWritablePropertyDescriptor, | |
| 52 | 54 | isBrandCheck, | |
| 55 | + kParkedAlgorithmResult, | ||
| 56 | + kResolvedPromise, | ||
| 53 | 57 | kState, | |
| 54 | 58 | kType, | |
| 55 | 59 | nonOpCancel, | |
@@ -258,7 +262,10 @@ function InternalTransferredTransformStream() { | |||
| 258 | 262 | readable: undefined, | |
| 259 | 263 | writable: undefined, | |
| 260 | 264 | backpressure: undefined, | |
| 261 | - backpressureChange: undefined, | ||
| 265 | + pullPending: false, | ||
| 266 | + pendingWrite: undefined, | ||
| 267 | + pendingWriteChunk: undefined, | ||
| 268 | + writeContinuation: undefined, | ||
| 262 | 269 | controller: undefined, | |
| 263 | 270 | }; | |
| 264 | 271 | } | |
@@ -348,7 +355,9 @@ const isTransformStream = | |||
| 348 | 355 | const isTransformStreamDefaultController = | |
| 349 | 356 | isBrandCheck('TransformStreamDefaultController'); | |
| 350 | 357 | ||
| 351 | - async function defaultTransformAlgorithm(chunk, controller) { | ||
| 358 | + // Raw callback (see createRawCallback*): invoked inside the try/catch of | ||
| 359 | + // transformStreamDefaultControllerPerformTransform. | ||
| 360 | + function defaultTransformAlgorithm(chunk, controller) { | ||
| 352 | 361 | transformStreamDefaultControllerEnqueue(controller, chunk); | |
| 353 | 362 | } | |
| 354 | 363 | ||
@@ -385,7 +394,12 @@ function initializeTransformStream( | |||
| 385 | 394 | writable, | |
| 386 | 395 | controller: undefined, | |
| 387 | 396 | backpressure: undefined, | |
| 388 | - backpressureChange: undefined, | ||
| 397 | + // Continuation slots replacing the spec's | ||
| 398 | + // [[backpressureChangePromise]]; see transformStreamSetBackpressure. | ||
| 399 | + pullPending: false, | ||
| 400 | + pendingWrite: undefined, | ||
| 401 | + pendingWriteChunk: undefined, | ||
| 402 | + writeContinuation: undefined, | ||
| 389 | 403 | }; | |
| 390 | 404 | ||
| 391 | 405 | transformStreamSetBackpressure(stream, true); | |
@@ -422,24 +436,30 @@ function transformStreamUnblockWrite(stream) { | |||
| 422 | 436 | // The spec's [[backpressureChangePromise]] is only ever observed by the | |
| 423 | 437 | // source pull algorithm (settles when backpressure next becomes true) and | |
| 424 | 438 | // by a sink write arriving while backpressure is set (settles when | |
| 425 | - // backpressure next becomes false). Instead of allocating a fresh promise | ||
| 426 | - // record on every flip, the record is materialized lazily on first | ||
| 427 | - // observation and dropped once settled; flips nobody is waiting on | ||
| 428 | - // allocate nothing. | ||
| 429 | - function transformStreamBackpressureChangePromise(stream) { | ||
| 430 | - const state = stream[kState]; | ||
| 431 | - return (state.backpressureChange ??= PromiseWithResolvers()).promise; | ||
| 432 | - } | ||
| 433 | - | ||
| 439 | + // backpressure next becomes false). Both observers are internal, so the | ||
| 440 | + // promise record is replaced by continuation slots: a parked pull is | ||
| 441 | + // completed by delivering the readable controller's pull-fulfilled step, | ||
| 442 | + // and a parked write by the cached write continuation (see | ||
| 443 | + // transformStreamDefaultSinkWriteAlgorithm). Each is enqueued on the | ||
| 444 | + // shared resolved promise at the exact microtask position the old | ||
| 445 | + // record's reaction would have had. | ||
| 434 | 446 | function transformStreamSetBackpressure(stream, backpressure) { | |
| 435 | 447 | const state = stream[kState]; | |
| 436 | 448 | assert(state.backpressure !== backpressure); | |
| 437 | - const backpressureChange = state.backpressureChange; | ||
| 438 | - if (backpressureChange !== undefined) { | ||
| 439 | - state.backpressureChange = undefined; | ||
| 440 | - backpressureChange.resolve(); | ||
| 441 | - } | ||
| 442 | 449 | state.backpressure = backpressure; | |
| 450 | + if (backpressure) { | ||
| 451 | + if (state.pullPending) { | ||
| 452 | + state.pullPending = false; | ||
| 453 | + // The pull-fulfilled step exists: a pull parked it (see | ||
| 454 | + // transformStreamDefaultSourcePullAlgorithm), and the readable | ||
| 455 | + // controller creates it before invoking the pull algorithm. | ||
| 456 | + PromisePrototypeThen( | ||
| 457 | + kResolvedPromise, | ||
| 458 | + state.readable[kState].controller[kState].pullFulfilled); | ||
| 459 | + } | ||
| 460 | + } else if (state.pendingWrite !== undefined) { | ||
| 461 | + PromisePrototypeThen(kResolvedPromise, state.writeContinuation); | ||
| 462 | + } | ||
| 443 | 463 | } | |
| 444 | 464 | ||
| 445 | 465 | function setupTransformStreamDefaultController( | |
@@ -456,6 +476,7 @@ function setupTransformStreamDefaultController( | |||
| 456 | 476 | transformAlgorithm, | |
| 457 | 477 | flushAlgorithm, | |
| 458 | 478 | cancelAlgorithm, | |
| 479 | + performTransformRejected: undefined, | ||
| 459 | 480 | }; | |
| 460 | 481 | stream[kState].controller = controller; | |
| 461 | 482 | } | |
@@ -468,7 +489,7 @@ function setupTransformStreamDefaultControllerFromTransformer( | |||
| 468 | 489 | const flush = transformer?.flush; | |
| 469 | 490 | const cancel = transformer?.cancel; | |
| 470 | 491 | const transformAlgorithm = transform ? | |
| 471 | - createPromiseCallback2Params('transformer.transform', transform, transformer) : | ||
| 492 | + createRawCallback2Params('transformer.transform', transform, transformer) : | ||
| 472 | 493 | defaultTransformAlgorithm; | |
| 473 | 494 | const flushAlgorithm = flush ? | |
| 474 | 495 | createPromiseCallback1Param('transformer.flush', flush, transformer) : | |
@@ -521,18 +542,40 @@ function transformStreamDefaultControllerError(controller, error) { | |||
| 521 | 542 | transformStreamError(controller[kState].stream, error); | |
| 522 | 543 | } | |
| 523 | 544 | ||
| 524 | - async function transformStreamDefaultControllerPerformTransform(controller, chunk) { | ||
| 545 | + // Mirrors the reference implementation's | ||
| 546 | + // `promiseCall(transformAlgorithm, ...).then(undefined, rejectionSteps)`: | ||
| 547 | + // the returned promise settles one microtask after the (coerced) result | ||
| 548 | + // does, and a rejection errors the transform stream before propagating. | ||
| 549 | + // The raw transform callback plus the shared resolved promise for | ||
| 550 | + // non-thenable results replace the previous async wrapper's two implicit | ||
| 551 | + // promises per chunk. | ||
| 552 | + function transformStreamDefaultControllerPerformTransform(controller, chunk) { | ||
| 553 | + const controllerState = controller[kState]; | ||
| 554 | + const transformAlgorithm = controllerState.transformAlgorithm; | ||
| 555 | + if (transformAlgorithm === undefined) { | ||
| 556 | + // Algorithms were cleared by a concurrent cancel/abort/close. | ||
| 557 | + return kResolvedPromise; | ||
| 558 | + } | ||
| 559 | + let result; | ||
| 525 | 560 | try { | |
| 526 | - const transformAlgorithm = controller[kState].transformAlgorithm; | ||
| 527 | - if (transformAlgorithm === undefined) { | ||
| 528 | - // Algorithms were cleared by a concurrent cancel/abort/close. | ||
| 529 | - return; | ||
| 530 | - } | ||
| 531 | - return await transformAlgorithm(chunk, controller); | ||
| 561 | + result = transformAlgorithm(chunk, controller); | ||
| 532 | 562 | } catch (error) { | |
| 563 | + result = PromiseReject(error); | ||
| 564 | + } | ||
| 565 | + if (result === null || | ||
| 566 | + (typeof result !== 'object' && typeof result !== 'function')) { | ||
| 567 | + result = kResolvedPromise; | ||
| 568 | + } else { | ||
| 569 | + result = PromiseResolve(result); | ||
| 570 | + } | ||
| 571 | + controllerState.performTransformRejected ??= (error) => { | ||
| 533 | 572 | transformStreamError(controller[kState].stream, error); | |
| 534 | 573 | throw error; | |
| 535 | - } | ||
| 574 | + }; | ||
| 575 | + return PromisePrototypeThen( | ||
| 576 | + result, | ||
| 577 | + undefined, | ||
| 578 | + controllerState.performTransformRejected); | ||
| 536 | 579 | } | |
| 537 | 580 | ||
| 538 | 581 | function transformStreamDefaultControllerTerminate(controller) { | |
@@ -553,26 +596,42 @@ function transformStreamDefaultControllerTerminate(controller) { | |||
| 553 | 596 | } | |
| 554 | 597 | ||
| 555 | 598 | function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { | |
| 599 | + const state = stream[kState]; | ||
| 556 | 600 | const { | |
| 557 | 601 | writable, | |
| 558 | 602 | controller, | |
| 559 | - } = stream[kState]; | ||
| 603 | + } = state; | ||
| 560 | 604 | assert(writable[kState].state === 'writable'); | |
| 561 | - if (stream[kState].backpressure) { | ||
| 562 | - const backpressureChange = transformStreamBackpressureChangePromise(stream); | ||
| 563 | - return PromisePrototypeThen( | ||
| 564 | - backpressureChange, | ||
| 565 | - () => { | ||
| 566 | - const { | ||
| 567 | - writable, | ||
| 568 | - } = stream[kState]; | ||
| 569 | - if (writable[kState].state === 'erroring') | ||
| 570 | - throw writable[kState].storedError; | ||
| 571 | - assert(writable[kState].state === 'writable'); | ||
| 572 | - return transformStreamDefaultControllerPerformTransform( | ||
| 605 | + if (state.backpressure) { | ||
| 606 | + // Park the chunk and one promise record; the backpressure -> false | ||
| 607 | + // flip delivers the cached continuation (see | ||
| 608 | + // transformStreamSetBackpressure) at the same microtask position as | ||
| 609 | + // the old [[backpressureChangePromise]] reaction. The continuation | ||
| 610 | + // resolves the sink promise with the perform-transform promise, so | ||
| 611 | + // adoption reproduces the old derived-chain settle depth exactly. | ||
| 612 | + // The writable dispatches a single write at a time, so one pending | ||
| 613 | + // slot suffices. | ||
| 614 | + assert(state.pendingWrite === undefined); | ||
| 615 | + const pendingWrite = PromiseWithResolvers(); | ||
| 616 | + state.pendingWrite = pendingWrite; | ||
| 617 | + state.pendingWriteChunk = chunk; | ||
| 618 | + state.writeContinuation ??= () => { | ||
| 619 | + const pending = state.pendingWrite; | ||
| 620 | + const pendingChunk = state.pendingWriteChunk; | ||
| 621 | + state.pendingWrite = undefined; | ||
| 622 | + state.pendingWriteChunk = undefined; | ||
| 623 | + const writableState = state.writable[kState]; | ||
| 624 | + if (writableState.state === 'erroring') { | ||
| 625 | + pending.reject(writableState.storedError); | ||
| 626 | + return; | ||
| 627 | + } | ||
| 628 | + assert(writableState.state === 'writable'); | ||
| 629 | + pending.resolve( | ||
| 630 | + transformStreamDefaultControllerPerformTransform( | ||
| 573 | 631 | controller, | |
| 574 | - chunk); | ||
| 575 | - }); | ||
| 632 | + pendingChunk)); | ||
| 633 | + }; | ||
| 634 | + return pendingWrite.promise; | ||
| 576 | 635 | } | |
| 577 | 636 | return transformStreamDefaultControllerPerformTransform(controller, chunk); | |
| 578 | 637 | } | |
@@ -642,9 +701,15 @@ function transformStreamDefaultSinkCloseAlgorithm(stream) { | |||
| 642 | 701 | } | |
| 643 | 702 | ||
| 644 | 703 | function transformStreamDefaultSourcePullAlgorithm(stream) { | |
| 645 | - assert(stream[kState].backpressure); | ||
| 704 | + const state = stream[kState]; | ||
| 705 | + assert(state.backpressure); | ||
| 646 | 706 | transformStreamSetBackpressure(stream, false); | |
| 647 | - return transformStreamBackpressureChangePromise(stream); | ||
| 707 | + // Park the pull: the next backpressure -> true flip delivers the | ||
| 708 | + // pull-fulfilled step (see transformStreamSetBackpressure). The old | ||
| 709 | + // [[backpressureChangePromise]] this replaces was only ever resolved, | ||
| 710 | + // so the parked pull needs no rejection delivery. | ||
| 711 | + state.pullPending = true; | ||
| 712 | + return kParkedAlgorithmResult; | ||
| 648 | 713 | } | |
| 649 | 714 | ||
| 650 | 715 | function transformStreamDefaultSourceCancelAlgorithm(stream, reason) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -355,6 +355,12 @@ function createRawCallback2Params(name, fn, thisArg) { | |||
| 355 | 355 | // the next microtask checkpoint without allocating a fresh promise. | |
| 356 | 356 | const kResolvedPromise = PromiseResolve(); | |
| 357 | 357 | ||
| 358 | + // Returned by an internal algorithm to signal that it parked the | ||
| 359 | + // operation and takes responsibility for delivering the fulfilled (or | ||
| 360 | + // rejected) continuation itself later, instead of settling a promise | ||
| 361 | + // (see the transform stream source pull algorithm). | ||
| 362 | + const kParkedAlgorithmResult = { __proto__: null }; | ||
| 363 | + | ||
| 358 | 364 | // Wires the (possibly non-thenable) result of an underlying algorithm | |
| 359 | 365 | // callback to its fulfilled/rejected continuations. A non-thenable result | |
| 360 | 366 | // means fulfillment is guaranteed and no then() lookup is observable, so | |
@@ -364,6 +370,8 @@ const kResolvedPromise = PromiseResolve(); | |||
| 364 | 370 | // matches the spec's "a promise resolved with" conversion (identity for | |
| 365 | 371 | // native promises). | |
| 366 | 372 | function thenAlgorithmResult(result, onFulfilled, onRejected) { | |
| 373 | + if (result === kParkedAlgorithmResult) | ||
| 374 | + return; | ||
| 367 | 375 | if (result === null || | |
| 368 | 376 | (typeof result !== 'object' && typeof result !== 'function')) { | |
| 369 | 377 | PromisePrototypeThen(kResolvedPromise, onFulfilled); | |
@@ -457,6 +465,7 @@ module.exports = { | |||
| 457 | 465 | isBrandCheck, | |
| 458 | 466 | isPromisePending, | |
| 459 | 467 | kEmptyQueue, | |
| 468 | + kParkedAlgorithmResult, | ||
| 460 | 469 | kResolvedPromise, | |
| 461 | 470 | kState, | |
| 462 | 471 | kType, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments