Skip to content

Commit

Permalink
timers: improvements to TimersList management
Browse files Browse the repository at this point in the history
No longer use delete to remove keys on unrefed & refed lists,
instead simply set to undefined.

Move all the TimersList instantiation code into the constructor.

Compare values to undefined & null as appropriate instead of
truthy or falsy.
  • Loading branch information
apapirovski committed Dec 7, 2017
1 parent 4503da8 commit 5ecc4a9
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const activateImmediateCheck = process._activateImmediateCheck;
delete process._activateImmediateCheck;

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


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

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

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

function createTimersList(msecs, unrefed) {
// Make a new linked list of timers, and create a TimerWrap to schedule
// processing for the list.
const list = new TimersList(msecs, unrefed);
L.init(list);
list._timer._list = list;

if (unrefed === true) list._timer.unref();
list._timer.start(msecs);

list._timer[kOnTimeout] = listOnTimeout;

return list;
}

function TimersList(msecs, unrefed) {
this._idleNext = null; // Create the list with the linkedlist properties to
this._idlePrev = null; // prevent any unnecessary hidden class changes.
this._timer = new TimerWrap();
this._idleNext = this; // Create the list with the linkedlist properties to
this._idlePrev = this; // prevent any unnecessary hidden class changes.
this._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;

const timer = this._timer = new TimerWrap();
timer._list = this;

if (unrefed === true)
timer.unref();
timer.start(msecs);

timer[kOnTimeout] = listOnTimeout;
}

function listOnTimeout() {
Expand Down Expand Up @@ -359,7 +352,7 @@ function reuse(item) {

var list = refedLists[item._idleTimeout];
// if empty - reuse the watcher
if (list && L.isEmpty(list)) {
if (list !== undefined && L.isEmpty(list)) {
debug('reuse hit');
list._timer.stop();
delete refedLists[item._idleTimeout];
Expand All @@ -382,7 +375,7 @@ const unenroll = exports.unenroll = function(item) {
}

var handle = reuse(item);
if (handle) {
if (handle !== null) {
debug('unenroll: list empty');
handle.close();
}
Expand Down Expand Up @@ -610,7 +603,7 @@ Timeout.prototype.unref = function() {
}

var handle = reuse(this);
if (handle) {
if (handle !== null) {
handle._list = undefined;
}

Expand Down

0 comments on commit 5ecc4a9

Please sign in to comment.