Skip to content

Commit 0bcb9d8

Browse files
committed
process: skip AsyncResource in queueMicrotask when only ALS is active
Signed-off-by: Ilyas Shabi <ilyas.shabi@datadoghq.com>
1 parent 0cc50e6 commit 0bcb9d8

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎benchmark/process/queue-microtask-breadth.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
const common = require('../common.js');
44
const bench = common.createBenchmark(main, {
55
n: [4e5],
6+
als: [0, 1],
67
});
78

8-
function main({ n }) {
9+
function main({ n, als }) {
10+
if (als) {
11+
const { AsyncLocalStorage } = require('async_hooks');
12+
// Run the benchmark inside an AsyncLocalStorage context
13+
new AsyncLocalStorage().run({}, () => queue(n));
14+
} else {
15+
queue(n);
16+
}
17+
}
18+
19+
function queue(n) {
920
let j = 0;
1021

1122
function cb() {

‎benchmark/process/queue-microtask-depth.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
const common = require('../common.js');
33
const bench = common.createBenchmark(main, {
44
n: [12e5],
5+
als: [0, 1],
56
});
67

7-
function main({ n }) {
8+
function main({ n, als }) {
9+
if (als) {
10+
const { AsyncLocalStorage } = require('async_hooks');
11+
// Run the benchmark inside an AsyncLocalStorage context.
12+
new AsyncLocalStorage().run({}, () => start(n));
13+
} else {
14+
start(n);
15+
}
16+
}
17+
18+
function start(n) {
819
let counter = n;
920
bench.start();
1021
queueMicrotask(onNextTick);

‎lib/internal/process/task_queues.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,15 @@ const defaultMicrotaskResourceOpts = { requireManualDestroy: true };
167167
function queueMicrotask(callback) {
168168
validateFunction(callback, 'callback');
169169

170-
const contextFrame = AsyncContextFrame.current();
171-
if (contextFrame || enabledHooksExist()) {
170+
if (enabledHooksExist()) {
172171
const asyncResource = new AsyncResource(
173172
'Microtask',
174173
defaultMicrotaskResourceOpts,
175174
);
176175
asyncResource.callback = callback;
177176
enqueueMicrotask(FunctionPrototypeBind(runMicrotask, asyncResource));
178177
} else {
179-
// Fast path: no AsyncLocalStorage in use
178+
// Fast path: V8's CallableTask preserves ContinuationPreservedEmbedderData.
180179
enqueueMicrotask(callback);
181180
}
182181
}

0 commit comments

Comments
 (0)