Skip to content

Commit fbad8bb

Browse files
committed
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. 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 5a1aeab commit fbad8bb

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
@@ -452,8 +452,8 @@ Socket.prototype.send = function(buffer,
452452
const afterDns = (ex, ip) => {
453453
defaultTriggerAsyncIdScope(
454454
this[async_id_symbol],
455-
[ex, this, ip, list, address, port, callback],
456-
doSend
455+
doSend,
456+
ex, this, ip, list, address, port, callback
457457
);
458458
};
459459

lib/internal/async_hooks.js

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

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

319319
var err = defaultTriggerAsyncIdScope(
320-
this[async_id_symbol], [this, afterShutdown], shutdownSocket
320+
this[async_id_symbol], shutdownSocket, this, afterShutdown
321321
);
322322

323323
if (err)
@@ -1043,7 +1043,7 @@ Socket.prototype.connect = function(...args) {
10431043
path);
10441044
}
10451045
defaultTriggerAsyncIdScope(
1046-
this[async_id_symbol], [this, path], internalConnect
1046+
this[async_id_symbol], internalConnect, this, path
10471047
);
10481048
} else {
10491049
lookupAndConnect(this, options);
@@ -1089,8 +1089,8 @@ function lookupAndConnect(self, options) {
10891089
if (self.connecting)
10901090
defaultTriggerAsyncIdScope(
10911091
self[async_id_symbol],
1092-
[self, host, port, addressType, localAddress, localPort],
1093-
internalConnect
1092+
internalConnect,
1093+
self, host, port, addressType, localAddress, localPort
10941094
);
10951095
});
10961096
return;
@@ -1118,7 +1118,7 @@ function lookupAndConnect(self, options) {
11181118
debug('connect: dns options', dnsopts);
11191119
self._host = host;
11201120
var lookup = options.lookup || dns.lookup;
1121-
defaultTriggerAsyncIdScope(self[async_id_symbol], [], function() {
1121+
defaultTriggerAsyncIdScope(self[async_id_symbol], function() {
11221122
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
11231123
self.emit('lookup', err, ip, addressType, host);
11241124

@@ -1140,8 +1140,8 @@ function lookupAndConnect(self, options) {
11401140
self._unrefTimer();
11411141
defaultTriggerAsyncIdScope(
11421142
self[async_id_symbol],
1143-
[self, ip, port, addressType, localAddress, localPort],
1144-
internalConnect
1143+
internalConnect,
1144+
self, ip, port, addressType, localAddress, localPort
11451145
);
11461146
}
11471147
});

0 commit comments

Comments
 (0)