async_hooks: move to lazy destroy hook registration in AsyncResource

PR-URL: https://github.com/nodejs/node/pull/32429
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
This commit is contained in:
Andrey Pechkurov 2020-03-22 21:53:52 +03:00 committed by Gerhard Stoebich
parent 78e6d2484f
commit 561dda273f
3 changed files with 14 additions and 2 deletions

View File

@ -1,11 +1,12 @@
'use strict';
const common = require('../common.js');
const { AsyncResource } = require('async_hooks');
const { createHook, AsyncResource } = require('async_hooks');
const bench = common.createBenchmark(main, {
n: [1e6],
method: [
'trackingEnabled',
'trackingEnabledWithDestroyHook',
'trackingDisabled',
]
}, {
@ -30,6 +31,14 @@ function main({ n, method }) {
}
endAfterGC(n);
break;
case 'trackingEnabledWithDestroyHook':
createHook({ destroy: () => {} }).enable();
bench.start();
for (let i = 0; i < n; i++) {
new AsyncResource('foobar');
}
endAfterGC(n);
break;
case 'trackingDisabled':
bench.start();
for (let i = 0; i < n; i++) {

View File

@ -685,6 +685,8 @@ asyncResource.triggerAsyncId();
when the object is garbage collected. This usually does not need to be set
(even if `emitDestroy` is called manually), unless the resource's `asyncId`
is retrieved and the sensitive API's `emitDestroy` is called with it.
When set to `false`, the `emitDestroy` call on garbage collection
will only take place if there is at least one active `destroy` hook.
**Default:** `false`.
Example usage:

View File

@ -36,6 +36,7 @@ const {
emitDestroy,
enabledHooksExist,
initHooksExist,
destroyHooksExist,
} = internal_async_hooks;
// Get symbols
@ -168,7 +169,7 @@ class AsyncResource {
emitInit(asyncId, type, triggerAsyncId, this);
}
if (!requireManualDestroy) {
if (!requireManualDestroy && destroyHooksExist()) {
// This prop name (destroyed) has to be synchronized with C++
const destroyed = { destroyed: false };
this[destroyedSymbol] = destroyed;