mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
lib: stop refreshing composite AbortSignals
AbortSignal.any() follows its sources as soon as it creates a composite, so aborting a source synchronously aborts every composite that follows it. The aborted and reason getters and throwIfAborted() still walked the composite's sources on every call and dereferenced the WeakRef of each one, which is expensive because WeakRef.prototype.deref() adds its target to the kept objects of the current job. That walk can no longer find an aborted source of a composite that isn't aborted itself, so remove it. throwIfAborted() on a composite with two sources becomes about 17 times faster, the same as on a plain signal. Add a benchmark for throwIfAborted() on plain, timeout, composite and mixed signals. Assisted-by: claude:opus-5.5 Signed-off-by: Robert Nagy <ronagy@icloud.com> PR-URL: https://github.com/nodejs/node/pull/67543 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
4f37a6a8aa
commit
029acbfbd7
2 files changed
+46
-23
No files matched your search
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
const common = require('../common.js');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
n: [5e7],
|
||||
kind: ['controller', 'timeout', 'any', 'mixed'],
|
||||
});
|
||||
|
||||
const kSignals = 16;
|
||||
|
||||
function createSignal(kind, i, controllers) {
|
||||
switch (kind) {
|
||||
case 'controller': {
|
||||
const controller = new AbortController();
|
||||
controllers.push(controller);
|
||||
return controller.signal;
|
||||
}
|
||||
case 'timeout':
|
||||
return AbortSignal.timeout(1e9);
|
||||
case 'any': {
|
||||
const controller = new AbortController();
|
||||
controllers.push(controller);
|
||||
return AbortSignal.any([controller.signal, AbortSignal.timeout(1e9)]);
|
||||
}
|
||||
case 'mixed':
|
||||
return createSignal(['controller', 'timeout', 'any'][i % 3], i, controllers);
|
||||
default:
|
||||
throw new Error('Invalid kind');
|
||||
}
|
||||
}
|
||||
|
||||
function main({ n, kind }) {
|
||||
// Keep the controllers alive so that composite signals keep their sources.
|
||||
const controllers = [];
|
||||
const signals = [];
|
||||
for (let i = 0; i < kSignals; ++i)
|
||||
signals.push(createSignal(kind, i, controllers));
|
||||
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
signals[i % kSignals].throwIfAborted();
|
||||
bench.end(n);
|
||||
}
|
||||
@@ -163,25 +163,6 @@ function validateThisAbortSignal(obj) {
|
||||
throw new ERR_INVALID_THIS('AbortSignal');
|
||||
}
|
||||
|
||||
function refreshCompositeSignal(signal) {
|
||||
if (!signal[kComposite] || signal[kAborted] || !signal[kSourceSignals]?.size) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const sourceSignalWeakRef of signal[kSourceSignals]) {
|
||||
const sourceSignal = sourceSignalWeakRef.deref();
|
||||
if (sourceSignal === undefined) {
|
||||
signal[kSourceSignals].delete(sourceSignalWeakRef);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sourceSignal[kAborted]) {
|
||||
abortSignal(signal, sourceSignal[kReason]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function followCompositeSignal(signal) {
|
||||
if (signal[kFollowing] || signal[kAborted] || !signal[kSourceSignals]?.size) {
|
||||
return;
|
||||
@@ -320,7 +301,6 @@ class AbortSignal extends EventTarget {
|
||||
*/
|
||||
get aborted() {
|
||||
validateThisAbortSignal(this);
|
||||
refreshCompositeSignal(this);
|
||||
return !!this[kAborted];
|
||||
}
|
||||
|
||||
@@ -329,13 +309,11 @@ class AbortSignal extends EventTarget {
|
||||
*/
|
||||
get reason() {
|
||||
validateThisAbortSignal(this);
|
||||
refreshCompositeSignal(this);
|
||||
return this[kReason];
|
||||
}
|
||||
|
||||
throwIfAborted() {
|
||||
validateThisAbortSignal(this);
|
||||
refreshCompositeSignal(this);
|
||||
if (this[kAborted]) {
|
||||
throw this[kReason];
|
||||
}
|
||||
@@ -401,7 +379,6 @@ class AbortSignal extends EventTarget {
|
||||
gcPersistentSignals.add(signal);
|
||||
}
|
||||
|
||||
refreshCompositeSignal(signal);
|
||||
if (signal[kAborted]) {
|
||||
abortSignal(resultSignal, signal[kReason]);
|
||||
return resultSignal;
|
||||
@@ -438,6 +415,9 @@ class AbortSignal extends EventTarget {
|
||||
|
||||
// Preserve abort state even if sources are collected before this signal is
|
||||
// observed. Following uses weak references so unused signals can be collected.
|
||||
// Since aborting a source synchronously aborts the composites that follow it,
|
||||
// a composite's own abort state is always current and reading it doesn't
|
||||
// need to check its sources.
|
||||
followCompositeSignal(resultSignal);
|
||||
|
||||
return resultSignal;
|
||||
|
||||
Reference in new issue
Block a user