Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions benchmark/abort_controller/abort-signal-throw-if-aborted.js
Original file line number Diff line number Diff line change
@@ -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);
}
26 changes: 3 additions & 23 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -320,7 +301,6 @@ class AbortSignal extends EventTarget {
*/
get aborted() {
validateThisAbortSignal(this);
refreshCompositeSignal(this);
return !!this[kAborted];
}

Expand All @@ -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];
}
Expand Down Expand Up @@ -401,7 +379,6 @@ class AbortSignal extends EventTarget {
gcPersistentSignals.add(signal);
}

refreshCompositeSignal(signal);
if (signal[kAborted]) {
abortSignal(resultSignal, signal[kReason]);
return resultSignal;
Expand Down Expand Up @@ -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;
Expand Down
Loading