GeezerVison is showing github.com — async_hooks: move to lazy destroy hook registration in AsyncResource · nodejs/node@b632231
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/async_hooks/gc-tracking.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const common = require('../common.js'); | 3 | | -const { AsyncResource } = require('async_hooks'); | | 3 | +const { createHook, AsyncResource } = require('async_hooks'); | 4 | 4 |
| 5 | 5 | const bench = common.createBenchmark(main, { | 6 | 6 | n: [1e6], | 7 | 7 | method: [ | 8 | 8 | 'trackingEnabled', | | 9 | + 'trackingEnabledWithDestroyHook', | 9 | 10 | 'trackingDisabled', | 10 | 11 | ] | 11 | 12 | }, { | @@ -30,6 +31,14 @@ function main({ n, method }) { | 30 | 31 | } | 31 | 32 | endAfterGC(n); | 32 | 33 | 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; | 33 | 42 | case 'trackingDisabled': | 34 | 43 | bench.start(); | 35 | 44 | for (let i = 0; i < n; i++) { | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/async_hooks.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -685,6 +685,8 @@ asyncResource.triggerAsyncId(); | 685 | 685 | when the object is garbage collected. This usually does not need to be set | 686 | 686 | (even if `emitDestroy` is called manually), unless the resource's `asyncId` | 687 | 687 | is retrieved and the sensitive API's `emitDestroy` is called with it. | | 688 | + When set to `false`, the `emitDestroy` call on garbage collection | | 689 | + will only take place if there is at least one active `destroy` hook. | 688 | 690 | **Default:** `false`. | 689 | 691 |
| 690 | 692 | Example usage: | |
Collapse file Copy file name to clipboardExpand all lines: lib/async_hooks.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ const { | 36 | 36 | emitDestroy, | 37 | 37 | enabledHooksExist, | 38 | 38 | initHooksExist, | | 39 | + destroyHooksExist, | 39 | 40 | } = internal_async_hooks; | 40 | 41 |
| 41 | 42 | // Get symbols | @@ -168,7 +169,7 @@ class AsyncResource { | 168 | 169 | emitInit(asyncId, type, triggerAsyncId, this); | 169 | 170 | } | 170 | 171 |
| 171 | | - if (!requireManualDestroy) { | | 172 | + if (!requireManualDestroy && destroyHooksExist()) { | 172 | 173 | // This prop name (destroyed) has to be synchronized with C++ | 173 | 174 | const destroyed = { destroyed: false }; | 174 | 175 | this[destroyedSymbol] = destroyed; | |
|