stream: keep stream/iter pipeTo() fast paths with a signal

Without transforms, pipeTo() with a signal read its source through an
abortable iterator, which adds a promise, a reaction and an iterator
result to every batch, and gave up reading sync sources synchronously.
Every write(), writev() and end() was passed a new options object.

Instead, read the source as without a signal, synchronously when
possible, checking between batches whether the signal has aborted, and
race the whole pipe once with the abort: one listener and one promise
for the pipe. When the signal aborts, the pipe rejects at once with the
abort reason, after calling the source's return() without waiting for
it, as a pull() pipeline with the signal rejects its pending pull, and
no other batch is read or written. Async sources are read with next(),
which return() can cancel while it is pending.

Pass the same options object to every write(), writev() and end(). This
departs from the specification, where WriteOptions is a dictionary,
converted to a new object for every call.

For 16-byte chunks one per batch, piping a sync generator with a signal
is about 3.9 times faster, as fast as without one, and piping an async
generator with a signal about 1.55 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:25 +01:00
1 parent e447e75aa6
commit 09dbcd02bf
3 files changed
+178 -19

No files matched your search

+3 -1
View File
@@ -674,7 +674,9 @@ added:
* `writer` {Object} Destination with `write(chunk)` method.
* `options` {Object}
* `signal` {AbortSignal} Abort the pipeline. Aborting fails the destination
writer unless `preventFail` is `true`.
writer unless `preventFail` is `true`. The signal is passed to the
writer's `write()`, `writev()` and `end()` in an options object, the same
object for every call.
* `preventClose` {boolean} If `true`, do not call `writer.end()` when
the source ends. **Default:** `false`.
* `preventFail` {boolean} If `true`, do not call `writer.fail()` on
+94 -18
View File
@@ -73,7 +73,6 @@ const {
toUint8Array,
validateBatchEntry,
validateByteView,
yieldAbortable,
} = require('internal/streams/iter/utils');
const {
converters,
@@ -1737,6 +1736,11 @@ async function pipeTo(source, ...args) {
const hasWriteSync = typeof writer.writeSync === 'function';
const normalized = from(source);
// The options passed to every write(), writev() and end().
const writeOptions = signal ? { __proto__: null, signal } : undefined;
// Without transforms, whether `signal` has aborted the pipe: the batches
// are then no longer read or written (see raceSignal()).
let signalAborted = false;
let totalBytes = 0;
const hasWritev = typeof writer.writev === 'function';
@@ -1757,9 +1761,7 @@ async function pipeTo(source, ...args) {
}
validateByteView(view);
}
const result = writer.write(
validateByteView(view),
signal ? { __proto__: null, signal } : undefined);
const result = writer.write(validateByteView(view), writeOptions);
if (result !== undefined) {
await result;
}
@@ -1792,10 +1794,11 @@ async function pipeTo(source, ...args) {
// ignoring errors closing it.
async function pipeSyncSource(iterator) {
for (;;) {
if (signalAborted) return;
let batch = iterator[kNextSyncBatch]();
if (batch === undefined) {
const result = await iterator.next();
if (result.done) return;
if (result.done || signalAborted) return;
batch = result.value;
} else if (batch === null) {
return;
@@ -1804,16 +1807,87 @@ async function pipeTo(source, ...args) {
const p = writeBatch(batch);
if (p) await p;
} catch (error) {
try {
await iterator.return();
} catch {
// The error writing the batch is thrown.
if (!signalAborted) {
try {
await iterator.return();
} catch {
// The error writing the batch is thrown.
}
}
throw error;
}
}
}
// Write the batches of a source read with next(), as the for await...of
// loop below does, stopping once `signal` aborts.
async function pipeSourceWithSignal(iterator) {
for (;;) {
const result = await iterator.next();
if (result.done || signalAborted) return;
try {
const p = writeBatch(result.value);
if (p) await p;
} catch (error) {
if (!signalAborted) {
try {
await iterator.return();
} catch {
// The error writing the batch is thrown.
}
}
throw error;
}
}
}
// Wait for `pipe(iterator)` to write the batches of `iterator`, the
// normalized source, unless `signal` aborts first. On an abort, the source
// is closed, without waiting, and the abort reason is thrown at once, as a
// pull() pipeline with `signal` would reject its pending pull; `piped`
// then stops before reading or writing another batch. This takes one
// listener and one promise for the whole pipe, instead of a layer reading
// the source for every batch. The listener is added first, as the signal
// can abort while the source is read.
function raceSignal(iterator, pipe) {
const { promise, resolve, reject } = PromiseWithResolvers();
let settled = false;
function closeAndReject() {
try {
const returnMethod = iterator.return;
if (typeof returnMethod === 'function') {
markPromiseAsHandled(PromiseResolve(
FunctionPrototypeCall(returnMethod, iterator)));
}
} catch {
// The abort takes precedence over errors closing the source.
}
reject(signal.reason);
}
function onAbort() {
if (settled) return;
settled = true;
signalAborted = true;
// The signal can abort while the source is called: close it once that
// call has returned.
PromisePrototypeThen(kResolvedPromise, closeAndReject);
}
signal.addEventListener('abort', onAbort, kNullOnceOption);
PromisePrototypeThen(pipe(iterator), (value) => {
if (settled) return;
settled = true;
signal.removeEventListener('abort', onAbort);
resolve(value);
}, (error) => {
if (settled) return;
settled = true;
signal.removeEventListener('abort', onAbort);
reject(error);
});
return promise;
}
// Write the batches of an async source as the for await...of loop below
// does. Nothing cancels the normalization while a batch is read: return()
// is only called after an error writing one.
@@ -1856,8 +1930,8 @@ async function pipeTo(source, ...args) {
if (!hasWritevSync ||
!writer.writevSync(validateBatchEntry(entry))) {
validateBatchEntry(entry);
const opts = signal ? { __proto__: null, signal } : undefined;
const writevResult = writer.writev(validateBatchEntry(entry), opts);
const writevResult = writer.writev(
validateBatchEntry(entry), writeOptions);
if (writevResult === undefined) {
validateBatchEntry(entry);
totalBytes += entry.byteLength;
@@ -1889,11 +1963,12 @@ async function pipeTo(source, ...args) {
if (transforms.length === 0) {
// Fast path: no transforms - iterate normalized source directly
if (signal) {
for await (const batch of yieldAbortable(normalized, signal)) {
signal.throwIfAborted();
const p = writeBatch(batch);
if (p) await p;
}
// Batches are read synchronously when possible, as without a signal.
// Async reads go through next(), which return() can cancel while it
// is pending, unlike kNextUncancellable.
const iterator = normalized[SymbolAsyncIterator]();
await raceSignal(iterator, iterator[kNextSyncBatch] !== undefined ?
pipeSyncSource : pipeSourceWithSignal);
} else if (normalized[kNextSyncBatch] !== undefined) {
await pipeSyncSource(normalized);
} else if (normalized[kNextUncancellable] !== undefined) {
@@ -1909,8 +1984,9 @@ async function pipeTo(source, ...args) {
normalized, transforms, signal, signal !== undefined);
if (signal) {
// The pipeline rejects its pending pull when `signal` aborts.
for await (const batch of pipeline) {
signal.throwIfAborted();
if (signal.aborted) throw signal.reason;
const p = writeBatch(batch);
if (p) await p;
}
@@ -1924,7 +2000,7 @@ async function pipeTo(source, ...args) {
if (!options.preventClose) {
if (!hasEndSync || writer.endSync() < 0) {
await writer.end?.(signal ? { __proto__: null, signal } : undefined);
await writer.end?.(writeOptions);
}
}
} catch (error) {
@@ -195,6 +195,84 @@ async function testSignalAbortedWhileReadingSource() {
}
}
// write(), writev() and end() are all passed the same options object.
async function testWriteOptionsShared() {
const ac = new AbortController();
const seen = [];
const writer = {
async write(chunk, options) { seen.push(options); },
async writev(chunks, options) { seen.push(options); },
async end(options) { seen.push(options); },
};
async function* source() {
yield [new Uint8Array([1])];
yield [new Uint8Array([2]), new Uint8Array([3])];
yield [new Uint8Array([4])];
}
await pipeTo(source(), writer, { signal: ac.signal });
assert.strictEqual(seen.length, 4);
assert.strictEqual(new Set(seen).size, 1);
assert.strictEqual(seen[0].signal, ac.signal);
}
// The signal aborting while a chunk is written synchronously stops the pipe:
// no other chunk is written, the source is closed, and the pipe rejects with
// the abort reason.
async function testAbortDuringWriteSync() {
for (const sync of [true, false]) {
const ac = new AbortController();
const reason = new Error('abort in writeSync');
const written = [];
let closed = false;
function* syncSource() {
try {
for (let i = 0; i < 5; i++) yield [new Uint8Array([i])];
} finally {
closed = true;
}
}
async function* asyncSource() {
try {
for (let i = 0; i < 5; i++) yield [new Uint8Array([i])];
} finally {
closed = true;
}
}
const writer = {
write: common.mustNotCall(),
writeSync(chunk) {
written.push(chunk[0]);
if (chunk[0] === 1) ac.abort(reason);
return true;
},
fail: common.mustCall((error) => assert.strictEqual(error, reason)),
};
await assert.rejects(
pipeTo(sync ? syncSource() : asyncSource(), writer,
{ signal: ac.signal }),
reason);
assert.deepStrictEqual(written, [0, 1]);
await setTimeout(1);
assert.strictEqual(closed, true);
}
}
// The signal aborting while a write is pending rejects the pipe at once,
// even if the write never completes.
async function testAbortWhileWritePending() {
const ac = new AbortController();
const reason = new Error('abort while writing');
const writer = {
write() {
ac.abort(reason);
return new Promise(() => {});
},
};
await assert.rejects(pipeTo(from('a'), writer, { signal: ac.signal }),
reason);
}
async function testSignalListenersRemoved() {
// No abort listener is left on the signal once reading completes, fails
// or is aborted.
@@ -226,4 +304,7 @@ Promise.all([
testPipeToLiveSignalWithTransformsCompletes(),
testSignalAbortedWhileReadingSource(),
testSignalListenersRemoved(),
testWriteOptionsShared(),
testAbortDuringWriteSync(),
testAbortWhileWritePending(),
]).then(common.mustCall());