Commit 09e0f99f9d9 for nodejs
commit 09e0f99f9d96f8b0c9d8269d22d1b7266046380c
Author: James M Snell <jasnell@gmail.com>
Date: Wed Oct 7 13:56:50 2026 +0000
stream: do not keep stream/iter from() sync sources busy for a tick
The iterator of from() over a sync iterable stayed busy until the
microtask after every result it produced synchronously, as an async
generator stays busy until the tick after a yield, so that a call made
synchronously after one was queued behind it. That cost a microtask per
batch, also when nothing was ever queued.
Settle each operation when its result is known instead, as the
normalizer of async sources does: a call made after one that finished
synchronously now runs at once, and calls queued while one was in
progress, such as behind a value normalized asynchronously, still run
a microtask later. Only calls made back to back without awaiting are
affected: a second next() reads the source during the call, and a
return() made after it no longer cancels it, as it did when it was
still queued. The other iterators of from(), for values and async
sources, already behaved this way. The queue's release() is no longer
used.
Iterating from() over a sync source yielding one-chunk batches is
about 1.38 times faster, now on par with a ReadableStream, and with
64-chunk batches about 7% faster.
Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js
index 0598859a189..551a91158c5 100644
--- a/lib/internal/streams/iter/from.js
+++ b/lib/internal/streams/iter/from.js
@@ -1323,19 +1323,15 @@ function createSyncSourceNormalizer(source, context) {
let done = false;
// A normalizeAsyncSourceValue() generator for the value being normalized.
let valueBatches = null;
- const { run, settled, release, idle } = createOperationQueue();
-
- // Results are often produced synchronously. An async generator stays busy
- // until the tick after a yield or return (both await their operand), so
- // that a next(), return() or throw() made synchronously after this one is
- // queued behind it; do the same.
- // TODO(@jasnell): This costs a microtask per batch. Running a call made
- // while the previous one has finished synchronously right away would make
- // for-await over from(syncSource) about 1.3x faster, at the cost of this
- // async generator parity for calls made back to back without awaiting
- // (see testFromSyncSourceQueuesBehindReturn).
+ const { run, settled, idle } = createOperationQueue();
+
+ // Results are often produced synchronously. Once one is, the next call
+ // runs at once, as for the async source normalizer; calls queued while
+ // an operation was in progress run a microtask later. (An async generator
+ // would stay busy until the tick after a yield, and queue a call made
+ // synchronously after one: that costs a microtask per batch.)
function finish(result) {
- PromisePrototypeThen(kResolvedPromise, release);
+ settled();
return result;
}
diff --git a/lib/internal/streams/iter/utils.js b/lib/internal/streams/iter/utils.js
index 7eb0da7dbfc..0191a4dc83d 100644
--- a/lib/internal/streams/iter/utils.js
+++ b/lib/internal/streams/iter/utils.js
@@ -604,7 +604,7 @@ class QueuedOperation {
* Serializes the operations of a hand-written async iterator the way an
* async generator queues its requests: an operation started while another
* is in progress waits until it has settled.
- * @returns {{ run: Function, settled: Function, release: Function }}
+ * @returns {{ run: Function, settled: Function, idle: Function }}
*/
function createOperationQueue() {
let busy = false;
@@ -639,12 +639,6 @@ function createOperationQueue() {
PromisePrototypeThen(kResolvedPromise, drain);
}
},
- // Like settled(), but start the next queued operation now, as an async
- // generator does once the await of a yield or return completes.
- release() {
- busy = false;
- drain();
- },
// Whether no operation is running or queued.
idle() {
return !busy && (queue === null || queue.length === 0);
diff --git a/test/parallel/test-stream-iter-from-async.js b/test/parallel/test-stream-iter-from-async.js
index 44befd8eb7e..a6bd4a48692 100644
--- a/test/parallel/test-stream-iter-from-async.js
+++ b/test/parallel/test-stream-iter-from-async.js
@@ -856,20 +856,52 @@ async function testFromSyncSourceReturnAndThrow() {
}
}
-async function testFromSyncSourceQueuesBehindReturn() {
- // A next() made synchronously after another waits for it, and so sees a
- // return() made synchronously after it as well.
- const log = [];
- const iterator = from(createLoggedSyncSource(log, [
+async function testFromSyncSourceCallOrder() {
+ // A next() made synchronously after another that finished synchronously
+ // runs at once, before a return() made after it.
+ let log = [];
+ let iterator = from(createLoggedSyncSource(log, [
[Uint8Array.of(1)], 'a', [Uint8Array.of(2)],
]))[Symbol.asyncIterator]();
- const results = [iterator.next(), iterator.next(), iterator.return()];
+ let results = [iterator.next(), iterator.next(), iterator.return()];
+ for (let i = 0; i < results.length; i++) {
+ await settle(log, `result ${i}`, results[i]);
+ }
+ assert.deepStrictEqual(log, [
+ 'iterator', 'next 0', 'next 1', 'result 0: 1', 'result 1: 1', 'return',
+ 'result 2: done',
+ ]);
+
+ // A call made while a value is normalized asynchronously is queued
+ // behind it, and so a return() made then cancels it.
+ log = [];
+ iterator = from(createLoggedSyncSource(log, [
+ [Uint8Array.of(1)], Promise.resolve(Uint8Array.of(2)), [Uint8Array.of(3)],
+ ]))[Symbol.asyncIterator]();
+ results = [iterator.next(), iterator.next(), iterator.next(),
+ iterator.return()];
for (let i = 0; i < results.length; i++) {
await settle(log, `result ${i}`, results[i]);
}
assert.deepStrictEqual(log, [
- 'iterator', 'next 0', 'next 1', 'return', 'result 0: 1',
- 'result 1: AbortError', 'result 2: done',
+ 'iterator', 'next 0', 'next 1', 'result 0: 1', 'return',
+ 'result 1: AbortError', 'result 2: done', 'result 3: done',
+ ]);
+
+ // A next() made synchronously after another reads the source at once.
+ log = [];
+ iterator = from(createLoggedSyncSource(log, [
+ [Uint8Array.of(1)], [Uint8Array.of(2)],
+ ]))[Symbol.asyncIterator]();
+ const first = iterator.next();
+ log.push('first called');
+ const second = iterator.next();
+ log.push('second called');
+ await settle(log, 'first', first);
+ await settle(log, 'second', second);
+ assert.deepStrictEqual(log, [
+ 'iterator', 'next 0', 'first called', 'next 1', 'second called',
+ 'first: 1', 'second: 1',
]);
}
@@ -975,5 +1007,5 @@ Promise.all([
testFromSyncSourceLargeAndEmptyBatches(),
testFromSyncSourceErrors(),
testFromSyncSourceReturnAndThrow(),
- testFromSyncSourceQueuesBehindReturn(),
+ testFromSyncSourceCallOrder(),
]).then(common.mustCall());