Skip to content

Commit 0a68018

Browse files
apapirovskievanlucas
authored andcommitted
async_hooks: update defaultTriggerAsyncIdScope for perf
The existing version of defaultTriggerAsyncIdScope creates an Array for the callback's arguments which is highly inefficient. Instead, use rest syntax and allow V8 to do that work for us. This yields roughly 2x performance for this particular function. Backport-PR-URL: #18474 PR-URL: #18004 Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent c4abdcd commit 0a68018

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

lib/dgram.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ Socket.prototype.send = function(buffer,
450450
const afterDns = (ex, ip) => {
451451
defaultTriggerAsyncIdScope(
452452
this[async_id_symbol],
453-
[ex, this, ip, list, address, port, callback],
454-
doSend
453+
doSend,
454+
ex, this, ip, list, address, port, callback
455455
);
456456
};
457457

lib/internal/async_hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ function getDefaultTriggerAsyncId() {
260260
}
261261

262262

263-
function defaultTriggerAsyncIdScope(triggerAsyncId, opaque, block) {
263+
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
264264
// CHECK(Number.isSafeInteger(triggerAsyncId))
265265
// CHECK(triggerAsyncId > 0)
266266
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
267267
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
268268

269269
var ret;
270270
try {
271-
ret = Reflect.apply(block, null, opaque);
271+
ret = Reflect.apply(block, null, args);
272272
} finally {
273273
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
274274
}

lib/net.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ function onSocketFinish() {
304304
return this.destroy();
305305

306306
var err = defaultTriggerAsyncIdScope(
307-
this[async_id_symbol], [this, afterShutdown], shutdownSocket
307+
this[async_id_symbol], shutdownSocket, this, afterShutdown
308308
);
309309

310310
if (err)
@@ -1017,7 +1017,7 @@ Socket.prototype.connect = function(...args) {
10171017
path);
10181018
}
10191019
defaultTriggerAsyncIdScope(
1020-
this[async_id_symbol], [this, path], internalConnect
1020+
this[async_id_symbol], internalConnect, this, path
10211021
);
10221022
} else {
10231023
lookupAndConnect(this, options);
@@ -1063,8 +1063,8 @@ function lookupAndConnect(self, options) {
10631063
if (self.connecting)
10641064
defaultTriggerAsyncIdScope(
10651065
self[async_id_symbol],
1066-
[self, host, port, addressType, localAddress, localPort],
1067-
internalConnect
1066+
internalConnect,
1067+
self, host, port, addressType, localAddress, localPort
10681068
);
10691069
});
10701070
return;
@@ -1092,7 +1092,7 @@ function lookupAndConnect(self, options) {
10921092
debug('connect: dns options', dnsopts);
10931093
self._host = host;
10941094
var lookup = options.lookup || dns.lookup;
1095-
defaultTriggerAsyncIdScope(self[async_id_symbol], [], function() {
1095+
defaultTriggerAsyncIdScope(self[async_id_symbol], function() {
10961096
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
10971097
self.emit('lookup', err, ip, addressType, host);
10981098

@@ -1114,8 +1114,8 @@ function lookupAndConnect(self, options) {
11141114
self._unrefTimer();
11151115
defaultTriggerAsyncIdScope(
11161116
self[async_id_symbol],
1117-
[self, ip, port, addressType, localAddress, localPort],
1118-
internalConnect
1117+
internalConnect,
1118+
self, ip, port, addressType, localAddress, localPort
11191119
);
11201120
}
11211121
});

0 commit comments

Comments
 (0)