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.

Backport-PR-URL: #18179
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]>
  • Loading branch information
apapirovski authored and MylesBorins committed Jan 19, 2018
1 parent 1d3d1dd commit 5dab90b
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 @@ -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
);
};

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 @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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
);
}
});
Expand Down

0 comments on commit 5dab90b

Please sign in to comment.