stream: return stream/iter from() results unchanged from from()

from() returns sources known to yield normalized batches unchanged,
such as the readable of a push() stream, but not its own results. Since
pipeTo(), pull() and the consumers call from() on their source, a
stream created with from() was normalized a second time when passed to
them, adding a layer to every read and hiding the fast paths that
pipeTo() uses to read sync and async sources.

Mark the results of from() as validated sources, so that from() returns
them unchanged, as the specification allows for async iterables created
by the implementation that yield only normalized batches.

Piping from(source) is now as fast as piping source: for a generator
yielding 16-byte chunks one per batch, about 2.8 times faster when it
is a sync generator and 1.7 times faster when it is an async one.

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 c35e05db05
commit eb3a8d2348
3 files changed
+38 -3

No files matched your search

+3 -3
View File
@@ -602,9 +602,9 @@ Objects implementing `Symbol.for('Stream.toAsyncStreamable')` or
precedence over the iteration protocols (`Symbol.asyncIterator`,
`Symbol.iterator`).
The readable of a [`push()`][] stream without transforms and the iterables
returned by [`fromReadable()`][] already yield normalized batches, so `from()`
returns them unchanged.
The readable of a [`push()`][] stream without transforms, the iterables
returned by [`fromReadable()`][], and the results of `from()` itself already
yield normalized batches, so `from()` returns them unchanged.
```mjs
import { Buffer } from 'node:buffer';
+7
View File
@@ -180,6 +180,7 @@ function createNormalizationIterator(createIterator) {
[SymbolAsyncIterator]() {
return this;
},
[kValidatedSource]: true,
}, null);
}
if (iterator[kNextSyncBatch] !== undefined) {
@@ -202,6 +203,7 @@ function createNormalizationIterator(createIterator) {
[SymbolAsyncIterator]() {
return this;
},
[kValidatedSource]: true,
}, null);
}
return ObjectSetPrototypeOf({
@@ -220,6 +222,7 @@ function createNormalizationIterator(createIterator) {
[SymbolAsyncIterator]() {
return this;
},
[kValidatedSource]: true,
}, null);
}
@@ -229,6 +232,7 @@ function createNormalizationSource(createIterator) {
[SymbolAsyncIterator]() {
return createNormalizationIterator(createIterator);
},
[kValidatedSource]: true,
};
}
@@ -1349,6 +1353,7 @@ function from(input) {
async *[SymbolAsyncIterator]() {
yield [chunk];
},
[kValidatedSource]: true,
};
}
@@ -1392,6 +1397,7 @@ function from(input) {
async *[SymbolAsyncIterator]() {
// Empty - yield nothing
},
[kValidatedSource]: true,
};
}
if (isUint8Array(input[0])) {
@@ -1409,6 +1415,7 @@ function from(input) {
}
}
},
[kValidatedSource]: true,
};
}
}
@@ -801,7 +801,35 @@ async function testFromSyncSourceQueuesBehindReturn() {
]);
}
// from() returns its own results unchanged: they already yield normalized
// batches, and normalizing them again would add a layer to every read.
async function testFromReturnsItsOwnResults() {
async function* asyncSource() {
yield 'a';
yield [new Uint8Array([98])];
}
function* syncSource() {
yield 'c';
yield new Uint8Array([100]);
}
const inputs = [
[asyncSource(), 'ab'],
[syncSource(), 'cd'],
['ef', 'ef'],
[[new Uint8Array([103]), new Uint8Array([104])], 'gh'],
[[], ''],
[{ [Symbol.for('Stream.toAsyncStreamable')]() { return 'ij'; } }, 'ij'],
];
for (const [input, expected] of inputs) {
const normalized = from(input);
assert.strictEqual(from(normalized), normalized);
assert.strictEqual(await text(from(from(normalized))), expected);
}
}
Promise.all([
testFromReturnsItsOwnResults(),
testFromString(),
testFromAsyncGenerator(),
testFromAsyncIteratorResultShapes(),