From eac3dfacb7b9bfab4b787bd6c4fa3e013ee48704 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 4 Oct 2026 03:24:10 +0000 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/streams/iter/from.js | 109 +++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 26 deletions(-) diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index 5f0103b7baa..15ed395a42e 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -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(