From 44bbda140cd80b3535307bffbc00d21839e7673f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 4 Oct 2026 05:30:30 +0000 Subject: [PATCH] stream: remove async generator layers from stream/iter pull() pull() read its pipeline through an async generator delegating to it with yield*, and without transforms the pipeline was another such generator around the source, each costing several promises per batch. The pipelines already start lazily and queue calls as an async generator does, so use the pipeline directly, and write the pipeline without transforms out by hand: it checks the signal on the first next(), then passes every call to the iterator reading the source. Results are now IterResult objects, like those of the other stream/iter iterators; one test compared them as ordinary objects. With an async generator yielding 16-byte chunks, one per batch, pull() is about 42% faster without transforms and 30% faster with a signal, and about 12% faster through a transform. Assisted-by: OpenCode Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/streams/iter/pull.js | 69 +++++++++++++++---- .../test-stream-iter-iterator-result.js | 8 ++- test/parallel/test-stream-iter-pull-async.js | 2 +- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index e300364a820..b59949ff1f3 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -1011,10 +1011,60 @@ function createAsyncPipeline(source, transforms, signal) { return createAsyncTransformPipeline(source, transforms, signal); } -async function* createAsyncPipelineWithoutTransforms(source, signal) { - // Check for abort - signal?.throwIfAborted(); - yield* yieldAbortable(source, signal); +/** + * The pipeline without transforms: what an async generator doing + * `signal?.throwIfAborted(); yield* yieldAbortable(source, signal);` would + * do. With a signal, it is written out by hand to avoid an async generator + * layer for every batch: the signal is checked by the first next(), then + * every call is passed to the iterator of yieldAbortable(), which queues + * them and completes as the generator would. + * @param {AsyncIterable} source + * @param {AbortSignal} [signal] + * @returns {AsyncIterator} + */ +function createAsyncPipelineWithoutTransforms(source, signal) { + if (signal === undefined) return yieldFrom(source); + let state = kStart; + let iterator; + return ObjectSetPrototypeOf({ + next() { + if (state === kStart) { + try { + // Check for abort + signal.throwIfAborted(); + iterator = yieldAbortable(source, signal)[SymbolAsyncIterator](); + } catch (error) { + state = kDone; + return PromiseReject(error); + } + state = kActive; + } else if (state === kDone) { + return PromiseResolve(new IterResult(true, undefined)); + } + return iterator.next(); + }, + return(value) { + if (state !== kActive) { + state = kDone; + return PromiseResolve(new IterResult(true, value)); + } + return iterator.return(value); + }, + throw(error) { + if (state !== kActive) { + state = kDone; + return PromiseReject(error); + } + return iterator.throw(error); + }, + [SymbolAsyncIterator]() { + return this; + }, + }, null); +} + +async function* yieldFrom(source) { + yield* source; } /** @@ -1303,10 +1353,8 @@ function pull(source, ...args) { [SymbolAsyncIterator]() { if (signal === undefined) { const controller = new AbortController(); - async function* pipeline() { - yield* createAsyncPipeline(normalized, transforms, controller.signal); - } - const iterator = pipeline(); + const iterator = createAsyncPipeline( + normalized, transforms, controller.signal); return ObjectSetPrototypeOf({ next(value) { return iterator.next(value); @@ -1341,10 +1389,7 @@ function createAbortablePullIterator(source, transforms, signal) { if (!aborted) { controller = new AbortController(); const iteratorSignal = AbortSignal.any([signal, controller.signal]); - async function* pipeline() { - yield* createAsyncPipeline(source, transforms, iteratorSignal); - } - iterator = pipeline(); + iterator = createAsyncPipeline(source, transforms, iteratorSignal); } function onRejected(error) { diff --git a/test/parallel/test-stream-iter-iterator-result.js b/test/parallel/test-stream-iter-iterator-result.js index ba4c35686de..96cbdc6ec18 100644 --- a/test/parallel/test-stream-iter-iterator-result.js +++ b/test/parallel/test-stream-iter-iterator-result.js @@ -3,8 +3,6 @@ // Iterator results created by the stream/iter iterators themselves do not // inherit from Object.prototype, so prototype pollution cannot affect them. -// (Iterators implemented as async generators, such as the one returned by -// pull(), return ordinary iterator results created by the engine.) const common = require('../common'); const assert = require('assert'); @@ -12,6 +10,7 @@ const { inspect } = require('util'); const { broadcast, from, + pull, push, share, shareSync, @@ -33,6 +32,11 @@ async function testAsyncIterators() { return readable; }, 'share()': () => share(from('a')).pull(), + 'pull()': () => pull(from('a')), + 'pull() with a signal': () => pull(from('a'), { + signal: new AbortController().signal, + }), + 'pull() with a transform': () => pull(from('a'), (chunks) => chunks), 'from() of an async iterable': () => from((async function*() { yield 'a'; })()), diff --git a/test/parallel/test-stream-iter-pull-async.js b/test/parallel/test-stream-iter-pull-async.js index c971ab02ff1..308e3a0ebb9 100644 --- a/test/parallel/test-stream-iter-pull-async.js +++ b/test/parallel/test-stream-iter-pull-async.js @@ -314,7 +314,7 @@ async function testPullReturnWhileSourceNextPending() { ]); assert.notStrictEqual(result, timeout); - assert.deepStrictEqual(result, { value: undefined, done: true }); + assert.deepStrictEqual({ ...result }, { value: undefined, done: true }); await next; }