Skip to content

Commit a159a2c

Browse files
apapirovskiMylesBorins
authored andcommitted
process: slight refinements to nextTick
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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7e38821 commit a159a2c

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

lib/internal/process/next_tick.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,31 @@ class NextTickQueue {
1414
constructor() {
1515
this.head = null;
1616
this.tail = null;
17-
this.length = 0;
1817
}
1918

2019
push(v) {
2120
const entry = { data: v, next: null };
22-
if (this.length > 0)
21+
if (this.tail !== null)
2322
this.tail.next = entry;
2423
else
2524
this.head = entry;
2625
this.tail = entry;
27-
++this.length;
2826
}
2927

3028
shift() {
31-
if (this.length === 0)
29+
if (this.head === null)
3230
return;
3331
const ret = this.head.data;
34-
if (this.length === 1)
32+
if (this.head === this.tail)
3533
this.head = this.tail = null;
3634
else
3735
this.head = this.head.next;
38-
--this.length;
3936
return ret;
4037
}
4138

4239
clear() {
4340
this.head = null;
4441
this.tail = null;
45-
this.length = 0;
4642
}
4743
}
4844

@@ -90,7 +86,7 @@ function setupNextTick() {
9086
nextTickQueue.clear();
9187
tickInfo[kLength] = 0;
9288
} else {
93-
tickInfo[kLength] = nextTickQueue.length;
89+
tickInfo[kLength] -= tickInfo[kIndex];
9490
}
9591
}
9692
tickInfo[kIndex] = 0;
@@ -124,8 +120,6 @@ function setupNextTick() {
124120
}
125121
}
126122

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

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

153148
const callback = tock.callback;
154149
if (tock.args === undefined)
155150
callback();
156151
else
157152
Reflect.apply(callback, undefined, tock.args);
158153

159-
emitAfter(tock[async_id_symbol]);
154+
emitAfter(asyncId);
160155

161156
if (kMaxCallbacksPerLoop < tickInfo[kIndex])
162157
tickDone();

0 commit comments

Comments
 (0)