-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
stream: speed up WHATWG web streams #65273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,7 @@ const { | |
| extractSizeAlgorithm, | ||
| getNonWritablePropertyDescriptor, | ||
| isBrandCheck, | ||
| isNonThenable, | ||
| kEmptyQueue, | ||
| kResolvedPromise, | ||
| kState, | ||
|
|
@@ -252,6 +253,16 @@ class ReadableStream { | |
| */ | ||
| constructor(source = kEmptyObject, strategy = kEmptyObject) { | ||
| markTransferMode(this, false, true); | ||
| // Empty-argument `new ReadableStream()`: no source, no strategy, and | ||
| // no controller. Reads never deliver data, so skip those allocations | ||
| // until getReader/cancel/error first need a default controller. | ||
| // Subclasses that call those methods after super() materialize the | ||
| // controller in the subclass constructor; see | ||
| // ensureEmptyDefaultController. | ||
| if (source === kEmptyObject && strategy === kEmptyObject) { | ||
| this[kState] = createReadableStreamState(); | ||
| return; | ||
| } | ||
| validateObject(source, 'source', kValidateObjectAllowObjects); | ||
| validateObject(strategy, 'strategy', kValidateObjectAllowObjectsAndNull); | ||
| this[kState] = createReadableStreamState(); | ||
|
|
@@ -301,8 +312,14 @@ class ReadableStream { | |
| // only default controllers were wired here; byte stream controllers | ||
| // keep the previous no-op behavior. | ||
| const controller = this[kState].controller; | ||
| if (controller === undefined) { | ||
| if (this[kState].state === 'readable') { | ||
| readableStreamError(this, error); | ||
| } | ||
| return; | ||
| } | ||
| if (isReadableStreamDefaultController(controller)) | ||
| controller.error(error); | ||
| readableStreamDefaultControllerError(controller, error); | ||
| } | ||
|
|
||
| // Used by the internal stream interop (end-of-stream). Materialized | ||
|
|
@@ -351,6 +368,10 @@ class ReadableStream { | |
| return PromiseReject( | ||
| new ERR_INVALID_STATE.TypeError('ReadableStream is locked')); | ||
| } | ||
| // Only materialize the deferred empty controller when cancel will | ||
| // actually run cancel steps. closed/errored streams return immediately. | ||
| if (this[kState].state === 'readable') | ||
| ensureEmptyDefaultController(this); | ||
| return readableStreamCancel(this, reason); | ||
| } | ||
|
|
||
|
|
@@ -1422,6 +1443,7 @@ function createReadableStreamState() { | |
| return { | ||
| __proto__: null, | ||
| closedPromise: undefined, | ||
| controller: undefined, | ||
| disturbed: false, | ||
| reader: undefined, | ||
| state: 'readable', | ||
|
|
@@ -2580,6 +2602,7 @@ function setupReadableStreamBYOBReader(reader, stream) { | |
| function setupReadableStreamDefaultReader(reader, stream) { | ||
| if (isReadableStreamLocked(stream)) | ||
| throw new ERR_INVALID_STATE.TypeError('ReadableStream is locked'); | ||
| ensureEmptyDefaultController(stream); | ||
| readableStreamReaderGenericInitialize(reader, stream); | ||
| reader[kState].readRequests = kEmptyQueue; | ||
| } | ||
|
|
@@ -2729,7 +2752,8 @@ function readableStreamDefaultControllerPull(controller) { | |
| // The pull algorithm may be a raw callback (a wrapped user source.pull | ||
| // returns its result uncoerced; a synchronous throw surfaces here) or an | ||
| // internal algorithm that always returns a promise; thenAlgorithmResult | ||
| // handles both. | ||
| // handles both. Non-thenable results react on kResolvedPromise so each | ||
| // pull is still separated by a microtask, matching the spec. | ||
| let result; | ||
| try { | ||
| result = controller[kState].pullAlgorithm(controller); | ||
|
|
@@ -2796,6 +2820,43 @@ function readableStreamDefaultControllerPullSteps(controller, readRequest) { | |
| readableStreamDefaultControllerPull(controller); | ||
| } | ||
|
|
||
| // Materialize the deferred default controller for `new ReadableStream()`. | ||
| // | ||
| // started is true immediately: the empty-argument start algorithm is a | ||
| // no-op, so there is no initial pull and nothing can observe an unstarted | ||
| // controller without first calling getReader/cancel/pipeTo/tee/values, | ||
| // all of which come through here. That is also why this still matches | ||
| // WPT: those tests either pass a source (leaving this path) or wait for | ||
| // start, which is already complete for a no-op start. | ||
| // | ||
| // Subclasses that call cancel(), getReader(), pipeTo(), tee(), or | ||
| // values() in the constructor body after super() will materialize the | ||
| // controller before the subclass constructor finishes. Passing a source | ||
| // (for example to install start/pull) leaves the empty-argument path | ||
| // and creates the controller during super() as usual. | ||
| function ensureEmptyDefaultController(stream) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correctly handled in subclasses? A subclass could end up calling cancel, getReader, etc before the constructor finishes, causing the controller to be materialized. Worth documenting and tests.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks okay to me? As explained in anonrig's previous comment, the controller is materialized in all those methods:
Subclasses don't really affect this: if a subclass wanted to get access to the controller, they'd still need to pass a |
||
| if (stream[kState].controller !== undefined) | ||
| return stream[kState].controller; | ||
| const controller = new ReadableStreamDefaultController(kSkipThrow); | ||
| controller[kState] = { | ||
| cancelAlgorithm: nonOpCancel, | ||
| closeRequested: false, | ||
| highWaterMark: 1, | ||
| pullAgain: false, | ||
| pullAlgorithm: nonOpCallback, | ||
| pulling: false, | ||
| pullFulfilled: undefined, | ||
| pullRejected: undefined, | ||
| queue: kEmptyQueue, | ||
| queueTotalSize: 0, | ||
| started: true, | ||
| sizeAlgorithm: defaultSizeAlgorithm, | ||
| stream, | ||
| }; | ||
| stream[kState].controller = controller; | ||
| return controller; | ||
| } | ||
|
|
||
| function setupReadableStreamDefaultController( | ||
| stream, | ||
| controller, | ||
|
|
@@ -2824,8 +2885,7 @@ function setupReadableStreamDefaultController( | |
|
|
||
| const startResult = startAlgorithm(); | ||
|
|
||
| if (startResult === null || | ||
| (typeof startResult !== 'object' && typeof startResult !== 'function')) { | ||
| if (isNonThenable(startResult)) { | ||
| // Non-thenable start result: fulfillment is guaranteed and no .then | ||
| // lookup on the result is observable, so run the post-start step | ||
| // directly at the exact microtask position the promise reaction | ||
|
|
@@ -3708,8 +3768,7 @@ function setupReadableByteStreamController( | |
|
|
||
| const startResult = startAlgorithm(); | ||
|
|
||
| if (startResult === null || | ||
| (typeof startResult !== 'object' && typeof startResult !== 'function')) { | ||
| if (isNonThenable(startResult)) { | ||
| // See setupReadableStreamDefaultController. | ||
| queueMicrotask(() => { | ||
| controller[kState].started = true; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.