Skip to content

Commit

Permalink
timers: optimize linkedlist
Browse files Browse the repository at this point in the history
Now uses a new L.create() factory to create access-optimized linkedlist
objects.

PR-URL: #6436
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
Andras authored and Fishrock123 committed Jun 29, 2016
1 parent f8d3f6f commit 6b0f86a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
18 changes: 16 additions & 2 deletions lib/internal/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ function init(list) {
}
exports.init = init;

// create a new linked list
function create() {
var list = { _idleNext: null, _idlePrev: null };
init(list);
return list;
}
exports.create = create;

// show the most idle item
function peek(list) {
Expand Down Expand Up @@ -42,10 +49,17 @@ exports.remove = remove;

// remove a item from its list and place at the end.
function append(list, item) {
remove(item);
if (item._idleNext || item._idlePrev) {
remove(item);
}

// items are linked with _idleNext -> (older) and _idlePrev -> (newer)
// TODO: swap the linkage to match the intuitive older items at "prev"
item._idleNext = list._idleNext;
list._idleNext._idlePrev = item;
item._idlePrev = list;

// the list _idleNext points to tail (newest) and _idlePrev to head (oldest)
list._idleNext._idlePrev = item;
list._idleNext = item;
}
exports.append = append;
Expand Down
6 changes: 2 additions & 4 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,14 @@ Timeout.prototype.close = function() {
};


var immediateQueue = {};
L.init(immediateQueue);
var immediateQueue = L.create();


function processImmediate() {
var queue = immediateQueue;
var domain, immediate;

immediateQueue = {};
L.init(immediateQueue);
immediateQueue = L.create();

while (L.isEmpty(queue) === false) {
immediate = L.shift(queue);
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-timers-linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ assert.equal(C, L.shift(list));
// list
assert.ok(L.isEmpty(list));

var list2 = L.create();
var list3 = L.create();
assert.ok(L.isEmpty(list2));
assert.ok(L.isEmpty(list3));
assert.ok(list2 != list3);

0 comments on commit 6b0f86a

Please sign in to comment.