mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
stream: read stream/iter consumer sources without a layer per signal
bytes(), text(), arrayBuffer() and array() with a signal read their source through an abortable iterator, which adds a promise, a reaction and an iterator result to every batch. Read the source as for await...of does, checking between batches whether the signal has aborted, and race the whole read once with the abort, as pipeTo() does without transforms: move that race to raceSignal() in utils.js and use it for both. When the signal aborts, the consumer rejects at once with the abort reason, after calling the source's return() without waiting for it, and the source is not read further. For 16-byte chunks one per batch, array() of a sync generator with a signal is about 1.9 times faster, and of an async generator about 1.35 times faster: about as fast as without a signal. 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:
1 parent
0b71627e30
commit
3ae4fa2409
4 files changed
+122
-59
No files matched your search
@@ -58,6 +58,7 @@ const {
|
||||
kNullOnceOption,
|
||||
concatBytes,
|
||||
getProtocolMethod,
|
||||
raceSignal,
|
||||
recordChunk,
|
||||
validateRecordedChunks,
|
||||
yieldAbortable,
|
||||
@@ -150,10 +151,7 @@ async function collectAsync(source, signal, limit) {
|
||||
|
||||
// Slow path: with signal or limit checks
|
||||
let totalBytes = 0;
|
||||
const iterable = signal ? yieldAbortable(normalized, signal) : normalized;
|
||||
|
||||
for await (const batch of iterable) {
|
||||
signal?.throwIfAborted();
|
||||
function recordBatch(batch) {
|
||||
for (let i = 0; i < batch.length; i++) {
|
||||
totalBytes += recordChunk(chunks, checks, batch[i]);
|
||||
if (limit !== undefined && totalBytes > limit) {
|
||||
@@ -162,6 +160,33 @@ async function collectAsync(source, signal, limit) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!signal) {
|
||||
for await (const batch of normalized) recordBatch(batch);
|
||||
return validateRecordedChunks(chunks, checks);
|
||||
}
|
||||
|
||||
// Read as for await...of does, stopping once the signal aborts; see
|
||||
// raceSignal().
|
||||
const state = { __proto__: null, aborted: false };
|
||||
async function consume(iterator) {
|
||||
for (;;) {
|
||||
const result = await iterator.next();
|
||||
if (result.done || state.aborted) return;
|
||||
try {
|
||||
recordBatch(result.value);
|
||||
} catch (error) {
|
||||
// As for await closes the source when its body throws, ignoring
|
||||
// errors from closing it.
|
||||
try {
|
||||
await iterator.return?.();
|
||||
} catch {
|
||||
// The error recording the batch is thrown.
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
await raceSignal(normalized[SymbolAsyncIterator](), signal, state, consume);
|
||||
return validateRecordedChunks(chunks, checks);
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ const {
|
||||
fixedBatchToEntry,
|
||||
isTransformObject,
|
||||
parsePullArgs,
|
||||
raceSignal,
|
||||
snapshotFixedBatch,
|
||||
snapshotTransform,
|
||||
toUint8Array,
|
||||
@@ -1740,7 +1741,7 @@ async function pipeTo(source, ...args) {
|
||||
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;
|
||||
const abortState = { __proto__: null, aborted: false };
|
||||
|
||||
let totalBytes = 0;
|
||||
const hasWritev = typeof writer.writev === 'function';
|
||||
@@ -1794,11 +1795,11 @@ async function pipeTo(source, ...args) {
|
||||
// ignoring errors closing it.
|
||||
async function pipeSyncSource(iterator) {
|
||||
for (;;) {
|
||||
if (signalAborted) return;
|
||||
if (abortState.aborted) return;
|
||||
let batch = iterator[kNextSyncBatch]();
|
||||
if (batch === undefined) {
|
||||
const result = await iterator.next();
|
||||
if (result.done || signalAborted) return;
|
||||
if (result.done || abortState.aborted) return;
|
||||
batch = result.value;
|
||||
} else if (batch === null) {
|
||||
return;
|
||||
@@ -1807,7 +1808,7 @@ async function pipeTo(source, ...args) {
|
||||
const p = writeBatch(batch);
|
||||
if (p) await p;
|
||||
} catch (error) {
|
||||
if (!signalAborted) {
|
||||
if (!abortState.aborted) {
|
||||
try {
|
||||
await iterator.return();
|
||||
} catch {
|
||||
@@ -1824,12 +1825,12 @@ async function pipeTo(source, ...args) {
|
||||
async function pipeSourceWithSignal(iterator) {
|
||||
for (;;) {
|
||||
const result = await iterator.next();
|
||||
if (result.done || signalAborted) return;
|
||||
if (result.done || abortState.aborted) return;
|
||||
try {
|
||||
const p = writeBatch(result.value);
|
||||
if (p) await p;
|
||||
} catch (error) {
|
||||
if (!signalAborted) {
|
||||
if (!abortState.aborted) {
|
||||
try {
|
||||
await iterator.return();
|
||||
} catch {
|
||||
@@ -1841,53 +1842,6 @@ async function pipeTo(source, ...args) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -1967,8 +1921,9 @@ async function pipeTo(source, ...args) {
|
||||
// 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);
|
||||
await raceSignal(iterator, signal, abortState,
|
||||
iterator[kNextSyncBatch] !== undefined ?
|
||||
pipeSyncSource : pipeSourceWithSignal);
|
||||
} else if (normalized[kNextSyncBatch] !== undefined) {
|
||||
await pipeSyncSource(normalized);
|
||||
} else if (normalized[kNextUncancellable] !== undefined) {
|
||||
|
||||
@@ -158,6 +158,67 @@ class PipelineAbort {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for `consume(iterator)`, which reads `iterator` (a normalized source)
|
||||
* to the end, unless `signal` aborts first. On an abort, `state.aborted` is
|
||||
* set, the source is closed without waiting, ignoring errors, and the
|
||||
* returned promise rejects with the abort reason at once, as a pull()
|
||||
* pipeline with `signal` would reject its pending pull; `consume` must check
|
||||
* `state.aborted` after each read and stop. Closing and rejecting wait for a
|
||||
* microtask, as the signal can abort while the source is called.
|
||||
*
|
||||
* This takes one listener and one promise for the whole read, instead of a
|
||||
* layer around the source adding a promise to every read.
|
||||
* @param {AsyncIterator} iterator
|
||||
* @param {AbortSignal} signal
|
||||
* @param {{ aborted: boolean }} state
|
||||
* @param {Function} consume
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
function raceSignal(iterator, signal, state, consume) {
|
||||
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;
|
||||
state.aborted = true;
|
||||
PromisePrototypeThen(kResolvedPromise, closeAndReject);
|
||||
}
|
||||
// Added first, as the signal can abort while the source is read.
|
||||
signal.addEventListener('abort', onAbort, kNullOnceOption);
|
||||
let consumed;
|
||||
try {
|
||||
consumed = consume(iterator);
|
||||
} catch (error) {
|
||||
consumed = PromiseReject(error);
|
||||
}
|
||||
PromisePrototypeThen(consumed, (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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an async source so each pending read is abort-aware.
|
||||
* @param {AsyncIterable<Uint8Array[]>} source - The source to read from.
|
||||
@@ -1013,6 +1074,7 @@ module.exports = {
|
||||
isTransformObject,
|
||||
onSignalAbort,
|
||||
parsePullArgs,
|
||||
raceSignal,
|
||||
snapshotTransform,
|
||||
toUint8Array,
|
||||
toWriterUint8Array,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { getEventListeners } = require('events');
|
||||
const {
|
||||
from,
|
||||
fromSync,
|
||||
@@ -272,7 +273,27 @@ async function testTextStringSource() {
|
||||
assert.strictEqual(result, 'direct-string');
|
||||
}
|
||||
|
||||
// With a signal, exceeding the limit closes the source, rejects with the
|
||||
// limit error, and leaves no listener on the signal.
|
||||
async function testBytesSignalAndLimit() {
|
||||
const ac = new AbortController();
|
||||
let closed = false;
|
||||
async function* source() {
|
||||
try {
|
||||
for (;;) yield [new Uint8Array(8)];
|
||||
} finally {
|
||||
closed = true;
|
||||
}
|
||||
}
|
||||
await assert.rejects(
|
||||
bytes(source(), { signal: ac.signal, limit: 20 }),
|
||||
{ code: 'ERR_OUT_OF_RANGE' });
|
||||
assert.strictEqual(closed, true);
|
||||
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
testBytesSignalAndLimit(),
|
||||
testBytesSyncBasic(),
|
||||
testBytesSyncLimit(),
|
||||
testBytesAsync(),
|
||||
|
||||
Reference in new issue
Block a user