Skip to content

Commit 213188e

Browse files
mcollinatargos
authored andcommitted
stream: use new AsyncResource instead of bind
The bind method uses ObjectDefineProperty that shows up in flamegraphs. This changes it to avoid the utility. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #59867 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 039ac19 commit 213188e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/internal/streams/end-of-stream.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const {
4545
} = require('internal/streams/utils');
4646

4747
// Lazy load
48-
let AsyncLocalStorage;
48+
let AsyncResource;
4949
let addAbortListener;
5050

5151
function isRequest(stream) {
@@ -54,6 +54,14 @@ function isRequest(stream) {
5454

5555
const nop = () => {};
5656

57+
function bindAsyncResource(fn, type) {
58+
AsyncResource ??= require('async_hooks').AsyncResource;
59+
const resource = new AsyncResource(type);
60+
return function(...args) {
61+
return resource.runInAsyncScope(fn, this, ...args);
62+
};
63+
}
64+
5765
function eos(stream, options, callback) {
5866
if (arguments.length === 2) {
5967
callback = options;
@@ -66,8 +74,9 @@ function eos(stream, options, callback) {
6674
validateFunction(callback, 'callback');
6775
validateAbortSignal(options.signal, 'options.signal');
6876

69-
AsyncLocalStorage ??= require('async_hooks').AsyncLocalStorage;
70-
callback = once(AsyncLocalStorage.bind(callback));
77+
// Avoid AsyncResource.bind() because it calls ObjectDefineProperties which
78+
// is a bottleneck here.
79+
callback = once(bindAsyncResource(callback, 'STREAM_END_OF_STREAM'));
7180

7281
if (isReadableStream(stream) || isWritableStream(stream)) {
7382
return eosWeb(stream, options, callback);

0 commit comments

Comments
 (0)