Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca7c4f4

Browse files
apapirovskiBethGriggs
authored andcommittedApr 9, 2019
async_hooks: minor cleanup and improvements
Cleanup some code and make the emit hooks very slightly faster. PR-URL: #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>
1 parent 1d2f4c4 commit ca7c4f4

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed
 

‎lib/internal/async_hooks.js

+32-31
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
7575
kCheck, kExecutionAsyncId, kAsyncIdCounter, kTriggerAsyncId,
7676
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
7777

78+
const FunctionBind = Function.call.bind(Function.prototype.bind);
79+
7880
// Used in AsyncHook and AsyncResource.
7981
const async_id_symbol = Symbol('asyncId');
8082
const trigger_async_id_symbol = Symbol('triggerAsyncId');
@@ -148,38 +150,37 @@ function emitInitNative(asyncId, type, triggerAsyncId, resource) {
148150
}
149151
}
150152

151-
152-
function emitHookFactory(symbol, name) {
153-
// Called from native. The asyncId stack handling is taken care of there
154-
// before this is called.
155-
// eslint-disable-next-line func-style
156-
const fn = function(asyncId) {
157-
active_hooks.call_depth += 1;
158-
// Use a single try/catch for all hook to avoid setting up one per
159-
// iteration.
160-
try {
161-
for (var i = 0; i < active_hooks.array.length; i++) {
162-
if (typeof active_hooks.array[i][symbol] === 'function') {
163-
active_hooks.array[i][symbol](asyncId);
164-
}
153+
// Called from native. The asyncId stack handling is taken care of there
154+
// before this is called.
155+
function emitHook(symbol, asyncId) {
156+
active_hooks.call_depth += 1;
157+
// Use a single try/catch for all hook to avoid setting up one per
158+
// iteration.
159+
try {
160+
for (var i = 0; i < active_hooks.array.length; i++) {
161+
if (typeof active_hooks.array[i][symbol] === 'function') {
162+
active_hooks.array[i][symbol](asyncId);
165163
}
166-
} catch (e) {
167-
fatalError(e);
168-
} finally {
169-
active_hooks.call_depth -= 1;
170164
}
165+
} catch (e) {
166+
fatalError(e);
167+
} finally {
168+
active_hooks.call_depth -= 1;
169+
}
171170

172-
// Hooks can only be restored if there have been no recursive hook calls.
173-
// Also the active hooks do not need to be restored if enable()/disable()
174-
// weren't called during hook execution, in which case
175-
// active_hooks.tmp_array will be null.
176-
if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
177-
restoreActiveHooks();
178-
}
179-
};
171+
// Hooks can only be restored if there have been no recursive hook calls.
172+
// Also the active hooks do not need to be restored if enable()/disable()
173+
// weren't called during hook execution, in which case
174+
// active_hooks.tmp_array will be null.
175+
if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
176+
restoreActiveHooks();
177+
}
178+
}
179+
180+
function emitHookFactory(symbol, name) {
181+
const fn = FunctionBind(emitHook, undefined, symbol);
180182

181-
// Set the name property of the anonymous function as it looks good in the
182-
// stack trace.
183+
// Set the name property of the function as it looks good in the stack trace.
183184
Object.defineProperty(fn, 'name', {
184185
value: name
185186
});
@@ -261,10 +262,10 @@ function getOrSetAsyncId(object) {
261262
// the user to safeguard this call and make sure it's zero'd out when the
262263
// constructor is complete.
263264
function getDefaultTriggerAsyncId() {
264-
let defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
265+
const defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
265266
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
266267
if (defaultTriggerAsyncId < 0)
267-
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
268+
return async_id_fields[kExecutionAsyncId];
268269
return defaultTriggerAsyncId;
269270
}
270271

@@ -393,8 +394,8 @@ function pushAsyncIds(asyncId, triggerAsyncId) {
393394

394395
// This is the equivalent of the native pop_async_ids() call.
395396
function popAsyncIds(asyncId) {
396-
if (async_hook_fields[kStackLength] === 0) return false;
397397
const stackLength = async_hook_fields[kStackLength];
398+
if (stackLength === 0) return false;
398399

399400
if (async_hook_fields[kCheck] > 0 &&
400401
async_id_fields[kExecutionAsyncId] !== asyncId) {

0 commit comments

Comments
 (0)
Please sign in to comment.