Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Pechkurov <apechkurov@gmail.com>2020-03-22 21:53:52 +0300
committerGerhard Stoebich <18708370+Flarna@users.noreply.github.com>2020-04-06 17:14:43 +0300
commit561dda273fc6590d998b2af0cf0c63654c679d48 (patch)
treef23a27ec8aeb022754852ef40d32bee2ba18e210
parent78e6d2484fcacdee0cc0a1303e49a5328f3470dd (diff)
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>
-rw-r--r--benchmark/async_hooks/gc-tracking.js11
-rw-r--r--doc/api/async_hooks.md2
-rw-r--r--lib/async_hooks.js3
3 files changed, 14 insertions, 2 deletions
diff --git a/benchmark/async_hooks/gc-tracking.js b/benchmark/async_hooks/gc-tracking.js
index 030b5f8e193..4a59a5a4b65 100644
--- a/benchmark/async_hooks/gc-tracking.js
+++ b/benchmark/async_hooks/gc-tracking.js
@@ -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++) {
diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md
index 17cdf61c39d..86c086ae4b2 100644
--- a/doc/api/async_hooks.md
+++ b/doc/api/async_hooks.md
@@ -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:
diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index d676f6dfcb4..cb017222a9b 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -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;