Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9feca3e

Browse filesBrowse 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
Copy full SHA for 9feca3e

File tree

Expand file treeCollapse file tree

1 file changed

+35
-27
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-27
lines changed
Open diff view settings
Collapse file

‎lib/internal/inspector_async_hook.js‎

Copy file name to clipboardExpand all lines: lib/internal/inspector_async_hook.js
+35-27Lines 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
const inspector = process.binding('inspector');
5-
const config = process.binding('config');
64

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

12-
const hook = createHook({
13-
init(asyncId, type, triggerAsyncId, resource) {
10+
let hook;
11+
let config;
12+
13+
function lazyHookCreation() {
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-
const recurring = true;
19-
if (type === 'PROMISE')
20-
this.promiseIds.add(asyncId);
21-
else
22-
inspector.asyncTaskScheduled(type, asyncId, recurring);
23-
},
23+
const recurring = 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-
return this.promiseIds.delete(asyncId);
40-
inspector.asyncTaskCanceled(asyncId);
41-
},
42-
});
42+
destroy(asyncId) {
43+
if (this.promiseIds.has(asyncId))
44+
return this.promiseIds.delete(asyncId);
45+
inspector.asyncTaskCanceled(asyncId);
46+
},
47+
});
4348

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

4652
function enable() {
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
function disable() {
71+
if (hook === undefined) lazyHookCreation();
6472
hook.disable();
6573
}
6674

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.