Skip to content

Commit

Permalink
async_hooks: lazy loading for startup performance
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 22, 2018
1 parent 74db9f4 commit 9feca3e
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions lib/internal/inspector_async_hook.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
'use strict';

const { createHook } = require('async_hooks');
const inspector = process.binding('inspector');
const config = process.binding('config');

if (!inspector || !inspector.asyncTaskScheduled) {
exports.setup = function() {};
return;
}

const hook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
let hook;
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,
// therefore we mark all tasks as recurring. Based on the discussion
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
// this should be fine as long as we call asyncTaskCanceled() too.
const recurring = true;
if (type === 'PROMISE')
this.promiseIds.add(asyncId);
else
inspector.asyncTaskScheduled(type, asyncId, recurring);
},
const recurring = true;
if (type === 'PROMISE')
this.promiseIds.add(asyncId);
else
inspector.asyncTaskScheduled(type, asyncId, recurring);
},

before(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskStarted(asyncId);
},
before(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskStarted(asyncId);
},

after(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskFinished(asyncId);
},
after(asyncId) {
if (this.promiseIds.has(asyncId))
return;
inspector.asyncTaskFinished(asyncId);
},

destroy(asyncId) {
if (this.promiseIds.has(asyncId))
return this.promiseIds.delete(asyncId);
inspector.asyncTaskCanceled(asyncId);
},
});
destroy(asyncId) {
if (this.promiseIds.has(asyncId))
return this.promiseIds.delete(asyncId);
inspector.asyncTaskCanceled(asyncId);
},
});

hook.promiseIds = new Set();
hook.promiseIds = new Set();
}

function enable() {
if (hook === undefined) lazyHookCreation();
if (config.bits < 64) {
// V8 Inspector stores task ids as (void*) pointers.
// async_hooks store ids as 64bit numbers.
Expand All @@ -61,6 +68,7 @@ function enable() {
}

function disable() {
if (hook === undefined) lazyHookCreation();
hook.disable();
}

Expand Down

0 comments on commit 9feca3e

Please sign in to comment.