Skip to content

Commit 28abbfd

Browse files
puzpuzpuztargos
authored andcommittedApr 22, 2020
async_hooks: move to lazy destroy hook registration in AsyncResource
PR-URL: #32429 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
1 parent 4305551 commit 28abbfd

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
 

‎benchmark/async_hooks/gc-tracking.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { AsyncResource } = require('async_hooks');
3+
const { createHook, AsyncResource } = require('async_hooks');
44

55
const bench = common.createBenchmark(main, {
66
n: [1e6],
77
method: [
88
'trackingEnabled',
9+
'trackingEnabledWithDestroyHook',
910
'trackingDisabled',
1011
]
1112
}, {
@@ -30,6 +31,14 @@ function main({ n, method }) {
3031
}
3132
endAfterGC(n);
3233
break;
34+
case 'trackingEnabledWithDestroyHook':
35+
createHook({ destroy: () => {} }).enable();
36+
bench.start();
37+
for (let i = 0; i < n; i++) {
38+
new AsyncResource('foobar');
39+
}
40+
endAfterGC(n);
41+
break;
3342
case 'trackingDisabled':
3443
bench.start();
3544
for (let i = 0; i < n; i++) {

‎doc/api/async_hooks.md

+2
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ asyncResource.triggerAsyncId();
629629
when the object is garbage collected. This usually does not need to be set
630630
(even if `emitDestroy` is called manually), unless the resource's `asyncId`
631631
is retrieved and the sensitive API's `emitDestroy` is called with it.
632+
When set to `false`, the `emitDestroy` call on garbage collection
633+
will only take place if there is at least one active `destroy` hook.
632634
**Default:** `false`.
633635

634636
Example usage:

‎lib/async_hooks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
emitDestroy,
3636
enabledHooksExist,
3737
initHooksExist,
38+
destroyHooksExist,
3839
} = internal_async_hooks;
3940

4041
// Get symbols
@@ -167,7 +168,7 @@ class AsyncResource {
167168
emitInit(asyncId, type, triggerAsyncId, this);
168169
}
169170

170-
if (!requireManualDestroy) {
171+
if (!requireManualDestroy && destroyHooksExist()) {
171172
// This prop name (destroyed) has to be synchronized with C++
172173
const destroyed = { destroyed: false };
173174
this[destroyedSymbol] = destroyed;

0 commit comments

Comments
 (0)
Please sign in to comment.