GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — async_hooks: move to lazy destroy hook registration in AsyncResource · nodejs/node@28abbfd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/async_hooks
    • gc-tracking.js
  • doc/api
    • async_hooks.md
  • lib
    • async_hooks.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/async_hooks/gc-tracking.js‎

Copy file name to clipboardExpand all lines: benchmark/async_hooks/gc-tracking.js
+10-1Lines changed: 10 additions & 1 deletion
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++) {
Collapse file

‎doc/api/async_hooks.md‎

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 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:
Collapse file

‎lib/async_hooks.js‎

Copy file name to clipboardExpand all lines: lib/async_hooks.js
+2-1Lines changed: 2 additions & 1 deletion
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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.