stream: create stream/iter from() values without async generators

from() of a string, ArrayBuffer, ArrayBufferView or array of
Uint8Arrays returns an iterable whose iterators are async generators
yielding the value's batches. Creating and resuming an async generator
costs a generator object, its frame and several promises, which
dominates the cost of a short stream: creating and reading a stream of
one 16-byte chunk spent a quarter of its time collecting garbage.

Iterate such values with a small iterator that does what the generator
did: each iteration starts over and yields the same batches, bounded
as before, and return() and throw() end it, throw() rejecting with its
argument.

Creating and reading a stream of one 16-byte chunk with from() is about
1.8 times faster, with 900 bytes less allocated per stream.

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:25 +01:00
1 parent eb3a8d2348
commit 43a5b9c5b4
2 files changed
+91 -29

No files matched your search

+61 -29
View File
@@ -1335,6 +1335,64 @@ function fromSync(input) {
};
}
/**
* An async iterable yielding the chunks of `batch`, a Uint8Array[], in
* batches of at most FROM_BATCH_SIZE chunks, for from() of values that need
* no normalization. Each iterator does what an async generator yielding the
* batches would, without the generator object and the promises and frames
* of resuming it: next() gives the batches, then done; return() and throw()
* end the iteration, throw() rejecting with its argument.
* @param {Uint8Array[]} batch
* @returns {AsyncIterable<Uint8Array[]>}
*/
function createBatchSource(batch) {
return {
__proto__: null,
[SymbolAsyncIterator]() {
return createBatchIterator(batch);
},
[kValidatedSource]: true,
};
}
function createBatchIterator(batch) {
let index = 0;
// The next batch, or null once done.
function nextBatch() {
if (index >= batch.length) {
index = batch.length;
return null;
}
if (index === 0 && batch.length <= FROM_BATCH_SIZE) {
index = batch.length;
return batch;
}
const start = index;
index += FROM_BATCH_SIZE;
return ArrayPrototypeSlice(batch, start, index);
}
return ObjectSetPrototypeOf({
next() {
const value = nextBatch();
return PromiseResolve(value === null ?
new IterResult(true, undefined) : new IterResult(false, value));
},
return(value) {
index = batch.length;
return PromiseResolve(new IterResult(true, value));
},
throw(error) {
index = batch.length;
return PromiseReject(error);
},
[SymbolAsyncIterator]() {
return this;
},
}, null);
}
/**
* Create a ByteStreamReadable from a ByteInput or Streamable.
* @param {string|ArrayBuffer|ArrayBufferView|Iterable|AsyncIterable} input
@@ -1347,14 +1405,7 @@ function from(input) {
// Check for primitives first (ByteInput)
if (isPrimitiveChunk(input)) {
const chunk = primitiveToUint8Array(input);
return {
__proto__: null,
async *[SymbolAsyncIterator]() {
yield [chunk];
},
[kValidatedSource]: true,
};
return createBatchSource([primitiveToUint8Array(input)]);
}
// Check toAsyncStreamable protocol (takes precedence over toStreamable and
@@ -1392,31 +1443,12 @@ function from(input) {
// the throughput benefit of batched processing.
if (ArrayIsArray(input)) {
if (input.length === 0) {
return {
__proto__: null,
async *[SymbolAsyncIterator]() {
// Empty - yield nothing
},
[kValidatedSource]: true,
};
return createBatchSource(input);
}
if (isUint8Array(input[0])) {
const allUint8 = ArrayPrototypeEvery(input, isUint8Array);
if (allUint8) {
const batch = input;
return {
__proto__: null,
async *[SymbolAsyncIterator]() {
if (batch.length <= FROM_BATCH_SIZE) {
yield batch;
} else {
for (let i = 0; i < batch.length; i += FROM_BATCH_SIZE) {
yield ArrayPrototypeSlice(batch, i, i + FROM_BATCH_SIZE);
}
}
},
[kValidatedSource]: true,
};
return createBatchSource(input);
}
}
}
@@ -828,7 +828,37 @@ async function testFromReturnsItsOwnResults() {
}
}
// from() of a value needing no normalization reads like an async generator
// yielding its batches: each iteration starts over, return() and throw()
// end it, and arrays are yielded in bounded batches.
async function testFromValueIteration() {
const chunk = new Uint8Array([1]);
const source = from(chunk);
for (let i = 0; i < 2; i++) {
const batches = await Array.fromAsync(source);
assert.deepStrictEqual(batches, [[chunk]]);
}
let iterator = source[Symbol.asyncIterator]();
assert.deepStrictEqual({ ...await iterator.return(5) },
{ done: true, value: 5 });
assert.strictEqual((await iterator.next()).done, true);
iterator = source[Symbol.asyncIterator]();
assert.deepStrictEqual((await iterator.next()).value, [chunk]);
const error = new Error('thrown');
await assert.rejects(iterator.throw(error), error);
assert.strictEqual((await iterator.next()).done, true);
const chunks = Array.from({ length: 300 }, () => chunk);
const lengths = (await Array.fromAsync(from(chunks)))
.map((batch) => batch.length);
assert.deepStrictEqual(lengths, [128, 128, 44]);
assert.deepStrictEqual(await Array.fromAsync(from([])), []);
}
Promise.all([
testFromValueIteration(),
testFromReturnsItsOwnResults(),
testFromString(),
testFromAsyncGenerator(),