Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba00875

Browse filesBrowse files
mcollinaaduh95
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 6d24b88 commit ba00875
Copy full SHA for ba00875

File tree

Expand file treeCollapse file tree

1 file changed

+12
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+12
-3
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/streams/end-of-stream.js
+12-3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.