Skip to content

Commit b75ca9d

Browse files
committed
lib: speed up AbortSignal
Speed up creating and aborting signals, adding and removing abort listeners, AbortSignal.any() and AbortSignal.timeout(), and stop the default abort reason from retaining the aborting caller. - Only allocate and dispatch the abort event when the signal has abort listeners, and only allocate the dependent signal list when there are dependent signals. - Track whether an event is trusted with a private field instead of a WeakSet. - Flatten addEventListener() options into locals instead of allocating a normalized options object, only validate the signal option when one is passed, and use the listener flags directly. - Call the EventTarget kNewListener and kRemoveListener implementations directly. V8 does not optimize keyed super property access such as super[kNewListener](). - Initialize the internal slots that AbortSignal.any() adds to its sources up front, and the composite-only slots for composites, so live signals don't change shape. - Give every signal a single WeakRef that is shared by all the composites that follow it, and register each composite and each source once with their cleanup registries instead of registering every (source, composite) pair with an unregister token. Registering with an unregister token is several times more expensive. - Register AbortSignal.timeout() timers through a holder that is emptied when the timer fires, instead of using the signal as an unregister token. - Take positional arguments in AbortSignal's internal constructor instead of destructuring an options object that defaults to kEmptyObject. kEmptyObject is a null-prototype object, which V8 keeps in dictionary mode, so each of those reads was an uncached dictionary miss that cost more than the rest of the constructor. - Create the default abort reason with Error.stackTraceLimit set to 0. Until a captured stack is formatted, V8 keeps the call sites, which retain the receivers and functions of the aborting caller's frames, so a retained signal kept the aborted work alive. Formatting the stack eagerly would fix that but costs several microseconds per abort(). The default AbortError no longer shows where abort() was called; callers that want that can pass their own reason. - Add benchmarks for aborting, adding and removing abort listeners, and AbortSignal.any(). Refs: nodejs#66192 Refs: nodejs#66265 Assisted-by: claude:opus-5.5 Signed-off-by: Robert Nagy <ronagy@icloud.com>
1 parent 2745698 commit b75ca9d

12 files changed

Lines changed: 439 additions & 199 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
n: [1e6],
6+
listeners: [0, 1],
7+
});
8+
9+
function main({ n, listeners }) {
10+
const reason = new Error('reason');
11+
const noop = () => {};
12+
13+
bench.start();
14+
for (let i = 0; i < n; i++) {
15+
const ac = new AbortController();
16+
if (listeners > 0)
17+
ac.signal.addEventListener('abort', noop);
18+
ac.abort(reason);
19+
}
20+
bench.end(n);
21+
}
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+
const { addAbortListener } = require('events');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1e6],
7+
kind: ['addEventListener', 'addEventListener-once', 'addAbortListener'],
8+
});
9+
10+
function main({ n, kind }) {
11+
const { signal } = new AbortController();
12+
const noop = () => {};
13+
14+
switch (kind) {
15+
case 'addEventListener':
16+
bench.start();
17+
for (let i = 0; i < n; i++) {
18+
signal.addEventListener('abort', noop);
19+
signal.removeEventListener('abort', noop);
20+
}
21+
bench.end(n);
22+
break;
23+
case 'addEventListener-once': {
24+
const options = { once: true };
25+
bench.start();
26+
for (let i = 0; i < n; i++) {
27+
signal.addEventListener('abort', noop, options);
28+
signal.removeEventListener('abort', noop);
29+
}
30+
bench.end(n);
31+
break;
32+
}
33+
case 'addAbortListener':
34+
bench.start();
35+
for (let i = 0; i < n; i++) {
36+
addAbortListener(signal, noop)[Symbol.dispose]();
37+
}
38+
bench.end(n);
39+
break;
40+
default:
41+
throw new Error('Invalid kind');
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
n: [2e5],
6+
kind: ['create', 'listen', 'abort'],
7+
});
8+
9+
// Combines a per-iteration signal with a long-lived one, like a request
10+
// signal combined with a server wide one. The work is split into batches
11+
// that run in separate macrotasks because WeakRef targets are kept alive
12+
// until the end of the current job, as they would be between requests.
13+
function main({ n, kind }) {
14+
const reason = new Error('reason');
15+
const noop = () => {};
16+
const longLived = new AbortController().signal;
17+
const batch = 1000;
18+
19+
function run(count) {
20+
for (let i = 0; i < count; i++) {
21+
const ac = new AbortController();
22+
const signal = AbortSignal.any([ac.signal, longLived]);
23+
if (kind === 'create')
24+
continue;
25+
signal.addEventListener('abort', noop, { once: true });
26+
if (kind === 'listen')
27+
signal.removeEventListener('abort', noop);
28+
else
29+
ac.abort(reason);
30+
}
31+
}
32+
33+
let left = n;
34+
function step() {
35+
const count = Math.min(batch, left);
36+
run(count);
37+
left -= count;
38+
if (left > 0)
39+
setImmediate(step);
40+
else
41+
bench.end(n);
42+
}
43+
44+
bench.start();
45+
step();
46+
}

0 commit comments

Comments
 (0)