Skip to content

Commit 029acbf

Browse files
authored
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: #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>
1 parent 4f37a6a commit 029acbf

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
n: [5e7],
6+
kind: ['controller', 'timeout', 'any', 'mixed'],
7+
});
8+
9+
const kSignals = 16;
10+
11+
function createSignal(kind, i, controllers) {
12+
switch (kind) {
13+
case 'controller': {
14+
const controller = new AbortController();
15+
controllers.push(controller);
16+
return controller.signal;
17+
}
18+
case 'timeout':
19+
return AbortSignal.timeout(1e9);
20+
case 'any': {
21+
const controller = new AbortController();
22+
controllers.push(controller);
23+
return AbortSignal.any([controller.signal, AbortSignal.timeout(1e9)]);
24+
}
25+
case 'mixed':
26+
return createSignal(['controller', 'timeout', 'any'][i % 3], i, controllers);
27+
default:
28+
throw new Error('Invalid kind');
29+
}
30+
}
31+
32+
function main({ n, kind }) {
33+
// Keep the controllers alive so that composite signals keep their sources.
34+
const controllers = [];
35+
const signals = [];
36+
for (let i = 0; i < kSignals; ++i)
37+
signals.push(createSignal(kind, i, controllers));
38+
39+
bench.start();
40+
for (let i = 0; i < n; ++i)
41+
signals[i % kSignals].throwIfAborted();
42+
bench.end(n);
43+
}

‎lib/internal/abort_controller.js‎

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,6 @@ function validateThisAbortSignal(obj) {
163163
throw new ERR_INVALID_THIS('AbortSignal');
164164
}
165165

166-
function refreshCompositeSignal(signal) {
167-
if (!signal[kComposite] || signal[kAborted] || !signal[kSourceSignals]?.size) {
168-
return;
169-
}
170-
171-
for (const sourceSignalWeakRef of signal[kSourceSignals]) {
172-
const sourceSignal = sourceSignalWeakRef.deref();
173-
if (sourceSignal === undefined) {
174-
signal[kSourceSignals].delete(sourceSignalWeakRef);
175-
continue;
176-
}
177-
178-
if (sourceSignal[kAborted]) {
179-
abortSignal(signal, sourceSignal[kReason]);
180-
return;
181-
}
182-
}
183-
}
184-
185166
function followCompositeSignal(signal) {
186167
if (signal[kFollowing] || signal[kAborted] || !signal[kSourceSignals]?.size) {
187168
return;
@@ -320,7 +301,6 @@ class AbortSignal extends EventTarget {
320301
*/
321302
get aborted() {
322303
validateThisAbortSignal(this);
323-
refreshCompositeSignal(this);
324304
return !!this[kAborted];
325305
}
326306

@@ -329,13 +309,11 @@ class AbortSignal extends EventTarget {
329309
*/
330310
get reason() {
331311
validateThisAbortSignal(this);
332-
refreshCompositeSignal(this);
333312
return this[kReason];
334313
}
335314

336315
throwIfAborted() {
337316
validateThisAbortSignal(this);
338-
refreshCompositeSignal(this);
339317
if (this[kAborted]) {
340318
throw this[kReason];
341319
}
@@ -401,7 +379,6 @@ class AbortSignal extends EventTarget {
401379
gcPersistentSignals.add(signal);
402380
}
403381

404-
refreshCompositeSignal(signal);
405382
if (signal[kAborted]) {
406383
abortSignal(resultSignal, signal[kReason]);
407384
return resultSignal;
@@ -438,6 +415,9 @@ class AbortSignal extends EventTarget {
438415

439416
// Preserve abort state even if sources are collected before this signal is
440417
// observed. Following uses weak references so unused signals can be collected.
418+
// Since aborting a source synchronously aborts the composites that follow it,
419+
// a composite's own abort state is always current and reading it doesn't
420+
// need to check its sources.
441421
followCompositeSignal(resultSignal);
442422

443423
return resultSignal;

0 commit comments

Comments
 (0)