async_hooks: use common emitBefore and emitAfter · nodejs/node@9f66f19 · GitHub
Skip to content

Commit 9f66f19

Browse files
AndreasMadsenaddaleax
authored andcommitted
async_hooks: use common emitBefore and emitAfter
Timers and nextTick have special emitBefore and emitAfter functions for historic reasons. These function are not needed any more, thus the public emitBefore and emitAfter function can be used. PR-URL: #14050 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent b799498 commit 9f66f19

5 files changed

Lines changed: 28 additions & 66 deletions

File tree

lib/async_hooks.js

Lines changed: 2 additions & 3 deletions

lib/internal/process/next_tick.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ function setupNextTick() {
5656
// Two arrays that share state between C++ and JS.
5757
const { async_hook_fields, async_uid_fields } = async_wrap;
5858
// Used to change the state of the async id stack.
59-
const { pushAsyncIds, popAsyncIds } = async_wrap;
60-
// The needed emit*() functions.
6159
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
6260
// Grab the constants necessary for working with internal arrays.
63-
const { kInit, kBefore, kAfter, kDestroy, kAsyncUidCntr } =
64-
async_wrap.constants;
61+
const { kInit, kDestroy, kAsyncUidCntr } = async_wrap.constants;
6562
const { async_id_symbol, trigger_id_symbol } = async_wrap;
6663
var nextTickQueue = new NextTickQueue();
6764
var microtasksScheduled = false;
@@ -149,24 +146,6 @@ function setupNextTick() {
149146
}
150147
}
151148

152-
// TODO(trevnorris): Using std::stack of Environment::AsyncHooks::ids_stack_
153-
// is much slower here than was the Float64Array stack used in a previous
154-
// implementation. Problem is the Float64Array stack was a bit brittle.
155-
// Investigate how to harden that implementation and possibly reintroduce it.
156-
function nextTickEmitBefore(asyncId, triggerAsyncId) {
157-
if (async_hook_fields[kBefore] > 0)
158-
emitBefore(asyncId, triggerAsyncId);
159-
else
160-
pushAsyncIds(asyncId, triggerAsyncId);
161-
}
162-
163-
function nextTickEmitAfter(asyncId) {
164-
if (async_hook_fields[kAfter] > 0)
165-
emitAfter(asyncId);
166-
else
167-
popAsyncIds(asyncId);
168-
}
169-
170149
// Run callbacks that have no domain.
171150
// Using domains will cause this to be overridden.
172151
function _tickCallback() {
@@ -182,7 +161,7 @@ function setupNextTick() {
182161
// CHECK(Number.isSafeInteger(tock[trigger_id_symbol]))
183162
// CHECK(tock[trigger_id_symbol] > 0)
184163

185-
nextTickEmitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
164+
emitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
186165
// emitDestroy() places the async_id_symbol into an asynchronous queue
187166
// that calls the destroy callback in the future. It's called before
188167
// calling tock.callback so destroy will be called even if the callback
@@ -200,7 +179,7 @@ function setupNextTick() {
200179
// performance hit associated with using `fn.apply()`
201180
_combinedTickCallback(args, callback);
202181

203-
nextTickEmitAfter(tock[async_id_symbol]);
182+
emitAfter(tock[async_id_symbol]);
204183

205184
if (kMaxCallbacksPerLoop < tickInfo[kIndex])
206185
tickDone();
@@ -227,7 +206,7 @@ function setupNextTick() {
227206
// CHECK(Number.isSafeInteger(tock[trigger_id_symbol]))
228207
// CHECK(tock[trigger_id_symbol] > 0)
229208

230-
nextTickEmitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
209+
emitBefore(tock[async_id_symbol], tock[trigger_id_symbol]);
231210
// TODO(trevnorris): See comment in _tickCallback() as to why this
232211
// isn't a good solution.
233212
if (async_hook_fields[kDestroy] > 0)
@@ -238,7 +217,7 @@ function setupNextTick() {
238217
// performance hit associated with using `fn.apply()`
239218
_combinedTickCallback(args, callback);
240219

241-
nextTickEmitAfter(tock[async_id_symbol]);
220+
emitAfter(tock[async_id_symbol]);
242221

243222
if (kMaxCallbacksPerLoop < tickInfo[kIndex])
244223
tickDone();

lib/timers.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ const kOnTimeout = TimerWrap.kOnTimeout | 0;
3434
const initTriggerId = async_hooks.initTriggerId;
3535
// Two arrays that share state between C++ and JS.
3636
const { async_hook_fields, async_uid_fields } = async_wrap;
37-
// Used to change the state of the async id stack.
38-
const { pushAsyncIds, popAsyncIds } = async_wrap;
3937
// The needed emit*() functions.
4038
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
4139
// Grab the constants necessary for working with internal arrays.
42-
const { kInit, kBefore, kAfter, kDestroy, kAsyncUidCntr } =
43-
async_wrap.constants;
40+
const { kInit, kDestroy, kAsyncUidCntr } = async_wrap.constants;
4441
// Symbols for storing async id state.
4542
const async_id_symbol = Symbol('asyncId');
4643
const trigger_id_symbol = Symbol('triggerAsyncId');
@@ -149,22 +146,6 @@ exports._unrefActive = function(item) {
149146
};
150147

151148

152-
function timerEmitBefore(asyncId, triggerAsyncId) {
153-
if (async_hook_fields[kBefore] > 0)
154-
emitBefore(asyncId, triggerAsyncId);
155-
else
156-
pushAsyncIds(asyncId, triggerAsyncId);
157-
}
158-
159-
160-
function timerEmitAfter(asyncId) {
161-
if (async_hook_fields[kAfter] > 0)
162-
emitAfter(asyncId);
163-
else
164-
popAsyncIds(asyncId);
165-
}
166-
167-
168149
// The underlying logic for scheduling or re-scheduling a timer.
169150
//
170151
// Appends a timer onto the end of an existing timers list, or creates a new
@@ -318,14 +299,14 @@ function tryOnTimeout(timer, list) {
318299
timer[async_id_symbol] : null;
319300
var threw = true;
320301
if (timerAsyncId !== null)
321-
timerEmitBefore(timerAsyncId, timer[trigger_id_symbol]);
302+
emitBefore(timerAsyncId, timer[trigger_id_symbol]);
322303
try {
323304
ontimeout(timer);
324305
threw = false;
325306
} finally {
326307
if (timerAsyncId !== null) {
327308
if (!threw)
328-
timerEmitAfter(timerAsyncId);
309+
emitAfter(timerAsyncId);
329310
if (!timer._repeat && async_hook_fields[kDestroy] > 0 &&
330311
!timer._destroyed) {
331312
emitDestroy(timerAsyncId);
@@ -756,7 +737,7 @@ function processImmediate() {
756737
// 4.7) what is in this smaller function.
757738
function tryOnImmediate(immediate, oldTail) {
758739
var threw = true;
759-
timerEmitBefore(immediate[async_id_symbol], immediate[trigger_id_symbol]);
740+
emitBefore(immediate[async_id_symbol], immediate[trigger_id_symbol]);
760741
try {
761742
// make the actual call outside the try/catch to allow it to be optimized
762743
runCallback(immediate);
@@ -765,7 +746,7 @@ function tryOnImmediate(immediate, oldTail) {
765746
// clearImmediate checks _callback === null for kDestroy hooks.
766747
immediate._callback = null;
767748
if (!threw)
768-
timerEmitAfter(immediate[async_id_symbol]);
749+
emitAfter(immediate[async_id_symbol]);
769750
if (async_hook_fields[kDestroy] > 0 && !immediate._destroyed) {
770751
emitDestroy(immediate[async_id_symbol]);
771752
immediate._destroyed = true;

test/async-hooks/test-callback-error.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,33 @@ switch (arg) {
1212
oninit: common.mustCall(() => { throw new Error(arg); })
1313
}).enable();
1414
async_hooks.emitInit(
15-
async_hooks.executionAsyncId(),
15+
async_hooks.newUid(),
1616
`${arg}_type`,
17-
async_hooks.triggerAsyncId()
17+
async_hooks.executionAsyncId()
1818
);
1919
return;
2020

2121
case 'test_callback':
2222
initHooks({
2323
onbefore: common.mustCall(() => { throw new Error(arg); })
2424
}).enable();
25+
const newAsyncId = async_hooks.newUid();
2526
async_hooks.emitInit(
26-
async_hooks.executionAsyncId(),
27+
newAsyncId,
2728
`${arg}_type`,
28-
async_hooks.triggerAsyncId()
29+
async_hooks.executionAsyncId()
2930
);
30-
async_hooks.emitBefore(async_hooks.executionAsyncId());
31+
async_hooks.emitBefore(newAsyncId, async_hooks.executionAsyncId());
3132
return;
3233

3334
case 'test_callback_abort':
3435
initHooks({
3536
oninit: common.mustCall(() => { throw new Error(arg); })
3637
}).enable();
3738
async_hooks.emitInit(
38-
async_hooks.executionAsyncId(),
39+
async_hooks.newUid(),
3940
`${arg}_type`,
40-
async_hooks.triggerAsyncId()
41+
async_hooks.executionAsyncId()
4142
);
4243
return;
4344
}

test/async-hooks/test-emit-before-after.js

Lines changed: 8 additions & 6 deletions

0 commit comments

Comments
 (0)