Skip to content

Commit 99d75ca

Browse files
committed
async_hooks: avoid extra frame copies in AsyncLocalStorage.run()
With the AsyncContextFrame implementation, every AsyncLocalStorage.run() built two copies of the current frame: one to enter the new value and another one in the finally block to put the previous value back. Both copies went through the Map iterator protocol, which calls next() and allocates an entry array per store. Copy frames with forEach() instead, and when fn() neither replaced the current frame (enterWith()) nor removed a store from it (disable()), put back the frame that was current before run() instead of building a new one. Otherwise keep building a new frame as before, so that those changes stay visible after run() returns. getStore() now does a single lookup unless the stored value is undefined. A side effect is that after a top-level run() returns the current frame is undefined again, as it was before run(), instead of a frame that holds the previous value of the store. getStore() returns the same values either way. Measured locally: als.run(i, noop) 100 ns -> 16 ns als.run(i, noop), two other stores set 145 ns -> 35 ns await als.run(i, async fn with 2 awaits) 250 ns -> 89 ns async_hooks/async-local-storage-run.js 7.0M -> 20M ops/s Assisted-by: claude:opus-5.5 Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 2aef805 commit 99d75ca

3 files changed

Lines changed: 214 additions & 5 deletions

File tree

‎lib/internal/async_context_frame.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ const {
99
setContinuationPreservedEmbedderData,
1010
} = internalBinding('async_context_frame');
1111

12+
function copyEntry(value, key) {
13+
this.set(key, value);
14+
}
15+
1216
class AsyncContextFrame extends SafeMap {
1317
constructor(store, data) {
14-
super(AsyncContextFrame.current());
18+
super();
19+
// Avoid the iterator protocol.
20+
const prior = AsyncContextFrame.current();
21+
if (prior !== undefined) {
22+
prior.forEach(copyEntry, this);
23+
}
1524
this.set(store, data);
1625
}
1726

‎lib/internal/async_local_storage/async_context_frame.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@ class AsyncLocalStorage {
6161
if (ObjectIs(prior, data)) {
6262
return ReflectApply(fn, null, args);
6363
}
64-
this.enterWith(data);
64+
const priorFrame = AsyncContextFrame.current();
65+
const frame = new AsyncContextFrame(this, data);
66+
const size = frame.size;
67+
AsyncContextFrame.set(frame);
6568
try {
6669
return ReflectApply(fn, null, args);
6770
} finally {
68-
this.enterWith(prior);
71+
// Frames are only ever deleted from, so the same frame with the same
72+
// size is untouched. Otherwise keep what fn() changed.
73+
if (AsyncContextFrame.current() === frame && frame.size === size) {
74+
AsyncContextFrame.set(priorFrame);
75+
} else {
76+
this.enterWith(prior);
77+
}
6978
}
7079
}
7180

@@ -75,10 +84,14 @@ class AsyncLocalStorage {
7584

7685
getStore() {
7786
const frame = AsyncContextFrame.current();
78-
if (!frame?.has(this)) {
87+
if (frame === undefined) {
88+
return this.#defaultValue;
89+
}
90+
const value = frame.get(this);
91+
if (value === undefined && !frame.has(this)) {
7992
return this.#defaultValue;
8093
}
81-
return frame?.get(this);
94+
return value;
8295
}
8396

8497
withScope(store) {
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { AsyncLocalStorage } = require('async_hooks');
5+
6+
// Verify which store values are visible after asyncLocalStorage.run() returns.
7+
8+
{
9+
const als1 = new AsyncLocalStorage();
10+
const als2 = new AsyncLocalStorage();
11+
12+
als1.run('outer', common.mustCall(() => {
13+
als1.run('inner', common.mustCall(() => {
14+
als2.enterWith('entered');
15+
als1.enterWith('replaced');
16+
assert.strictEqual(als1.getStore(), 'replaced');
17+
assert.strictEqual(als2.getStore(), 'entered');
18+
}));
19+
assert.strictEqual(als1.getStore(), 'outer');
20+
assert.strictEqual(als2.getStore(), 'entered');
21+
}));
22+
assert.strictEqual(als1.getStore(), undefined);
23+
assert.strictEqual(als2.getStore(), 'entered');
24+
als2.disable();
25+
}
26+
27+
{
28+
// enterWith() inside a run() with an unchanged value.
29+
const als1 = new AsyncLocalStorage();
30+
const als2 = new AsyncLocalStorage();
31+
32+
als1.run(NaN, common.mustCall(() => {
33+
als1.run(NaN, common.mustCall(() => {
34+
als2.enterWith('entered');
35+
}));
36+
assert.strictEqual(als2.getStore(), 'entered');
37+
}));
38+
assert.strictEqual(als1.getStore(), undefined);
39+
assert.strictEqual(als2.getStore(), 'entered');
40+
als2.disable();
41+
}
42+
43+
{
44+
const als1 = new AsyncLocalStorage();
45+
const als2 = new AsyncLocalStorage();
46+
const als3 = new AsyncLocalStorage();
47+
48+
als2.enterWith('store2');
49+
als1.run('store1', common.mustCall(() => {
50+
als3.disable();
51+
als2.disable();
52+
assert.strictEqual(als1.getStore(), 'store1');
53+
assert.strictEqual(als2.getStore(), undefined);
54+
}));
55+
assert.strictEqual(als1.getStore(), undefined);
56+
assert.strictEqual(als2.getStore(), undefined);
57+
assert.strictEqual(als3.getStore(), undefined);
58+
59+
als1.run('outer', common.mustCall(() => {
60+
als1.run('inner', common.mustCall(() => {
61+
als1.disable();
62+
assert.strictEqual(als1.getStore(), undefined);
63+
}));
64+
assert.strictEqual(als1.getStore(), 'outer');
65+
}));
66+
assert.strictEqual(als1.getStore(), undefined);
67+
68+
als2.enterWith('store2');
69+
als1.run('store1', common.mustCall(() => {
70+
als3.disable();
71+
}));
72+
assert.strictEqual(als1.getStore(), undefined);
73+
assert.strictEqual(als2.getStore(), 'store2');
74+
als2.disable();
75+
}
76+
77+
{
78+
// A store holding undefined does not fall back to defaultValue.
79+
const als = new AsyncLocalStorage({ defaultValue: 'default' });
80+
const other = new AsyncLocalStorage();
81+
82+
assert.strictEqual(als.getStore(), 'default');
83+
als.run(undefined, common.mustCall(() => {
84+
assert.strictEqual(als.getStore(), undefined);
85+
}));
86+
assert.strictEqual(als.getStore(), 'default');
87+
88+
als.run('value', common.mustCall(() => {
89+
als.exit(common.mustCall(() => {
90+
assert.strictEqual(als.getStore(), undefined);
91+
}));
92+
als.run(undefined, common.mustCall(() => {
93+
assert.strictEqual(als.getStore(), undefined);
94+
}));
95+
assert.strictEqual(als.getStore(), 'value');
96+
}));
97+
assert.strictEqual(als.getStore(), 'default');
98+
99+
other.run('other', common.mustCall(() => {
100+
assert.strictEqual(als.getStore(), 'default');
101+
als.run(undefined, common.mustCall(() => {
102+
assert.strictEqual(als.getStore(), undefined);
103+
assert.strictEqual(other.getStore(), 'other');
104+
}));
105+
assert.strictEqual(als.getStore(), 'default');
106+
}));
107+
assert.strictEqual(als.getStore(), 'default');
108+
assert.strictEqual(other.getStore(), undefined);
109+
110+
als.run('value', common.mustCall(() => {
111+
als.disable();
112+
assert.strictEqual(als.getStore(), 'default');
113+
}));
114+
assert.strictEqual(als.getStore(), 'default');
115+
}
116+
117+
{
118+
const als1 = new AsyncLocalStorage();
119+
const als2 = new AsyncLocalStorage();
120+
121+
als1.run('outer', common.mustCall(() => {
122+
als2.run('store2', common.mustCall(() => {
123+
assert.throws(() => {
124+
als1.run('inner', common.mustCall(() => {
125+
assert.strictEqual(als1.getStore(), 'inner');
126+
throw new Error('boom');
127+
}));
128+
}, /boom/);
129+
assert.strictEqual(als1.getStore(), 'outer');
130+
assert.strictEqual(als2.getStore(), 'store2');
131+
}));
132+
}));
133+
134+
assert.throws(() => {
135+
als1.run('value', common.mustCall(() => {
136+
throw new Error('boom');
137+
}));
138+
}, /boom/);
139+
assert.strictEqual(als1.getStore(), undefined);
140+
assert.strictEqual(als2.getStore(), undefined);
141+
}
142+
143+
{
144+
const als1 = new AsyncLocalStorage();
145+
const als2 = new AsyncLocalStorage();
146+
147+
als1.run('a1', common.mustCall(() => {
148+
als2.run('b1', common.mustCall(() => {
149+
als1.run('a2', common.mustCall(() => {
150+
assert.strictEqual(als1.getStore(), 'a2');
151+
assert.strictEqual(als2.getStore(), 'b1');
152+
setImmediate(common.mustCall(() => {
153+
assert.strictEqual(als1.getStore(), 'a2');
154+
assert.strictEqual(als2.getStore(), 'b1');
155+
}));
156+
}));
157+
assert.strictEqual(als1.getStore(), 'a1');
158+
assert.strictEqual(als2.getStore(), 'b1');
159+
als2.run('b2', common.mustCall(() => {
160+
assert.strictEqual(als1.getStore(), 'a1');
161+
assert.strictEqual(als2.getStore(), 'b2');
162+
}));
163+
assert.strictEqual(als2.getStore(), 'b1');
164+
}));
165+
assert.strictEqual(als1.getStore(), 'a1');
166+
assert.strictEqual(als2.getStore(), undefined);
167+
}));
168+
assert.strictEqual(als1.getStore(), undefined);
169+
assert.strictEqual(als2.getStore(), undefined);
170+
171+
const promise = als1.run('async', common.mustCall(async () => {
172+
await 1;
173+
assert.strictEqual(als1.getStore(), 'async');
174+
await als2.run('nested', common.mustCall(async () => {
175+
await 1;
176+
assert.strictEqual(als1.getStore(), 'async');
177+
assert.strictEqual(als2.getStore(), 'nested');
178+
}));
179+
assert.strictEqual(als2.getStore(), undefined);
180+
return als1.getStore();
181+
}));
182+
assert.strictEqual(als1.getStore(), undefined);
183+
promise.then(common.mustCall((value) => {
184+
assert.strictEqual(value, 'async');
185+
assert.strictEqual(als1.getStore(), undefined);
186+
}));
187+
}

0 commit comments

Comments
 (0)