From 09420b42897cddb9609d561402e1e075aee058bf Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 17 Nov 2026 14:05:36 +0000 Subject: [PATCH] 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 --- .../abort-signal-throw-if-aborted.js | 43 +++++++++++++++++++ lib/internal/abort_controller.js | 26 ++--------- 2 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 benchmark/abort_controller/abort-signal-throw-if-aborted.js diff --git a/benchmark/abort_controller/abort-signal-throw-if-aborted.js b/benchmark/abort_controller/abort-signal-throw-if-aborted.js new file mode 100644 index 000000000000..bf14d40b9c60 --- /dev/null +++ b/benchmark/abort_controller/abort-signal-throw-if-aborted.js @@ -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); +} diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 9f524d5cb5e2..3b0f70a58b36 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -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;