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: #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>
  • Loading branch information
apapirovski committed Jan 9, 2018
1 parent 5a1aeab commit fbad8bb
Show file tree
Hide file tree
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
Expand Up @@ -452,8 +452,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
);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,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;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,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)
Expand Down Expand Up @@ -1043,7 +1043,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);
Expand Down Expand Up @@ -1089,8 +1089,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;
Expand Down Expand Up @@ -1118,7 +1118,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);

Expand All @@ -1140,8 +1140,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
);
}
});
Expand Down

0 comments on commit fbad8bb

Please sign in to comment.