Skip to content

Commit 89d31ee

Browse files
apapirovskiMylesBorins
authored andcommitted
timers: improvements to TimersList management
Move all the TimersList instantiation code into the constructor. Compare values to undefined & null as appropriate instead of truthy or falsy. PR-URL: #17429 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent b8141a4 commit 89d31ee

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

lib/timers.js

+24-31
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const activateImmediateCheck = process._activateImmediateCheck;
5555
delete process._activateImmediateCheck;
5656

5757
// Timeout values > TIMEOUT_MAX are set to 1.
58-
const TIMEOUT_MAX = 2147483647; // 2^31-1
58+
const TIMEOUT_MAX = 2 ** 31 - 1;
5959

6060

6161
// HOW and WHY the timers implementation works the way it does.
@@ -173,9 +173,9 @@ function insert(item, unrefed) {
173173

174174
// Use an existing list if there is one, otherwise we need to make a new one.
175175
var list = lists[msecs];
176-
if (!list) {
176+
if (list === undefined) {
177177
debug('no %d list was found in insert, creating a new one', msecs);
178-
lists[msecs] = list = createTimersList(msecs, unrefed);
178+
lists[msecs] = list = new TimersList(msecs, unrefed);
179179
}
180180

181181
if (!item[async_id_symbol] || item._destroyed) {
@@ -194,28 +194,21 @@ function insert(item, unrefed) {
194194
assert(!L.isEmpty(list)); // list is not empty
195195
}
196196

197-
function createTimersList(msecs, unrefed) {
198-
// Make a new linked list of timers, and create a TimerWrap to schedule
199-
// processing for the list.
200-
const list = new TimersList(msecs, unrefed);
201-
L.init(list);
202-
list._timer._list = list;
203-
204-
if (unrefed === true) list._timer.unref();
205-
list._timer.start(msecs);
206-
207-
list._timer[kOnTimeout] = listOnTimeout;
208-
209-
return list;
210-
}
211-
212197
function TimersList(msecs, unrefed) {
213-
this._idleNext = null; // Create the list with the linkedlist properties to
214-
this._idlePrev = null; // prevent any unnecessary hidden class changes.
215-
this._timer = new TimerWrap();
198+
this._idleNext = this; // Create the list with the linkedlist properties to
199+
this._idlePrev = this; // prevent any unnecessary hidden class changes.
216200
this._unrefed = unrefed;
217201
this.msecs = msecs;
218202
this.nextTick = false;
203+
204+
const timer = this._timer = new TimerWrap();
205+
timer._list = this;
206+
207+
if (unrefed === true)
208+
timer.unref();
209+
timer.start(msecs);
210+
211+
timer[kOnTimeout] = listOnTimeout;
219212
}
220213

221214
function listOnTimeout() {
@@ -359,7 +352,7 @@ function reuse(item) {
359352

360353
var list = refedLists[item._idleTimeout];
361354
// if empty - reuse the watcher
362-
if (list && L.isEmpty(list)) {
355+
if (list !== undefined && L.isEmpty(list)) {
363356
debug('reuse hit');
364357
list._timer.stop();
365358
delete refedLists[item._idleTimeout];
@@ -382,7 +375,7 @@ const unenroll = exports.unenroll = function(item) {
382375
}
383376

384377
var handle = reuse(item);
385-
if (handle) {
378+
if (handle !== null) {
386379
debug('unenroll: list empty');
387380
handle.close();
388381
}
@@ -610,7 +603,7 @@ Timeout.prototype.unref = function() {
610603
}
611604

612605
var handle = reuse(this);
613-
if (handle) {
606+
if (handle !== null) {
614607
handle._list = undefined;
615608
}
616609

@@ -659,7 +652,7 @@ function ImmediateList() {
659652
// Appends an item to the end of the linked list, adjusting the current tail's
660653
// previous and next pointers where applicable
661654
ImmediateList.prototype.append = function(item) {
662-
if (this.tail) {
655+
if (this.tail !== null) {
663656
this.tail._idleNext = item;
664657
item._idlePrev = this.tail;
665658
} else {
@@ -671,11 +664,11 @@ ImmediateList.prototype.append = function(item) {
671664
// Removes an item from the linked list, adjusting the pointers of adjacent
672665
// items and the linked list's head or tail pointers as necessary
673666
ImmediateList.prototype.remove = function(item) {
674-
if (item._idleNext) {
667+
if (item._idleNext !== null) {
675668
item._idleNext._idlePrev = item._idlePrev;
676669
}
677670

678-
if (item._idlePrev) {
671+
if (item._idlePrev !== null) {
679672
item._idlePrev._idleNext = item._idleNext;
680673
}
681674

@@ -701,7 +694,7 @@ function processImmediate() {
701694
// immediate callbacks are executed
702695
immediateQueue.head = immediateQueue.tail = null;
703696

704-
while (immediate) {
697+
while (immediate !== null) {
705698
domain = immediate.domain;
706699

707700
if (!immediate._onImmediate) {
@@ -722,7 +715,7 @@ function processImmediate() {
722715

723716
// If `clearImmediate(immediate)` wasn't called from the callback, use the
724717
// `immediate`'s next item
725-
if (immediate._idleNext)
718+
if (immediate._idleNext !== null)
726719
immediate = immediate._idleNext;
727720
else
728721
immediate = next;
@@ -754,11 +747,11 @@ function tryOnImmediate(immediate, oldTail) {
754747
}
755748
}
756749

757-
if (threw && immediate._idleNext) {
750+
if (threw && immediate._idleNext !== null) {
758751
// Handle any remaining on next tick, assuming we're still alive to do so.
759752
const curHead = immediateQueue.head;
760753
const next = immediate._idleNext;
761-
if (curHead) {
754+
if (curHead !== null) {
762755
curHead._idlePrev = oldTail;
763756
oldTail._idleNext = curHead;
764757
next._idlePrev = null;

0 commit comments

Comments
 (0)