Skip to content

Commit 44bbda1

Browse files
jasnelladuh95
authored andcommitted
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 <jasnell@gmail.com> PR-URL: #66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 35af1d5 commit 44bbda1

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,60 @@ function createAsyncPipeline(source, transforms, signal) {
10111011
return createAsyncTransformPipeline(source, transforms, signal);
10121012
}
10131013

1014-
async function* createAsyncPipelineWithoutTransforms(source, signal) {
1015-
// Check for abort
1016-
signal?.throwIfAborted();
1017-
yield* yieldAbortable(source, signal);
1014+
/**
1015+
* The pipeline without transforms: what an async generator doing
1016+
* `signal?.throwIfAborted(); yield* yieldAbortable(source, signal);` would
1017+
* do. With a signal, it is written out by hand to avoid an async generator
1018+
* layer for every batch: the signal is checked by the first next(), then
1019+
* every call is passed to the iterator of yieldAbortable(), which queues
1020+
* them and completes as the generator would.
1021+
* @param {AsyncIterable<Uint8Array[]>} source
1022+
* @param {AbortSignal} [signal]
1023+
* @returns {AsyncIterator<Uint8Array[]>}
1024+
*/
1025+
function createAsyncPipelineWithoutTransforms(source, signal) {
1026+
if (signal === undefined) return yieldFrom(source);
1027+
let state = kStart;
1028+
let iterator;
1029+
return ObjectSetPrototypeOf({
1030+
next() {
1031+
if (state === kStart) {
1032+
try {
1033+
// Check for abort
1034+
signal.throwIfAborted();
1035+
iterator = yieldAbortable(source, signal)[SymbolAsyncIterator]();
1036+
} catch (error) {
1037+
state = kDone;
1038+
return PromiseReject(error);
1039+
}
1040+
state = kActive;
1041+
} else if (state === kDone) {
1042+
return PromiseResolve(new IterResult(true, undefined));
1043+
}
1044+
return iterator.next();
1045+
},
1046+
return(value) {
1047+
if (state !== kActive) {
1048+
state = kDone;
1049+
return PromiseResolve(new IterResult(true, value));
1050+
}
1051+
return iterator.return(value);
1052+
},
1053+
throw(error) {
1054+
if (state !== kActive) {
1055+
state = kDone;
1056+
return PromiseReject(error);
1057+
}
1058+
return iterator.throw(error);
1059+
},
1060+
[SymbolAsyncIterator]() {
1061+
return this;
1062+
},
1063+
}, null);
1064+
}
1065+
1066+
async function* yieldFrom(source) {
1067+
yield* source;
10181068
}
10191069

10201070
/**
@@ -1303,10 +1353,8 @@ function pull(source, ...args) {
13031353
[SymbolAsyncIterator]() {
13041354
if (signal === undefined) {
13051355
const controller = new AbortController();
1306-
async function* pipeline() {
1307-
yield* createAsyncPipeline(normalized, transforms, controller.signal);
1308-
}
1309-
const iterator = pipeline();
1356+
const iterator = createAsyncPipeline(
1357+
normalized, transforms, controller.signal);
13101358
return ObjectSetPrototypeOf({
13111359
next(value) {
13121360
return iterator.next(value);
@@ -1341,10 +1389,7 @@ function createAbortablePullIterator(source, transforms, signal) {
13411389
if (!aborted) {
13421390
controller = new AbortController();
13431391
const iteratorSignal = AbortSignal.any([signal, controller.signal]);
1344-
async function* pipeline() {
1345-
yield* createAsyncPipeline(source, transforms, iteratorSignal);
1346-
}
1347-
iterator = pipeline();
1392+
iterator = createAsyncPipeline(source, transforms, iteratorSignal);
13481393
}
13491394

13501395
function onRejected(error) {

‎test/parallel/test-stream-iter-iterator-result.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
// Iterator results created by the stream/iter iterators themselves do not
55
// inherit from Object.prototype, so prototype pollution cannot affect them.
6-
// (Iterators implemented as async generators, such as the one returned by
7-
// pull(), return ordinary iterator results created by the engine.)
86

97
const common = require('../common');
108
const assert = require('assert');
119
const { inspect } = require('util');
1210
const {
1311
broadcast,
1412
from,
13+
pull,
1514
push,
1615
share,
1716
shareSync,
@@ -33,6 +32,11 @@ async function testAsyncIterators() {
3332
return readable;
3433
},
3534
'share()': () => share(from('a')).pull(),
35+
'pull()': () => pull(from('a')),
36+
'pull() with a signal': () => pull(from('a'), {
37+
signal: new AbortController().signal,
38+
}),
39+
'pull() with a transform': () => pull(from('a'), (chunks) => chunks),
3640
'from() of an async iterable': () => from((async function*() {
3741
yield 'a';
3842
})()),

‎test/parallel/test-stream-iter-pull-async.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ async function testPullReturnWhileSourceNextPending() {
314314
]);
315315

316316
assert.notStrictEqual(result, timeout);
317-
assert.deepStrictEqual(result, { value: undefined, done: true });
317+
assert.deepStrictEqual({ ...result }, { value: undefined, done: true });
318318
await next;
319319
}
320320

0 commit comments

Comments
 (0)