GeezerVison is showing github.com — async_hooks: move to lazy destroy hook registration in AsyncResource · nodejs/node@28abbfd
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 |
|---|
@@ -629,6 +629,8 @@ asyncResource.triggerAsyncId(); | 629 | 629 | when the object is garbage collected. This usually does not need to be set | 630 | 630 | (even if `emitDestroy` is called manually), unless the resource's `asyncId` | 631 | 631 | 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. | 632 | 634 | **Default:** `false`. | 633 | 635 |
| 634 | 636 | 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 |
|---|
@@ -35,6 +35,7 @@ const { | 35 | 35 | emitDestroy, | 36 | 36 | enabledHooksExist, | 37 | 37 | initHooksExist, | | 38 | + destroyHooksExist, | 38 | 39 | } = internal_async_hooks; | 39 | 40 |
| 40 | 41 | // Get symbols | @@ -167,7 +168,7 @@ class AsyncResource { | 167 | 168 | emitInit(asyncId, type, triggerAsyncId, this); | 168 | 169 | } | 169 | 170 |
| 170 | | - if (!requireManualDestroy) { | | 171 | + if (!requireManualDestroy && destroyHooksExist()) { | 171 | 172 | // This prop name (destroyed) has to be synchronized with C++ | 172 | 173 | const destroyed = { destroyed: false }; | 173 | 174 | this[destroyedSymbol] = destroyed; | |
|