Skip to content

Commit

Permalink
async_hooks: update defaultTriggerAsyncIdScope for perf
Browse files Browse the repository at this point in the history
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: nodejs#18004
Backport-PR-URL: nodejs#18179
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>
apapirovski authored and gibfahn committed Jan 17, 2018

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 4087dc8 commit a8b7e43
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
@@ -450,8 +450,8 @@ Socket.prototype.send = function(buffer,
const afterDns = (ex, ip) => {
defaultTriggerAsyncIdScope(
this[async_id_symbol],
[ex, this, ip, list, address, port, callback],
doSend
doSend,
ex, this, ip, list, address, port, callback
);
};

4 changes: 2 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
@@ -263,15 +263,15 @@ function getDefaultTriggerAsyncId() {
}


function defaultTriggerAsyncIdScope(triggerAsyncId, opaque, block) {
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
// CHECK(Number.isSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;

var ret;
try {
ret = Reflect.apply(block, null, opaque);
ret = Reflect.apply(block, null, args);
} finally {
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
}
14 changes: 7 additions & 7 deletions lib/net.js
Original file line number Diff line number Diff line change
@@ -300,7 +300,7 @@ function onSocketFinish() {
return this.destroy();

var err = defaultTriggerAsyncIdScope(
this[async_id_symbol], [this, afterShutdown], shutdownSocket
this[async_id_symbol], shutdownSocket, this, afterShutdown
);

if (err)
@@ -1031,7 +1031,7 @@ Socket.prototype.connect = function(...args) {
path);
}
defaultTriggerAsyncIdScope(
this[async_id_symbol], [this, path], internalConnect
this[async_id_symbol], internalConnect, this, path
);
} else {
lookupAndConnect(this, options);
@@ -1073,8 +1073,8 @@ function lookupAndConnect(self, options) {
if (self.connecting)
defaultTriggerAsyncIdScope(
self[async_id_symbol],
[self, host, port, addressType, localAddress, localPort],
internalConnect
internalConnect,
self, host, port, addressType, localAddress, localPort
);
});
return;
@@ -1096,7 +1096,7 @@ function lookupAndConnect(self, options) {
debug('connect: dns options', dnsopts);
self._host = host;
var lookup = options.lookup || dns.lookup;
defaultTriggerAsyncIdScope(self[async_id_symbol], [], function() {
defaultTriggerAsyncIdScope(self[async_id_symbol], function() {
lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
self.emit('lookup', err, ip, addressType, host);

@@ -1118,8 +1118,8 @@ function lookupAndConnect(self, options) {
self._unrefTimer();
defaultTriggerAsyncIdScope(
self[async_id_symbol],
[self, ip, port, addressType, localAddress, localPort],
internalConnect
internalConnect,
self, ip, port, addressType, localAddress, localPort
);
}
});

0 comments on commit a8b7e43

Please sign in to comment.