Skip to content

Commit 4fe400b

Browse files
committed
stream: trim webstreams sink wrappers and BYOB transfers
The close and abort methods of an underlying sink and the flush and cancel methods of a transformer were each wrapped at construction in an async closure that the controller kept for the stream's lifetime: two per WritableStream and two per TransformStream, each with its context. Store these one-shot methods as-is in the controller state next to the object they belong to, and call them as that object's methods with the spec's argument list through a promiseCall helper that keeps the reference implementation's adoption ticks. The per-chunk methods (write, transform) keep their closures, which make the method a constant for the optimizing compiler on the hot paths; so does the source's cancel wrapper, since the readable stream's construction path is sensitive to any change in its inlining budget. In the byte controller, the transfer of a pull-into descriptor's buffer when it is committed is dropped: nothing can reach that buffer any more, since it was transferred when the descriptor was created and the BYOB request that exposed it is only ever invalidated together with a transfer. The transfer of the first pending descriptor's buffer on enqueue is likewise only needed while a BYOB request exposes it. Allocating an ArrayBuffer costs about half a microsecond, so those two transfers were a large part of a BYOB read. Bytes allocated per stream with user methods: WritableStream 1040 -> 844, TransformStream 1816 -> 1720. The microtask ordering of every user method call is unchanged. Benchmark results (20 runs, vs. the previous commit): webstreams/readable-read.js type='byob' n=100000 *** 17.14 % ±1.80% webstreams/readable-async-iterator.js type='normal' n=100000 3.79 % ±4.49% webstreams/from.js kind='sync' n=1000000 * -2.18 % ±2.02% webstreams/js_transfer.js n=10000 payload='TransformStream' * -1.85 % ±1.58% The other 42 rows are within their confidence intervals; the two rows at the 5% level are the expected number of false positives for 46 comparisons. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent c5cfdef commit 4fe400b

5 files changed

Lines changed: 257 additions & 70 deletions

File tree

‎lib/internal/webstreams/readablestream.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,13 +2388,16 @@ function readableByteStreamControllerConvertPullIntoDescriptor(desc) {
23882388
if (bytesFilled > byteLength)
23892389
throw new ERR_INVALID_STATE.RangeError('The buffer size is invalid');
23902390
assert(!(bytesFilled % elementSize));
2391-
const transferredBuffer = ArrayBufferPrototypeTransfer(buffer);
2391+
// The spec transfers the buffer once more here. Nothing else can reach
2392+
// it by now: it was transferred when the descriptor was created, and
2393+
// the BYOB request that exposes it has been invalidated, which always
2394+
// goes together with a transfer, so the view is built on it as-is.
23922395

23932396
if (ctor === Buffer) {
2394-
return Buffer.from(transferredBuffer, byteOffset, bytesFilled / elementSize);
2397+
return Buffer.from(buffer, byteOffset, bytesFilled / elementSize);
23952398
}
23962399

2397-
return new ctor(transferredBuffer, byteOffset, bytesFilled / elementSize);
2400+
return new ctor(buffer, byteOffset, bytesFilled / elementSize);
23982401
}
23992402

24002403
function isReadableStreamLocked(stream) {
@@ -3427,11 +3430,15 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
34273430
);
34283431
}
34293432

3430-
readableByteStreamControllerInvalidateBYOBRequest(controller);
3431-
3432-
firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer(
3433-
firstPendingPullInto.buffer,
3434-
);
3433+
// The transfer detaches the view the BYOB request exposed; without
3434+
// a request the buffer is unreachable and there is nothing to
3435+
// detach.
3436+
if (controller[kState].byobRequest !== null) {
3437+
readableByteStreamControllerInvalidateBYOBRequest(controller);
3438+
firstPendingPullInto.buffer = ArrayBufferPrototypeTransfer(
3439+
firstPendingPullInto.buffer,
3440+
);
3441+
}
34353442

34363443
if (firstPendingPullInto.type === 'none') {
34373444
readableByteStreamControllerEnqueueDetachedPullIntoToQueue(

‎lib/internal/webstreams/transformstream.js‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
} = require('internal/util');
3434

3535
const {
36+
validateFunction,
3637
validateObject,
3738
kValidateObjectAllowObjects,
3839
kValidateObjectAllowObjectsAndNull,
@@ -46,7 +47,6 @@ const {
4647
} = require('internal/worker/js_transferable');
4748

4849
const {
49-
createPromiseCallback1Param,
5050
createRawCallback2Params,
5151
customInspect,
5252
deferredStartAlgorithm,
@@ -58,8 +58,8 @@ const {
5858
kResolvedPromise,
5959
kState,
6060
kType,
61-
nonOpCancel,
62-
nonOpFlush,
61+
nonOpCallback,
62+
promiseCall1Param,
6363
} = require('internal/webstreams/util');
6464

6565
const {
@@ -270,6 +270,9 @@ ObjectSetPrototypeOf(TransformStreamState.prototype, null);
270270

271271
class TransformStreamControllerState {
272272
stream = undefined;
273+
// The user-provided transformer the flush and cancel algorithms are
274+
// methods of.
275+
transformer = undefined;
273276
transformAlgorithm = undefined;
274277
flushAlgorithm = undefined;
275278
cancelAlgorithm = undefined;
@@ -514,11 +517,13 @@ function setupTransformStreamDefaultController(
514517
controller,
515518
transformAlgorithm,
516519
flushAlgorithm,
517-
cancelAlgorithm) {
520+
cancelAlgorithm,
521+
transformer) {
518522
assert(isTransformStream(stream));
519523
assert(stream[kState].controller === undefined);
520524
const controllerState = new TransformStreamControllerState();
521525
controllerState.stream = stream;
526+
controllerState.transformer = transformer;
522527
controllerState.transformAlgorithm = transformAlgorithm;
523528
controllerState.flushAlgorithm = flushAlgorithm;
524529
controllerState.cancelAlgorithm = cancelAlgorithm;
@@ -536,25 +541,25 @@ function setupTransformStreamDefaultControllerFromTransformer(
536541
const transformAlgorithm = transform ?
537542
createRawCallback2Params('transformer.transform', transform, transformer) :
538543
defaultTransformAlgorithm;
539-
const flushAlgorithm = flush ?
540-
createPromiseCallback1Param('transformer.flush', flush, transformer) :
541-
nonOpFlush;
542-
const cancelAlgorithm = cancel ?
543-
createPromiseCallback1Param('transformer.cancel', cancel, transformer) :
544-
nonOpCancel;
544+
if (flush)
545+
validateFunction(flush, 'transformer.flush');
546+
if (cancel)
547+
validateFunction(cancel, 'transformer.cancel');
545548

546549
setupTransformStreamDefaultController(
547550
stream,
548551
controller,
549552
transformAlgorithm,
550-
flushAlgorithm,
551-
cancelAlgorithm);
553+
flush || nonOpCallback,
554+
cancel || nonOpCallback,
555+
transformer);
552556
}
553557

554558
function transformStreamDefaultControllerClearAlgorithms(controller) {
555559
controller[kState].transformAlgorithm = undefined;
556560
controller[kState].flushAlgorithm = undefined;
557561
controller[kState].cancelAlgorithm = undefined;
562+
controller[kState].transformer = undefined;
558563
}
559564

560565
function transformStreamDefaultControllerEnqueue(controller, chunk) {
@@ -703,7 +708,9 @@ async function transformStreamDefaultSinkAbortAlgorithm(
703708

704709
const { promise, resolve, reject } = PromiseWithResolvers();
705710
controller[kState].finishPromise = promise;
706-
const cancelPromise = controller[kState].cancelAlgorithm(reason);
711+
const cancelPromise = promiseCall1Param(
712+
controller[kState].cancelAlgorithm, controller[kState].transformer,
713+
reason);
707714
transformStreamDefaultControllerClearAlgorithms(controller);
708715

709716
PromisePrototypeThen(
@@ -737,7 +744,9 @@ function transformStreamDefaultSinkCloseAlgorithm(writableController) {
737744
}
738745
const { promise, resolve, reject } = PromiseWithResolvers();
739746
controller[kState].finishPromise = promise;
740-
const flushPromise = controller[kState].flushAlgorithm(controller);
747+
const flushPromise = promiseCall1Param(
748+
controller[kState].flushAlgorithm, controller[kState].transformer,
749+
controller);
741750
transformStreamDefaultControllerClearAlgorithms(controller);
742751
PromisePrototypeThen(
743752
flushPromise,
@@ -784,7 +793,9 @@ function transformStreamDefaultSourceCancelAlgorithm(
784793

785794
const { promise, resolve, reject } = PromiseWithResolvers();
786795
controller[kState].finishPromise = promise;
787-
const cancelPromise = controller[kState].cancelAlgorithm(reason);
796+
const cancelPromise = promiseCall1Param(
797+
controller[kState].cancelAlgorithm, controller[kState].transformer,
798+
reason);
788799
transformStreamDefaultControllerClearAlgorithms(controller);
789800

790801
PromisePrototypeThen(

‎lib/internal/webstreams/util.js‎

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,14 @@ function enqueueValueWithSize(controller, value, size) {
323323
state.queueTotalSize += coercedSize;
324324
}
325325

326-
// Arity-specialized variants of the promise-callback wrapper. The generic
327-
// rest-parameter + ReflectApply form allocated an arguments array on every
328-
// invocation; these run on per-chunk hot paths (pull/write/transform), so
329-
// each known call-site arity gets its own wrapper. The exact number of
330-
// arguments passed through to the user callback is observable and must be
331-
// preserved.
332-
function createPromiseCallbackNoParams(name, fn, thisArg) {
333-
validateFunction(fn, name);
334-
return async () => FunctionPrototypeCall(fn, thisArg);
335-
}
336-
337-
// Raw variants that skip the async wrapper's implicit result promise.
338-
// Consumers of a raw callback invoke it inside try/catch and route the
339-
// result through thenAlgorithmResult() below.
326+
// The per-chunk callbacks of a user-provided source, sink or transformer
327+
// (pull, write, transform) are wrapped in a closure whose captured method
328+
// stays a constant for the optimizing compiler. Each known call-site
329+
// arity gets its own wrapper, since a rest-parameter form would allocate
330+
// an arguments array per invocation and the exact number of arguments
331+
// passed to the user callback is observable. Consumers of a raw callback
332+
// invoke it inside try/catch and route the result through
333+
// thenAlgorithmResult() below.
340334
function createRawCallback1Param(name, fn, thisArg) {
341335
validateFunction(fn, name);
342336
return (arg) => FunctionPrototypeCall(fn, thisArg, arg);
@@ -347,6 +341,46 @@ function createRawCallback2Params(name, fn, thisArg) {
347341
return (arg1, arg2) => FunctionPrototypeCall(fn, thisArg, arg1, arg2);
348342
}
349343

344+
// Async wrapper of a one-shot callback (the source cancel method).
345+
function createPromiseCallback1Param(name, fn, thisArg) {
346+
validateFunction(fn, name);
347+
return async (arg) => FunctionPrototypeCall(fn, thisArg, arg);
348+
}
349+
350+
// The one-shot methods of a sink or transformer (close, abort, flush,
351+
// cancel) are instead stored as-is in the controller state next to the
352+
// object they belong to and called the way the reference implementation's
353+
// promiseCall() does: a synchronous throw becomes a rejection and the
354+
// result is adopted through a fresh promise, so a returned promise settles
355+
// the caller two microtasks later and a plain value settles it at once.
356+
function promiseCall1Param(fn, thisArg, arg) {
357+
let result;
358+
try {
359+
result = FunctionPrototypeCall(fn, thisArg, arg);
360+
} catch (error) {
361+
return PromiseReject(error);
362+
}
363+
return promiseResolvedWith(result);
364+
}
365+
366+
function promiseCallNoParams(fn, thisArg) {
367+
let result;
368+
try {
369+
result = FunctionPrototypeCall(fn, thisArg);
370+
} catch (error) {
371+
return PromiseReject(error);
372+
}
373+
return promiseResolvedWith(result);
374+
}
375+
376+
function promiseResolvedWith(value) {
377+
if (value === null ||
378+
(typeof value !== 'object' && typeof value !== 'function')) {
379+
return PromiseResolve(value);
380+
}
381+
return new Promise((resolve) => resolve(value));
382+
}
383+
350384
// A single shared, forever-resolved promise used to enqueue a reaction at
351385
// the next microtask checkpoint without allocating a fresh promise.
352386
const kResolvedPromise = PromiseResolve();
@@ -385,16 +419,6 @@ function thenAlgorithmResult(result, onFulfilled, onRejected) {
385419
}
386420
}
387421

388-
function createPromiseCallback1Param(name, fn, thisArg) {
389-
validateFunction(fn, name);
390-
return async (arg) => FunctionPrototypeCall(fn, thisArg, arg);
391-
}
392-
393-
function createPromiseCallback2Params(name, fn, thisArg) {
394-
validateFunction(fn, name);
395-
return async (arg1, arg2) => FunctionPrototypeCall(fn, thisArg, arg1, arg2);
396-
}
397-
398422
function isPromisePending(promise) {
399423
if (promise === undefined) return false;
400424
const details = getPromiseDetails(promise);
@@ -438,12 +462,11 @@ function setPromiseHandled(promise) {
438462
PromisePrototypeThen(promise, undefined, () => {});
439463
}
440464

441-
async function nonOpFlush() {}
442-
443465
// Shared non-op for the start/pull/write algorithm callbacks, which all
444-
// follow the raw-callback contract (see createRawCallback*): the
466+
// follow the raw-callback contract (see createRawCallback*), and for the
467+
// missing one-shot methods of a user-provided sink or transformer: the
445468
// non-thenable return takes the allocation-free fast path in
446-
// thenAlgorithmResult().
469+
// thenAlgorithmResult() and settles promiseCall*() at once.
447470
function nonOpCallback() {}
448471

449472
async function nonOpCancel() {}
@@ -463,9 +486,7 @@ module.exports = {
463486
canCopyArrayBuffer,
464487
cloneAsUint8Array,
465488
copyArrayBuffer,
466-
createPromiseCallbackNoParams,
467489
createPromiseCallback1Param,
468-
createPromiseCallback2Params,
469490
createRawCallback1Param,
470491
createRawCallback2Params,
471492
customInspect,
@@ -489,9 +510,9 @@ module.exports = {
489510
materializeQueue,
490511
nonOpCallback,
491512
nonOpCancel,
492-
nonOpFlush,
493-
494513
peekQueueValue,
514+
promiseCall1Param,
515+
promiseCallNoParams,
495516
rejectedHandledRecord,
496517
resetQueue,
497518
resolvedRecord,

0 commit comments

Comments
 (0)