Skip to content

Commit 9feca3e

Browse files
BridgeARMylesBorins
authored andcommitted
async_hooks: lazy loading for startup performance
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>
1 parent 74db9f4 commit 9feca3e

1 file changed

Lines changed: 35 additions & 27 deletions

File tree

‎lib/internal/inspector_async_hook.js‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
11
'use strict';
22

3-
const{ createHook }=require('async_hooks');
43
constinspector=process.binding('inspector');
5-
constconfig=process.binding('config');
64

75
if(!inspector||!inspector.asyncTaskScheduled){
86
exports.setup=function(){};
97
return;
108
}
119

12-
consthook=createHook({
13-
init(asyncId,type,triggerAsyncId,resource){
10+
lethook;
11+
letconfig;
12+
13+
functionlazyHookCreation(){
14+
const{ createHook }=require('async_hooks');
15+
config=process.binding('config');
16+
17+
hook=createHook({
18+
init(asyncId,type,triggerAsyncId,resource){
1419
// It's difficult to tell which tasks will be recurring and which won't,
1520
// therefore we mark all tasks as recurring. Based on the discussion
1621
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
1722
// this should be fine as long as we call asyncTaskCanceled() too.
18-
constrecurring=true;
19-
if(type==='PROMISE')
20-
this.promiseIds.add(asyncId);
21-
else
22-
inspector.asyncTaskScheduled(type,asyncId,recurring);
23-
},
23+
constrecurring=true;
24+
if(type==='PROMISE')
25+
this.promiseIds.add(asyncId);
26+
else
27+
inspector.asyncTaskScheduled(type,asyncId,recurring);
28+
},
2429

25-
before(asyncId){
26-
if(this.promiseIds.has(asyncId))
27-
return;
28-
inspector.asyncTaskStarted(asyncId);
29-
},
30+
before(asyncId){
31+
if(this.promiseIds.has(asyncId))
32+
return;
33+
inspector.asyncTaskStarted(asyncId);
34+
},
3035

31-
after(asyncId){
32-
if(this.promiseIds.has(asyncId))
33-
return;
34-
inspector.asyncTaskFinished(asyncId);
35-
},
36+
after(asyncId){
37+
if(this.promiseIds.has(asyncId))
38+
return;
39+
inspector.asyncTaskFinished(asyncId);
40+
},
3641

37-
destroy(asyncId){
38-
if(this.promiseIds.has(asyncId))
39-
returnthis.promiseIds.delete(asyncId);
40-
inspector.asyncTaskCanceled(asyncId);
41-
},
42-
});
42+
destroy(asyncId){
43+
if(this.promiseIds.has(asyncId))
44+
returnthis.promiseIds.delete(asyncId);
45+
inspector.asyncTaskCanceled(asyncId);
46+
},
47+
});
4348

44-
hook.promiseIds=newSet();
49+
hook.promiseIds=newSet();
50+
}
4551

4652
functionenable(){
53+
if(hook===undefined)lazyHookCreation();
4754
if(config.bits<64){
4855
// V8 Inspector stores task ids as (void*) pointers.
4956
// async_hooks store ids as 64bit numbers.
@@ -61,6 +68,7 @@ function enable() {
6168
}
6269

6370
functiondisable(){
71+
if(hook===undefined)lazyHookCreation();
6472
hook.disable();
6573
}
6674

0 commit comments

Comments
 (0)