mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
async_hooks: improve AsyncResource performance
Accessing symbols is generally quite expensive and so is emitInit, only do both when actually required. PR-URL: https://github.com/nodejs/node/pull/27032 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
af03de48d8
commit
d3d4e10107
@ -27,6 +27,7 @@ const {
|
||||
emitBefore,
|
||||
emitAfter,
|
||||
emitDestroy,
|
||||
initHooksExist,
|
||||
} = internal_async_hooks;
|
||||
|
||||
// Get symbols
|
||||
@ -146,29 +147,31 @@ class AsyncResource {
|
||||
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
|
||||
}
|
||||
|
||||
this[async_id_symbol] = newAsyncId();
|
||||
const asyncId = newAsyncId();
|
||||
this[async_id_symbol] = asyncId;
|
||||
this[trigger_async_id_symbol] = triggerAsyncId;
|
||||
// This prop name (destroyed) has to be synchronized with C++
|
||||
this[destroyedSymbol] = { destroyed: false };
|
||||
|
||||
emitInit(
|
||||
this[async_id_symbol], type, this[trigger_async_id_symbol], this
|
||||
);
|
||||
// This prop name (destroyed) has to be synchronized with C++
|
||||
const destroyed = { destroyed: false };
|
||||
this[destroyedSymbol] = destroyed;
|
||||
|
||||
if (initHooksExist()) {
|
||||
emitInit(asyncId, type, triggerAsyncId, this);
|
||||
}
|
||||
|
||||
if (!opts.requireManualDestroy) {
|
||||
registerDestroyHook(this, this[async_id_symbol], this[destroyedSymbol]);
|
||||
registerDestroyHook(this, asyncId, destroyed);
|
||||
}
|
||||
}
|
||||
|
||||
runInAsyncScope(fn, thisArg, ...args) {
|
||||
emitBefore(this[async_id_symbol], this[trigger_async_id_symbol]);
|
||||
let ret;
|
||||
const asyncId = this[async_id_symbol];
|
||||
emitBefore(asyncId, this[trigger_async_id_symbol]);
|
||||
try {
|
||||
ret = Reflect.apply(fn, thisArg, args);
|
||||
return Reflect.apply(fn, thisArg, args);
|
||||
} finally {
|
||||
emitAfter(this[async_id_symbol]);
|
||||
emitAfter(asyncId);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
emitDestroy() {
|
||||
|
@ -286,14 +286,11 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
|
||||
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
|
||||
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
|
||||
|
||||
let ret;
|
||||
try {
|
||||
ret = Reflect.apply(block, null, args);
|
||||
return Reflect.apply(block, null, args);
|
||||
} finally {
|
||||
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user