stream: wait without an async function in stream/iter from()

The iterator through which from() reads an async source, to stay
cancellable while the source is pending, had an async next() that
awaited waitForNormalization() for every value: an async function
frame and promise, plus another promise and reactions for the wait.

Make next() a plain function that waits for the source's result with a
single promise, which a cancellation rejects directly. The result
checks, the closing of the source on cancellation and the precedence
between the source's result and a cancellation are unchanged.

With an async generator yielding 16-byte chunks, this allocates about
450 bytes less per chunk; pipeTo() is about 9% faster and iterating
from() about 11% 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>
This commit is contained in:
James M Snell authored and Antoine du Hamel committed 2026-11-23 12:34:23 +01:00
1 parent a70e8eb4c3
commit eac3dfacb7
1 file changed
+83 -26
+83 -26
View File
@@ -17,6 +17,7 @@ const {
FunctionPrototypeCall,
ObjectSetPrototypeOf,
PromisePrototypeThen,
PromiseReject,
PromiseResolve,
PromiseWithResolvers,
Symbol,
@@ -414,38 +415,94 @@ function yieldNormalizationAbortable(source, context) {
}
}
return ObjectSetPrototypeOf({
async next() {
if (completed) {
return new IterResult(true, undefined);
}
// Settle a pending next() with a result of the source's next(). Throws
// (to be handled by the caller) like the checks it replaces would.
function toIterResult(result) {
throwIfNormalizationCancelled(context);
if ((typeof result !== 'object' && typeof result !== 'function') ||
result === null) {
throw new ERR_INVALID_RETURN_VALUE(
'an object', 'iterator.next()', result);
}
if (result.done) {
reading = false;
throwIfNormalizationCancelled(context);
completed = true;
closed = true;
return new IterResult(true, result.value);
}
const value = result.value;
reading = false;
throwIfNormalizationCancelled(context);
return new IterResult(false, value);
}
// Reject a pending next(), closing the source first if the
// normalization has been cancelled.
function rejectNext(reject, error) {
if (context.cancelled) {
PromisePrototypeThen(closeSource(true), () => {
reading = false;
reject(error);
});
return;
}
reading = false;
reject(error);
}
return ObjectSetPrototypeOf({
// next() runs for every value of the source, so instead of being an
// async function awaiting waitForNormalization(), it waits for the
// source with a single promise, which a cancellation rejects
// directly.
next() {
if (completed) {
return PromiseResolve(new IterResult(true, undefined));
}
if (context.cancelled) return PromiseReject(context.reason);
reading = true;
const { promise, resolve, reject } = PromiseWithResolvers();
let next;
try {
const next = FunctionPrototypeCall(nextMethod, iterator);
const result = await waitForNormalization(next, context);
if ((typeof result !== 'object' && typeof result !== 'function') ||
result === null) {
throw new ERR_INVALID_RETURN_VALUE(
'an object', 'iterator.next()', result);
}
if (result.done) {
reading = false;
throwIfNormalizationCancelled(context);
completed = true;
closed = true;
return new IterResult(true, result.value);
}
const value = result.value;
reading = false;
throwIfNormalizationCancelled(context);
return new IterResult(false, value);
next = FunctionPrototypeCall(nextMethod, iterator);
} catch (error) {
if (context.cancelled) await closeSource(true);
reading = false;
throw error;
rejectNext(reject, error);
return promise;
}
// The first of the source's result and a cancellation settles
// next(); whichever comes later is ignored.
let settled = false;
const onCancel = () => {
if (settled) return;
settled = true;
if (context.resolve === onCancel) context.resolve = null;
rejectNext(reject, context.reason);
};
context.resolve = onCancel;
PromisePrototypeThen(
PromiseResolve(next),
(result) => {
if (settled) return;
settled = true;
if (context.resolve === onCancel) context.resolve = null;
let iterResult;
try {
iterResult = toIterResult(result);
} catch (error) {
rejectNext(reject, error);
return;
}
resolve(iterResult);
},
(error) => {
if (settled) return;
settled = true;
if (context.resolve === onCancel) context.resolve = null;
rejectNext(reject, error);
});
return promise;
},
async return(value) {
await closeSource(