Skip to content

Commit 04355ef

Browse files
apapirovskiBridgeAR
authored andcommitted
async_hooks: minor cleanup and improvements
Cleanup some code and make the emit hooks very slightly faster. PR-URL: #27034 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b2bb6c2 commit 04355ef

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

lib/internal/async_hooks.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
7777
kCheck, kExecutionAsyncId, kAsyncIdCounter, kTriggerAsyncId,
7878
kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
7979

80+
const FunctionBind = Function.call.bind(Function.prototype.bind);
81+
8082
// Used in AsyncHook and AsyncResource.
8183
const async_id_symbol = Symbol('asyncId');
8284
const trigger_async_id_symbol = Symbol('triggerAsyncId');
@@ -151,38 +153,37 @@ function emitInitNative(asyncId, type, triggerAsyncId, resource) {
151153
}
152154
}
153155

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

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

184-
// Set the name property of the anonymous function as it looks good in the
185-
// stack trace.
186+
// Set the name property of the function as it looks good in the stack trace.
186187
Object.defineProperty(fn, 'name', {
187188
value: name
188189
});
@@ -264,10 +265,10 @@ function getOrSetAsyncId(object) {
264265
// the user to safeguard this call and make sure it's zero'd out when the
265266
// constructor is complete.
266267
function getDefaultTriggerAsyncId() {
267-
let defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
268+
const defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
268269
// If defaultTriggerAsyncId isn't set, use the executionAsyncId
269270
if (defaultTriggerAsyncId < 0)
270-
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
271+
return async_id_fields[kExecutionAsyncId];
271272
return defaultTriggerAsyncId;
272273
}
273274

@@ -396,8 +397,8 @@ function pushAsyncIds(asyncId, triggerAsyncId) {
396397

397398
// This is the equivalent of the native pop_async_ids() call.
398399
function popAsyncIds(asyncId) {
399-
if (async_hook_fields[kStackLength] === 0) return false;
400400
const stackLength = async_hook_fields[kStackLength];
401+
if (stackLength === 0) return false;
401402

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

0 commit comments

Comments
 (0)