mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
async_hooks: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36168 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
514f464a60
commit
f47d65538a
3 changed files with 20 additions and 12 deletions
|
@ -1,6 +1,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
ArrayPrototypeIncludes,
|
||||||
|
ArrayPrototypeIndexOf,
|
||||||
|
ArrayPrototypePush,
|
||||||
|
ArrayPrototypeSplice,
|
||||||
|
FunctionPrototypeBind,
|
||||||
NumberIsSafeInteger,
|
NumberIsSafeInteger,
|
||||||
ObjectDefineProperties,
|
ObjectDefineProperties,
|
||||||
ObjectIs,
|
ObjectIs,
|
||||||
|
@ -85,7 +90,7 @@ class AsyncHook {
|
||||||
const [hooks_array, hook_fields] = getHookArrays();
|
const [hooks_array, hook_fields] = getHookArrays();
|
||||||
|
|
||||||
// Each hook is only allowed to be added once.
|
// Each hook is only allowed to be added once.
|
||||||
if (hooks_array.includes(this))
|
if (ArrayPrototypeIncludes(hooks_array, this))
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
const prev_kTotals = hook_fields[kTotals];
|
const prev_kTotals = hook_fields[kTotals];
|
||||||
|
@ -99,7 +104,7 @@ class AsyncHook {
|
||||||
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
||||||
hook_fields[kTotals] +=
|
hook_fields[kTotals] +=
|
||||||
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
||||||
hooks_array.push(this);
|
ArrayPrototypePush(hooks_array, this);
|
||||||
|
|
||||||
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
||||||
enableHooks();
|
enableHooks();
|
||||||
|
@ -113,7 +118,7 @@ class AsyncHook {
|
||||||
disable() {
|
disable() {
|
||||||
const [hooks_array, hook_fields] = getHookArrays();
|
const [hooks_array, hook_fields] = getHookArrays();
|
||||||
|
|
||||||
const index = hooks_array.indexOf(this);
|
const index = ArrayPrototypeIndexOf(hooks_array, this);
|
||||||
if (index === -1)
|
if (index === -1)
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
|
@ -125,7 +130,7 @@ class AsyncHook {
|
||||||
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
||||||
hook_fields[kTotals] +=
|
hook_fields[kTotals] +=
|
||||||
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
||||||
hooks_array.splice(index, 1);
|
ArrayPrototypeSplice(hooks_array, index, 1);
|
||||||
|
|
||||||
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
||||||
disableHooks();
|
disableHooks();
|
||||||
|
@ -218,7 +223,7 @@ class AsyncResource {
|
||||||
bind(fn) {
|
bind(fn) {
|
||||||
if (typeof fn !== 'function')
|
if (typeof fn !== 'function')
|
||||||
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
||||||
const ret = this.runInAsyncScope.bind(this, fn);
|
const ret = FunctionPrototypeBind(this.runInAsyncScope, this, fn);
|
||||||
ObjectDefineProperties(ret, {
|
ObjectDefineProperties(ret, {
|
||||||
'length': {
|
'length': {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
|
@ -264,7 +269,8 @@ class AsyncLocalStorage {
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
this.enabled = false;
|
this.enabled = false;
|
||||||
// If this.enabled, the instance must be in storageList
|
// If this.enabled, the instance must be in storageList
|
||||||
storageList.splice(storageList.indexOf(this), 1);
|
ArrayPrototypeSplice(storageList,
|
||||||
|
ArrayPrototypeIndexOf(storageList, this), 1);
|
||||||
if (storageList.length === 0) {
|
if (storageList.length === 0) {
|
||||||
storageHook.disable();
|
storageHook.disable();
|
||||||
}
|
}
|
||||||
|
@ -274,7 +280,7 @@ class AsyncLocalStorage {
|
||||||
_enable() {
|
_enable() {
|
||||||
if (!this.enabled) {
|
if (!this.enabled) {
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
storageList.push(this);
|
ArrayPrototypePush(storageList, this);
|
||||||
storageHook.enable();
|
storageHook.enable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
ArrayPrototypePop,
|
||||||
|
ArrayPrototypeSlice,
|
||||||
ArrayPrototypeUnshift,
|
ArrayPrototypeUnshift,
|
||||||
ErrorCaptureStackTrace,
|
ErrorCaptureStackTrace,
|
||||||
FunctionPrototypeBind,
|
FunctionPrototypeBind,
|
||||||
|
@ -132,7 +134,7 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
|
||||||
if (asyncId !== 0 && hasHooks(kAfter))
|
if (asyncId !== 0 && hasHooks(kAfter))
|
||||||
emitAfterNative(asyncId);
|
emitAfterNative(asyncId);
|
||||||
|
|
||||||
execution_async_resources.pop();
|
ArrayPrototypePop(execution_async_resources);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +272,7 @@ function getHookArrays() {
|
||||||
|
|
||||||
|
|
||||||
function storeActiveHooks() {
|
function storeActiveHooks() {
|
||||||
active_hooks.tmp_array = active_hooks.array.slice();
|
active_hooks.tmp_array = ArrayPrototypeSlice(active_hooks.array);
|
||||||
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
|
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
|
||||||
// 4. So do this the long way.
|
// 4. So do this the long way.
|
||||||
active_hooks.tmp_fields = [];
|
active_hooks.tmp_fields = [];
|
||||||
|
@ -522,7 +524,7 @@ function popAsyncContext(asyncId) {
|
||||||
const offset = stackLength - 1;
|
const offset = stackLength - 1;
|
||||||
async_id_fields[kExecutionAsyncId] = async_wrap.async_ids_stack[2 * offset];
|
async_id_fields[kExecutionAsyncId] = async_wrap.async_ids_stack[2 * offset];
|
||||||
async_id_fields[kTriggerAsyncId] = async_wrap.async_ids_stack[2 * offset + 1];
|
async_id_fields[kTriggerAsyncId] = async_wrap.async_ids_stack[2 * offset + 1];
|
||||||
execution_async_resources.pop();
|
ArrayPrototypePop(execution_async_resources);
|
||||||
async_hook_fields[kStackLength] = offset;
|
async_hook_fields[kStackLength] = offset;
|
||||||
return offset > 0;
|
return offset > 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ let hook;
|
||||||
let config;
|
let config;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
Set,
|
SafeSet,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
function lazyHookCreation() {
|
function lazyHookCreation() {
|
||||||
|
@ -44,7 +44,7 @@ function lazyHookCreation() {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
hook.promiseIds = new Set();
|
hook.promiseIds = new SafeSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
function enable() {
|
function enable() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue