Skip to content

Commit

Permalink
process: slight refinements to nextTick
Browse files Browse the repository at this point in the history
Remove length prop on NextTickQueue class. We technically don't need to
keep track of the length of the queue in two places as we already have
tickInfo doing that work (between the index & the length we have enough
data for everything).

Store asyncId in a const within the _tickCallback function. Accessing
Symbol properties seems to be quite a bit more expensive than string
keys so this actually has a decent performance impact.

PR-URL: #17421
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
apapirovski authored and MylesBorins committed Dec 12, 2017
1 parent 7e38821 commit a159a2c
Showing 1 changed file with 8 additions and 13 deletions.
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

0 comments on commit a159a2c

Please sign in to comment.