From fbad8bb1fa04bd47fdcc430a75a36c90ee996f09 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Fri, 5 Jan 2018 09:03:10 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/18004 Reviewed-By: Andreas Madsen Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig --- lib/dgram.js | 4 ++-- lib/internal/async_hooks.js | 4 ++-- lib/net.js | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index d2cec9dd4e1..0c4990cac5d 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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 ); }; diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 7d99985dd11..801d90b7f50 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -260,7 +260,7 @@ 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]; @@ -268,7 +268,7 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, opaque, block) { var ret; try { - ret = Reflect.apply(block, null, opaque); + ret = Reflect.apply(block, null, args); } finally { async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId; } diff --git a/lib/net.js b/lib/net.js index 487a7f55fb9..565a649a63a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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) @@ -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); @@ -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; @@ -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); @@ -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 ); } });