mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
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: https://github.com/nodejs/node/issues/66192 Refs: https://github.com/nodejs/node/pull/66265 Assisted-by: claude:opus-5.5 Signed-off-by: Robert Nagy <ronagy@icloud.com> PR-URL: https://github.com/nodejs/node/pull/66789 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
// Flags: --expose-gc
|
|
|
|
import '../common/index.mjs';
|
|
import { gcUntil } from '../common/gc.js';
|
|
import assert from 'node:assert/strict';
|
|
import { it } from 'node:test';
|
|
|
|
// The default abort reason must not keep the objects on the caller's stack
|
|
// alive. It captures no stack at all. Refs: https://github.com/nodejs/node/issues/66192
|
|
|
|
class Job {
|
|
controller = new AbortController();
|
|
|
|
cancel() {
|
|
this.controller.abort();
|
|
return this.controller.signal;
|
|
}
|
|
|
|
cancelStatic() {
|
|
return AbortSignal.abort();
|
|
}
|
|
}
|
|
|
|
for (const method of ['cancel', 'cancelStatic']) {
|
|
it(`does not retain the caller through the default reason (${method})`, async () => {
|
|
let job = new Job();
|
|
const jobRef = new WeakRef(job);
|
|
const signal = job[method]();
|
|
job = null;
|
|
|
|
await gcUntil('job is collected', () => jobRef.deref() === undefined);
|
|
assert.strictEqual(signal.aborted, true);
|
|
assert.ok(signal.reason instanceof DOMException);
|
|
assert.strictEqual(signal.reason.name, 'AbortError');
|
|
assert.strictEqual(signal.reason.message, 'This operation was aborted');
|
|
assert.doesNotMatch(signal.reason.stack, /\n\s+at /);
|
|
});
|
|
}
|
|
|
|
it('creates a new default reason for every abort', () => {
|
|
assert.notStrictEqual(new Job().cancel().reason, new Job().cancel().reason);
|
|
assert.notStrictEqual(AbortSignal.abort().reason, AbortSignal.abort().reason);
|
|
});
|
|
|
|
it('restores Error.stackTraceLimit', () => {
|
|
const { stackTraceLimit } = Error;
|
|
Error.stackTraceLimit = 7;
|
|
try {
|
|
new Job().cancel();
|
|
new Job().cancelStatic();
|
|
assert.strictEqual(Error.stackTraceLimit, 7);
|
|
} finally {
|
|
Error.stackTraceLimit = stackTraceLimit;
|
|
}
|
|
});
|
|
|
|
it('does not throw when Error.prepareStackTrace throws', () => {
|
|
const { prepareStackTrace } = Error;
|
|
Error.prepareStackTrace = () => { throw new Error('boom'); };
|
|
try {
|
|
assert.strictEqual(new Job().cancel().reason.name, 'AbortError');
|
|
assert.strictEqual(new Job().cancelStatic().reason.name, 'AbortError');
|
|
} finally {
|
|
Error.prepareStackTrace = prepareStackTrace;
|
|
}
|
|
});
|