Skip to content

Commit 5dab90b

Browse files
apapirovskiMylesBorins
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: #18179 PR-URL: #18004 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 1d3d1dd commit 5dab90b

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

lib/dgram.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,15 @@ function getDefaultTriggerAsyncId() {
263263
}
264264

265265

266-
function defaultTriggerAsyncIdScope(triggerAsyncId, opaque, block) {
266+
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
267267
// CHECK(Number.isSafeInteger(triggerAsyncId))
268268
// CHECK(triggerAsyncId > 0)
269269
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
270270
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
271271

272272
var ret;
273273
try {
274-
ret = Reflect.apply(block, null, opaque);
274+
ret = Reflect.apply(block, null, args);
275275
} finally {
276276
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
277277
}

lib/net.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function onSocketFinish() {
300300
return this.destroy();
301301

302302
var err = defaultTriggerAsyncIdScope(
303-
this[async_id_symbol], [this, afterShutdown], shutdownSocket
303+
this[async_id_symbol], shutdownSocket, this, afterShutdown
304304
);
305305

306306
if (err)
@@ -1031,7 +1031,7 @@ Socket.prototype.connect = function(...args) {
10311031
path);
10321032
}
10331033
defaultTriggerAsyncIdScope(
1034-
this[async_id_symbol], [this, path], internalConnect
1034+
this[async_id_symbol], internalConnect, this, path
10351035
);
10361036
} else {
10371037
lookupAndConnect(this, options);
@@ -1073,8 +1073,8 @@ function lookupAndConnect(self, options) {
10731073
if (self.connecting)
10741074
defaultTriggerAsyncIdScope(
10751075
self[async_id_symbol],
1076-
[self, host, port, addressType, localAddress, localPort],
1077-
internalConnect
1076+
internalConnect,
1077+
self, host, port, addressType, localAddress, localPort
10781078
);
10791079
});
10801080
return;
@@ -1096,7 +1096,7 @@ function lookupAndConnect(self, options) {
10961096
debug('connect: dns options', dnsopts);
10971097
self._host = host;
10981098
var lookup = options.lookup || dns.lookup;
1099-
defaultTriggerAsyncIdScope(self[async_id_symbol], [], function() {
1099+
defaultTriggerAsyncIdScope(self[async_id_symbol], function() {
11001100
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
11011101
self.emit('lookup', err, ip, addressType, host);
11021102

@@ -1118,8 +1118,8 @@ function lookupAndConnect(self, options) {
11181118
self._unrefTimer();
11191119
defaultTriggerAsyncIdScope(
11201120
self[async_id_symbol],
1121-
[self, ip, port, addressType, localAddress, localPort],
1122-
internalConnect
1121+
internalConnect,
1122+
self, ip, port, addressType, localAddress, localPort
11231123
);
11241124
}
11251125
});

0 commit comments

Comments
 (0)