Skip to content

process: slight refinements to nextTick #17421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,31 @@ class NextTickQueue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}

push(v) {
const entry = { data: v, next: null };
if (this.length > 0)
if (this.tail !== null)
this.tail.next = entry;
else
this.head = entry;
this.tail = entry;
++this.length;
}

shift() {
if (this.length === 0)
if (this.head === null)
return;
const ret = this.head.data;
if (this.length === 1)
if (this.head === this.tail)
this.head = this.tail = null;
else
this.head = this.head.next;
--this.length;
return ret;
}

clear() {
this.head = null;
this.tail = null;
this.length = 0;
}
}

Expand Down Expand Up @@ -90,7 +86,7 @@ function setupNextTick() {
nextTickQueue.clear();
tickInfo[kLength] = 0;
} else {
tickInfo[kLength] = nextTickQueue.length;
tickInfo[kLength] -= tickInfo[kIndex];
}
}
tickInfo[kIndex] = 0;
Expand Down Expand Up @@ -124,8 +120,6 @@ function setupNextTick() {
}
}

// Run callbacks that have no domain.
// Using domains will cause this to be overridden.
function _tickCallback() {
do {
while (tickInfo[kIndex] < tickInfo[kLength]) {
Expand All @@ -137,7 +131,8 @@ function setupNextTick() {
// CHECK(Number.isSafeInteger(tock[trigger_async_id_symbol]))
// CHECK(tock[trigger_async_id_symbol] > 0)

emitBefore(tock[async_id_symbol], tock[trigger_async_id_symbol]);
const asyncId = tock[async_id_symbol];
emitBefore(asyncId, tock[trigger_async_id_symbol]);
// emitDestroy() places the async_id_symbol into an asynchronous queue
// that calls the destroy callback in the future. It's called before
// calling tock.callback so destroy will be called even if the callback
Expand All @@ -148,15 +143,15 @@ function setupNextTick() {
// any async hooks are enabled during the callback's execution then
// this tock's after hook will be called, but not its destroy hook.
if (async_hook_fields[kDestroy] > 0)
emitDestroy(tock[async_id_symbol]);
emitDestroy(asyncId);

const callback = tock.callback;
if (tock.args === undefined)
callback();
else
Reflect.apply(callback, undefined, tock.args);

emitAfter(tock[async_id_symbol]);
emitAfter(asyncId);

if (kMaxCallbacksPerLoop < tickInfo[kIndex])
tickDone();
Expand Down