async_hooks: lazy loading for startup performance
PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
This commit is contained in:
parent
17d95ea1f0
commit
a3a14082fc
@ -1,49 +1,56 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { createHook } = require('async_hooks');
|
|
||||||
const inspector = process.binding('inspector');
|
const inspector = process.binding('inspector');
|
||||||
const config = process.binding('config');
|
|
||||||
|
|
||||||
if (!inspector || !inspector.asyncTaskScheduled) {
|
if (!inspector || !inspector.asyncTaskScheduled) {
|
||||||
exports.setup = function() {};
|
exports.setup = function() {};
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hook = createHook({
|
let hook;
|
||||||
init(asyncId, type, triggerAsyncId, resource) {
|
let config;
|
||||||
|
|
||||||
|
function lazyHookCreation() {
|
||||||
|
const { createHook } = require('async_hooks');
|
||||||
|
config = process.binding('config');
|
||||||
|
|
||||||
|
hook = createHook({
|
||||||
|
init(asyncId, type, triggerAsyncId, resource) {
|
||||||
// It's difficult to tell which tasks will be recurring and which won't,
|
// It's difficult to tell which tasks will be recurring and which won't,
|
||||||
// therefore we mark all tasks as recurring. Based on the discussion
|
// therefore we mark all tasks as recurring. Based on the discussion
|
||||||
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
|
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
|
||||||
// this should be fine as long as we call asyncTaskCanceled() too.
|
// this should be fine as long as we call asyncTaskCanceled() too.
|
||||||
const recurring = true;
|
const recurring = true;
|
||||||
if (type === 'PROMISE')
|
if (type === 'PROMISE')
|
||||||
this.promiseIds.add(asyncId);
|
this.promiseIds.add(asyncId);
|
||||||
else
|
else
|
||||||
inspector.asyncTaskScheduled(type, asyncId, recurring);
|
inspector.asyncTaskScheduled(type, asyncId, recurring);
|
||||||
},
|
},
|
||||||
|
|
||||||
before(asyncId) {
|
before(asyncId) {
|
||||||
if (this.promiseIds.has(asyncId))
|
if (this.promiseIds.has(asyncId))
|
||||||
return;
|
return;
|
||||||
inspector.asyncTaskStarted(asyncId);
|
inspector.asyncTaskStarted(asyncId);
|
||||||
},
|
},
|
||||||
|
|
||||||
after(asyncId) {
|
after(asyncId) {
|
||||||
if (this.promiseIds.has(asyncId))
|
if (this.promiseIds.has(asyncId))
|
||||||
return;
|
return;
|
||||||
inspector.asyncTaskFinished(asyncId);
|
inspector.asyncTaskFinished(asyncId);
|
||||||
},
|
},
|
||||||
|
|
||||||
destroy(asyncId) {
|
destroy(asyncId) {
|
||||||
if (this.promiseIds.has(asyncId))
|
if (this.promiseIds.has(asyncId))
|
||||||
return this.promiseIds.delete(asyncId);
|
return this.promiseIds.delete(asyncId);
|
||||||
inspector.asyncTaskCanceled(asyncId);
|
inspector.asyncTaskCanceled(asyncId);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
hook.promiseIds = new Set();
|
hook.promiseIds = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
function enable() {
|
function enable() {
|
||||||
|
if (hook === undefined) lazyHookCreation();
|
||||||
if (config.bits < 64) {
|
if (config.bits < 64) {
|
||||||
// V8 Inspector stores task ids as (void*) pointers.
|
// V8 Inspector stores task ids as (void*) pointers.
|
||||||
// async_hooks store ids as 64bit numbers.
|
// async_hooks store ids as 64bit numbers.
|
||||||
@ -61,6 +68,7 @@ function enable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function disable() {
|
function disable() {
|
||||||
|
if (hook === undefined) lazyHookCreation();
|
||||||
hook.disable();
|
hook.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user