Skip to content

Commit d7786f1

Browse files
jasnelladuh95
authored andcommitted
stream: run stream/iter transforms without async generators
pipeTo() and pull() ran a pipeline of transforms through two async generator layers for every batch: one applying each run of stateless transforms, and the pipeline itself, which checks for an abort before passing each batch on and cleans up when done. Each costs several promises and an async frame per batch. Write both out by hand, behaving the same way: the source is opened by the first next(), calls made while one is in progress are queued, an error from the source ends the pipeline without closing it, an error from a transform closes it, return() and throw() close what is being read, and the transforms' signal is aborted when the pipeline fails or is stopped early. Transforms returning batches or chunks synchronously take a single promise per batch for each layer; results that have to be waited for or normalized asynchronously, the flush, and stateful transforms are still handled by async generators. With an async generator yielding 16-byte chunks, one per batch, pipeTo() through one or two stateless transforms allocates about 900 bytes less per chunk and is about 38% faster. The queue of operations for these iterators moves to utils.js. New tests cover closing the source and aborting the transforms' signal. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 17299c9 commit d7786f1

4 files changed

Lines changed: 729 additions & 174 deletions

File tree

‎lib/internal/streams/iter/from.js‎

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
ArrayIsArray,
1111
ArrayPrototypeEvery,
1212
ArrayPrototypePush,
13-
ArrayPrototypeShift,
1413
ArrayPrototypeSlice,
1514
DataViewPrototypeGetBuffer,
1615
DataViewPrototypeGetByteLength,
@@ -56,7 +55,11 @@ const {
5655

5756
const {
5857
IterResult,
58+
kActive,
59+
kDone,
5960
kResolvedPromise,
61+
kStart,
62+
createOperationQueue,
6063
getProtocolMethod,
6164
toUint8Array,
6265
} = require('internal/streams/iter/utils');
@@ -666,56 +669,6 @@ async function* normalizeAsyncSourceValue(value, context,
666669
}
667670
}
668671

669-
const kStart = 0;
670-
const kActive = 1;
671-
const kDone = 2;
672-
673-
/**
674-
* Serializes the operations of a hand-written async iterator the way an
675-
* async generator queues its requests: an operation started while another
676-
* is in progress waits until it has settled.
677-
* @returns {{ run: Function, settled: Function, release: Function }}
678-
*/
679-
function createOperationQueue() {
680-
let busy = false;
681-
let queue = null;
682-
683-
function drain() {
684-
if (busy || queue === null) return;
685-
const { 0: method, 1: arg, 2: resolve, 3: reject } =
686-
ArrayPrototypeShift(queue);
687-
if (queue.length === 0) queue = null;
688-
busy = true;
689-
PromisePrototypeThen(method(arg), resolve, reject);
690-
}
691-
692-
return {
693-
__proto__: null,
694-
// Run `method(arg)`, which returns a promise and must call settled()
695-
// once its result is known, now or after the operations before it.
696-
run(method, arg) {
697-
if (busy || queue !== null) {
698-
const { promise, resolve, reject } = PromiseWithResolvers();
699-
queue ??= [];
700-
ArrayPrototypePush(queue, [method, arg, resolve, reject]);
701-
return promise;
702-
}
703-
busy = true;
704-
return method(arg);
705-
},
706-
settled() {
707-
busy = false;
708-
if (queue !== null) PromisePrototypeThen(kResolvedPromise, drain);
709-
},
710-
// Like settled(), but start the next queued operation now, as an async
711-
// generator does once the await of a yield or return completes.
712-
release() {
713-
busy = false;
714-
drain();
715-
},
716-
};
717-
}
718-
719672
/**
720673
* Iterator normalizing an async iterable source into batches of Uint8Array.
721674
*

0 commit comments

Comments
 (0)