stream: pump stream/iter Broadcast.from() sources with fewer layers

Broadcast.from() read its source through yieldAbortable(), adding a
promise, a race and an operation queue to every batch, and wrote each
batch with the public writevSync(), converting the batch from() had
already normalized as a Web IDL sequence and checking every chunk
again.

Read the source as pipeTo() does with a signal: one abort listener for
the whole pump, with raceSignal(), and, for a sync source with a
'strict' or 'unbounded' policy, batches read synchronously with
from()'s kNextSyncBatch while the writes succeed synchronously. The
first batch is still read asynchronously, so that it is written after
Broadcast.from() has returned and its consumers have been created; with
'drop-oldest' or 'drop-newest', whose writes always succeed, every batch
is. Write batches to the writer without converting them again. As
before, a cancellation or an abort closes the source without waiting
for a pending read, and an error writing a batch closes it.

With two consumers of Broadcast.from() of 16-byte chunks one per batch,
reading a sync source is about 1.9 times faster, and an async source
about 1.4 times 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:26 +01:00
1 parent 28c874043c
commit ccefb83dfe
2 files changed
+133 -13

No files matched your search

+70 -13
View File
@@ -49,6 +49,7 @@ const {
from,
isAsyncIterable,
isSyncIterable,
kNextSyncBatch,
} = require('internal/streams/iter/from');
const {
@@ -70,8 +71,8 @@ const {
onSignalAbort,
parsePullArgs,
toWriterUint8Array,
raceSignal,
validateBatchEntry,
yieldAbortable,
validateBudget,
} = require('internal/streams/iter/utils');
const {
@@ -93,6 +94,10 @@ const kOnCancel = Symbol('kOnCancel');
const kPendingWriteRemoved = Symbol('kPendingWriteRemoved');
const kNoBroadcastError = Symbol('kNoBroadcastError');
const kSetFactorySignal = Symbol('kSetFactorySignal');
// Write a batch of from() (already a Uint8Array[]), without converting it:
// synchronously, returning whether it was written, or asynchronously.
const kWriteBatchSync = Symbol('kWriteBatchSync');
const kWriteBatch = Symbol('kWriteBatch');
function raceEndWithSignal(promise, signal) {
if (!signal) return promise;
@@ -721,6 +726,20 @@ class BroadcastWriter {
return false;
}
[kWriteBatchSync](chunks) {
if (this.#state !== 'open') return false;
const batch = createBatchEntry(chunks);
if (this.#broadcast[kWrite](batch)) {
this.#totalBytes += batch.byteLength;
return true;
}
return false;
}
[kWriteBatch](chunks, signal) {
return this.#writeBatchSlow(createBatchEntry(chunks), signal);
}
end(options) {
const signal = getWriterSignal(options);
if (this.#state === 'errored') return PromiseReject(this.#error);
@@ -955,20 +974,58 @@ const Broadcast = {
};
result.broadcast[kOnCancel] = onCancel;
const pump = async () => {
const w = result.writer;
try {
if (isAsyncIterable(source)) {
for await (const chunks of yieldAbortable(source, controller.signal)) {
controller.signal.throwIfAborted();
if (ArrayIsArray(chunks)) {
if (!w.writevSync(chunks)) {
await w.writev(chunks, signal ? { signal } : undefined);
}
} else if (!w.writeSync(chunks)) {
await w.write(chunks, signal ? { signal } : undefined);
const w = result.writer;
// Batches of a sync source are read synchronously, as pipeTo() reads
// them, while the writes succeed synchronously. With a 'drop-oldest' or
// 'drop-newest' policy writes always do, so the whole source would be
// written before any consumer reads: read it asynchronously.
const policy = result.broadcast.backpressurePolicy;
const readSync = policy === 'strict' || policy === 'unbounded';
// Read as for await...of does, stopping once the broadcast is
// cancelled or its signal aborts; see raceSignal().
const abortState = { __proto__: null, aborted: false };
const consume = async (iterator) => {
const nextSyncBatch = readSync ? iterator[kNextSyncBatch] : undefined;
// The first batch is read with next(), so that it is written after
// Broadcast.from() has returned and its consumers have been created.
let first = true;
for (;;) {
let chunks = nextSyncBatch !== undefined && !first ?
FunctionPrototypeCall(nextSyncBatch, iterator) : undefined;
first = false;
if (chunks === undefined) {
const result = await iterator.next();
if (result.done || abortState.aborted) return;
chunks = result.value;
} else if (chunks === null) {
return;
}
try {
if (!w[kWriteBatchSync](chunks)) {
await w[kWriteBatch](chunks, signal);
}
} catch (error) {
// As for await closes the source when its body throws, ignoring
// errors from closing it.
if (!abortState.aborted) {
try {
await iterator.return?.();
} catch {
// The error writing the batch is thrown.
}
}
throw error;
}
if (abortState.aborted) return;
}
};
const pump = async () => {
try {
controller.signal.throwIfAborted();
if (isAsyncIterable(source)) {
await raceSignal(source[SymbolAsyncIterator](), controller.signal,
abortState, consume);
} else if (isSyncIterable(source)) {
for (const chunks of source) {
controller.signal.throwIfAborted();
@@ -159,6 +159,67 @@ async function testBroadcastFromCancelWhileBlocked() {
assert.strictEqual(sourceReturned, true);
}
// A sync source is written as the broadcast has room, including values
// normalized asynchronously, and its error reaches the consumers after the
// data before it. Cancelling the broadcast while the pump waits for room
// closes the source.
async function testBroadcastFromSyncSource() {
const reason = new Error('sync source boom');
function* source() {
yield [Uint8Array.of(1)];
yield Promise.resolve(Uint8Array.of(2));
yield 'c';
yield [Uint8Array.of(4)];
throw reason;
}
const { broadcast: bc } = Broadcast.from(source());
const read = async (consumer) => {
const values = [];
try {
for await (const batch of consumer) values.push(...batch.map((c) => c[0]));
} catch (error) {
values.push(error);
}
return values;
};
const results = await Promise.all([read(bc.push()), read(bc.push())]);
for (const values of results) {
assert.deepStrictEqual(values, [1, 2, 99, 4, reason]);
}
let closed = false;
function* endless() {
try {
for (;;) yield [new Uint8Array(1024)];
} finally {
closed = true;
}
}
const { broadcast: blocked } = Broadcast.from(endless(), { budget: 4096 });
const consumer = blocked.push()[Symbol.asyncIterator]();
await consumer.next();
await setImmediate();
assert.strictEqual(closed, false);
blocked.cancel();
await setImmediate();
assert.strictEqual(closed, true);
assert.strictEqual((await consumer.next()).done, true);
}
// With a 'drop-newest' policy, a sync source is still read one batch at a
// time while consumers read.
async function testBroadcastFromSyncSourceDropNewest() {
function* source() {
for (let i = 0; i < 8; i++) yield [Uint8Array.of(i)];
}
const { broadcast: bc } = Broadcast.from(source(), {
budget: 2, backpressure: 'drop-newest',
});
const values = [];
for await (const batch of bc.push()) values.push(...batch.map((c) => c[0]));
assert.deepStrictEqual(values, [0, 1, 2, 3, 4, 5, 6, 7]);
}
// =============================================================================
// Source error propagation via Broadcast.from()
// =============================================================================
@@ -229,6 +290,8 @@ Promise.all([
testAbortSignal(),
testAlreadyAbortedSignal(),
testBroadcastFromCancelWhileBlocked(),
testBroadcastFromSyncSource(),
testBroadcastFromSyncSourceDropNewest(),
testBroadcastFromSourceError(),
testBroadcastProtocolReturnsBroadcast(),
testBroadcastProtocolReturnsNull(),