process: optimize asyncHandledRejections by using FixedQueue

PR-URL: https://github.com/nodejs/node/pull/60854
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
Gürgün Dayıoğlu authored and Antoine du Hamel committed 2026-05-07 15:41:17 +02:00
1 parent b39508b368
commit 37d49f3219
2 files changed
+51 -8

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
'use strict';
const common = require('../common.js');
// Benchmarks the throughput of processing many promise rejections that are
// initially unhandled, get warned, and then handled asynchronously, exercising
// asyncHandledRejections + processPromiseRejections.
//
// Note: This benchmark uses --unhandled-rejections=warn to avoid crashing
// when promises are temporarily unhandled.
const bench = common.createBenchmark(main, {
n: [1e4, 5e4, 1e5],
}, {
flags: ['--unhandled-rejections=warn'],
});
function main({ n }) {
const rejections = [];
// Suppress warning output during the benchmark
process.removeAllListeners('warning');
for (let i = 0; i < n; i++) {
rejections.push(Promise.reject(i));
}
// Wait for them to be processed as unhandled and warned.
setImmediate(() => {
setImmediate(() => {
bench.start();
for (let i = 0; i < n; i++) {
rejections[i].catch(() => {});
}
// Let processPromiseRejections drain asyncHandledRejections.
setImmediate(() => {
bench.end(n);
});
});
});
}
+8 -8
View File
@@ -1,14 +1,14 @@
'use strict';
const {
ArrayPrototypePush,
ArrayPrototypeShift,
Error,
ObjectPrototypeHasOwnProperty,
SafeMap,
SafeWeakMap,
} = primordials;
const FixedQueue = require('internal/fixed_queue');
const {
tickInfo,
promiseRejectEvents: {
@@ -142,9 +142,9 @@ const maybeUnhandledPromises = new SafeWeakMap();
let pendingUnhandledRejections = new SafeMap();
/**
* @type {Array<{promise: Promise, warning: Error}>}
* @type {import('internal/fixed_queue')<{promise: Promise, warning: Error}>}
*/
const asyncHandledRejections = [];
const asyncHandledRejections = new FixedQueue();
/**
* @type {number}
@@ -281,7 +281,7 @@ function handledRejection(promise) {
if (promiseInfo.warned) {
// Generate the warning object early to get a good stack trace.
const warning = new PromiseRejectionHandledWarning(promiseInfo.uid);
ArrayPrototypePush(asyncHandledRejections, { promise, warning });
asyncHandledRejections.push({ promise, warning });
setHasRejectionToWarn(true);
}
}
@@ -437,10 +437,10 @@ function getUnhandledRejectionsMode() {
// a warning to be emitted which requires the microtask and next tick
// queues to be drained again.
function processPromiseRejections() {
let maybeScheduledTicksOrMicrotasks = asyncHandledRejections.length > 0;
let maybeScheduledTicksOrMicrotasks = !asyncHandledRejections.isEmpty();
while (asyncHandledRejections.length !== 0) {
const { promise, warning } = ArrayPrototypeShift(asyncHandledRejections);
while (!asyncHandledRejections.isEmpty()) {
const { promise, warning } = asyncHandledRejections.shift();
if (!process.emit('rejectionHandled', promise)) {
process.emitWarning(warning);
}