Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnatoli Papirovski <anatoli.papirovski@postmates.com>2019-03-31 23:40:14 +0300
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2019-04-10 17:30:31 +0300
commitd688a936862c55b5386c6b360062928c47dc0a84 (patch)
tree20effebc5bddc9c3cf13965b624a6e13d18753dd
parent066cd6090f89d258b515b6ee5abea9d4cae6e9fb (diff)
async_hooks: minor cleanup and improvements
Cleanup some code and make the emit hooks very slightly faster. PR-URL: https://github.com/nodejs/node/pull/27034 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
-rw-r--r--lib/internal/async_hooks.js63
1 files changed, 32 insertions, 31 deletions
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js
index 53c8d08a7d9..fac34c06696 100644
--- a/lib/internal/async_hooks.js
+++ b/lib/internal/async_hooks.js
@@ -75,6 +75,8 @@ const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
kCheck, kExecutionAsyncId, kAsyncIdCounter, kTriggerAsyncId,
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
+const FunctionBind = Function.call.bind(Function.prototype.bind);
+
// Used in AsyncHook and AsyncResource.
const async_id_symbol = Symbol('asyncId');
const trigger_async_id_symbol = Symbol('triggerAsyncId');
@@ -148,38 +150,37 @@ function emitInitNative(asyncId, type, triggerAsyncId, resource) {
}
}
-
-function emitHookFactory(symbol, name) {
- // Called from native. The asyncId stack handling is taken care of there
- // before this is called.
- // eslint-disable-next-line func-style
- const fn = function(asyncId) {
- active_hooks.call_depth += 1;
- // Use a single try/catch for all hook to avoid setting up one per
- // iteration.
- try {
- for (var i = 0; i < active_hooks.array.length; i++) {
- if (typeof active_hooks.array[i][symbol] === 'function') {
- active_hooks.array[i][symbol](asyncId);
- }
+// Called from native. The asyncId stack handling is taken care of there
+// before this is called.
+function emitHook(symbol, asyncId) {
+ active_hooks.call_depth += 1;
+ // Use a single try/catch for all hook to avoid setting up one per
+ // iteration.
+ try {
+ for (var i = 0; i < active_hooks.array.length; i++) {
+ if (typeof active_hooks.array[i][symbol] === 'function') {
+ active_hooks.array[i][symbol](asyncId);
}
- } catch (e) {
- fatalError(e);
- } finally {
- active_hooks.call_depth -= 1;
}
+ } catch (e) {
+ fatalError(e);
+ } finally {
+ active_hooks.call_depth -= 1;
+ }
- // Hooks can only be restored if there have been no recursive hook calls.
- // Also the active hooks do not need to be restored if enable()/disable()
- // weren't called during hook execution, in which case
- // active_hooks.tmp_array will be null.
- if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
- restoreActiveHooks();
- }
- };
+ // Hooks can only be restored if there have been no recursive hook calls.
+ // Also the active hooks do not need to be restored if enable()/disable()
+ // weren't called during hook execution, in which case
+ // active_hooks.tmp_array will be null.
+ if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
+ restoreActiveHooks();
+ }
+}
+
+function emitHookFactory(symbol, name) {
+ const fn = FunctionBind(emitHook, undefined, symbol);
- // Set the name property of the anonymous function as it looks good in the
- // stack trace.
+ // Set the name property of the function as it looks good in the stack trace.
Object.defineProperty(fn, 'name', {
value: name
});
@@ -261,10 +262,10 @@ function getOrSetAsyncId(object) {
// the user to safeguard this call and make sure it's zero'd out when the
// constructor is complete.
function getDefaultTriggerAsyncId() {
- let defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
+ const defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
if (defaultTriggerAsyncId < 0)
- defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
+ return async_id_fields[kExecutionAsyncId];
return defaultTriggerAsyncId;
}
@@ -393,8 +394,8 @@ function pushAsyncIds(asyncId, triggerAsyncId) {
// This is the equivalent of the native pop_async_ids() call.
function popAsyncIds(asyncId) {
- if (async_hook_fields[kStackLength] === 0) return false;
const stackLength = async_hook_fields[kStackLength];
+ if (stackLength === 0) return false;
if (async_hook_fields[kCheck] > 0 &&
async_id_fields[kExecutionAsyncId] !== asyncId) {