Skip to content

Commit eb3a8d2

Browse files
jasnelladuh95
authored andcommitted
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: #66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent c35e05d commit eb3a8d2

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎doc/api/stream_iter.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ Objects implementing `Symbol.for('Stream.toAsyncStreamable')` or
602602
precedence over the iteration protocols (`Symbol.asyncIterator`,
603603
`Symbol.iterator`).
604604

605-
The readable of a [`push()`][] stream without transforms and the iterables
606-
returned by [`fromReadable()`][] already yield normalized batches, so `from()`
607-
returns them unchanged.
605+
The readable of a [`push()`][] stream without transforms, the iterables
606+
returned by [`fromReadable()`][], and the results of `from()` itself already
607+
yield normalized batches, so `from()` returns them unchanged.
608608

609609
```mjs
610610
import { Buffer } from 'node:buffer';

‎lib/internal/streams/iter/from.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ function createNormalizationIterator(createIterator) {
180180
[SymbolAsyncIterator]() {
181181
return this;
182182
},
183+
[kValidatedSource]: true,
183184
}, null);
184185
}
185186
if (iterator[kNextSyncBatch] !== undefined) {
@@ -202,6 +203,7 @@ function createNormalizationIterator(createIterator) {
202203
[SymbolAsyncIterator]() {
203204
return this;
204205
},
206+
[kValidatedSource]: true,
205207
}, null);
206208
}
207209
return ObjectSetPrototypeOf({
@@ -220,6 +222,7 @@ function createNormalizationIterator(createIterator) {
220222
[SymbolAsyncIterator]() {
221223
return this;
222224
},
225+
[kValidatedSource]: true,
223226
}, null);
224227
}
225228

@@ -229,6 +232,7 @@ function createNormalizationSource(createIterator) {
229232
[SymbolAsyncIterator]() {
230233
return createNormalizationIterator(createIterator);
231234
},
235+
[kValidatedSource]: true,
232236
};
233237
}
234238

@@ -1349,6 +1353,7 @@ function from(input) {
13491353
async *[SymbolAsyncIterator]() {
13501354
yield [chunk];
13511355
},
1356+
[kValidatedSource]: true,
13521357
};
13531358
}
13541359

@@ -1392,6 +1397,7 @@ function from(input) {
13921397
async *[SymbolAsyncIterator]() {
13931398
// Empty - yield nothing
13941399
},
1400+
[kValidatedSource]: true,
13951401
};
13961402
}
13971403
if (isUint8Array(input[0])) {
@@ -1409,6 +1415,7 @@ function from(input) {
14091415
}
14101416
}
14111417
},
1418+
[kValidatedSource]: true,
14121419
};
14131420
}
14141421
}

‎test/parallel/test-stream-iter-from-async.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,35 @@ async function testFromSyncSourceQueuesBehindReturn() {
801801
]);
802802
}
803803

804+
// from() returns its own results unchanged: they already yield normalized
805+
// batches, and normalizing them again would add a layer to every read.
806+
async function testFromReturnsItsOwnResults() {
807+
async function* asyncSource() {
808+
yield 'a';
809+
yield [new Uint8Array([98])];
810+
}
811+
812+
function* syncSource() {
813+
yield 'c';
814+
yield new Uint8Array([100]);
815+
}
816+
const inputs = [
817+
[asyncSource(), 'ab'],
818+
[syncSource(), 'cd'],
819+
['ef', 'ef'],
820+
[[new Uint8Array([103]), new Uint8Array([104])], 'gh'],
821+
[[], ''],
822+
[{ [Symbol.for('Stream.toAsyncStreamable')]() { return 'ij'; } }, 'ij'],
823+
];
824+
for (const [input, expected] of inputs) {
825+
const normalized = from(input);
826+
assert.strictEqual(from(normalized), normalized);
827+
assert.strictEqual(await text(from(from(normalized))), expected);
828+
}
829+
}
830+
804831
Promise.all([
832+
testFromReturnsItsOwnResults(),
805833
testFromString(),
806834
testFromAsyncGenerator(),
807835
testFromAsyncIteratorResultShapes(),

0 commit comments

Comments
 (0)