summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnatoli Papirovski <anatoli.papirovski@postmates.com>2019-03-31 12:41:14 -0700
committerDaniel Bevenius <daniel.bevenius@gmail.com>2019-04-05 05:37:01 +0200
commitd3d4e10107a594a758baf2cd5e2c1258671e58c5 (patch)
tree633fd85a27faf2cb3666a0c49bdf0f9db10aa206
parentaf03de48d89175a9ef712228786a83968ff788df (diff)
downloadandroid-node-v8-d3d4e10107a594a758baf2cd5e2c1258671e58c5.tar.gz
android-node-v8-d3d4e10107a594a758baf2cd5e2c1258671e58c5.tar.bz2
android-node-v8-d3d4e10107a594a758baf2cd5e2c1258671e58c5.zip
async_hooks: improve AsyncResource performance
Accessing symbols is generally quite expensive and so is emitInit, only do both when actually required. PR-URL: https://github.com/nodejs/node/pull/27032 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--lib/async_hooks.js25
-rw-r--r--lib/internal/async_hooks.js5
2 files changed, 15 insertions, 15 deletions
diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index 88b8fd705a..3a12a241ea 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -27,6 +27,7 @@ const {
emitBefore,
emitAfter,
emitDestroy,
+ initHooksExist,
} = internal_async_hooks;
// Get symbols
@@ -146,29 +147,31 @@ class AsyncResource {
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
}
- this[async_id_symbol] = newAsyncId();
+ const asyncId = newAsyncId();
+ this[async_id_symbol] = asyncId;
this[trigger_async_id_symbol] = triggerAsyncId;
+
// This prop name (destroyed) has to be synchronized with C++
- this[destroyedSymbol] = { destroyed: false };
+ const destroyed = { destroyed: false };
+ this[destroyedSymbol] = destroyed;
- emitInit(
- this[async_id_symbol], type, this[trigger_async_id_symbol], this
- );
+ if (initHooksExist()) {
+ emitInit(asyncId, type, triggerAsyncId, this);
+ }
if (!opts.requireManualDestroy) {
- registerDestroyHook(this, this[async_id_symbol], this[destroyedSymbol]);
+ registerDestroyHook(this, asyncId, destroyed);
}
}
runInAsyncScope(fn, thisArg, ...args) {
- emitBefore(this[async_id_symbol], this[trigger_async_id_symbol]);
- let ret;
+ const asyncId = this[async_id_symbol];
+ emitBefore(asyncId, this[trigger_async_id_symbol]);
try {
- ret = Reflect.apply(fn, thisArg, args);
+ return Reflect.apply(fn, thisArg, args);
} finally {
- emitAfter(this[async_id_symbol]);
+ emitAfter(asyncId);
}
- return ret;
}
emitDestroy() {
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js
index 676a2bd2c0..a1f3c07def 100644
--- a/lib/internal/async_hooks.js
+++ b/lib/internal/async_hooks.js
@@ -286,14 +286,11 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
- let ret;
try {
- ret = Reflect.apply(block, null, args);
+ return Reflect.apply(block, null, args);
} finally {
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
}
-
- return ret;
}