lib: optimize async context frame activation

Signed-off-by: Tim Perry <pimterry@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65519
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Tim Perry authored and Antoine du Hamel committed 2026-09-19 12:07:52 +02:00
1 parent d2e0f2d3bd
commit c4b390cf17
1 file changed
+30 -36
+30 -36
View File
@@ -1,7 +1,6 @@
'use strict';
const {
ObjectSetPrototypeOf,
SafeMap,
} = primordials;
@@ -10,45 +9,22 @@ const {
setContinuationPreservedEmbedderData,
} = internalBinding('async_context_frame');
let enabled_;
class ActiveAsyncContextFrame extends SafeMap {
static get enabled() {
return true;
}
static current() {
return getContinuationPreservedEmbedderData();
}
static set(frame) {
setContinuationPreservedEmbedderData(frame);
}
static exchange(frame) {
const prior = this.current();
this.set(frame);
return prior;
}
static disable(store) {
const frame = this.current();
frame?.disable(store);
}
function activeCurrent() {
return getContinuationPreservedEmbedderData();
}
function checkEnabled() {
const enabled = require('internal/options')
.getOptionValue('--async-context-frame');
function activeSet(frame) {
setContinuationPreservedEmbedderData(frame);
}
// If enabled, swap to active prototype so we don't need to check status
// on every interaction with the async context frame.
if (enabled) {
// eslint-disable-next-line no-use-before-define
ObjectSetPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame);
}
function activeExchange(frame) {
const prior = getContinuationPreservedEmbedderData();
setContinuationPreservedEmbedderData(frame);
return prior;
}
return enabled;
function activeDisable(store) {
getContinuationPreservedEmbedderData()?.disable(store);
}
class InactiveAsyncContextFrame extends SafeMap {
@@ -74,4 +50,22 @@ class AsyncContextFrame extends InactiveAsyncContextFrame {
}
}
let enabled_;
function checkEnabled() {
const enabled = require('internal/options')
.getOptionValue('--async-context-frame');
// If enabled, install the active implementations directly. We use props
// rather than a prototype replacement to preserve V8 optimizations.
if (enabled) {
AsyncContextFrame.current = activeCurrent;
AsyncContextFrame.set = activeSet;
AsyncContextFrame.exchange = activeExchange;
AsyncContextFrame.disable = activeDisable;
}
return enabled;
}
module.exports = AsyncContextFrame;