Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6810d3e

Browse files
committedMar 3, 2015
node: improve performance of nextTick
Couple micro optimizations to improve performance of process.nextTick(). Removes ~60ns of execution time. Also added small threshold to test that allows timer to fire early on the order if microseconds.
1 parent 08e89b1 commit 6810d3e

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed
 

‎src/node.js

+44-43
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@
321321
callback: runMicrotasksCallback,
322322
domain: null
323323
});
324-
325324
tickInfo[kLength]++;
326325
microtasksScheduled = true;
327326
}
@@ -340,65 +339,67 @@
340339
function _tickCallback() {
341340
var callback, threw, tock;
342341

343-
scheduleMicrotasks();
344-
345-
while (tickInfo[kIndex] < tickInfo[kLength]) {
346-
tock = nextTickQueue[tickInfo[kIndex]++];
347-
callback = tock.callback;
348-
threw = true;
349-
try {
350-
callback();
351-
threw = false;
352-
} finally {
353-
if (threw)
342+
do {
343+
while (tickInfo[kIndex] < tickInfo[kLength]) {
344+
tock = nextTickQueue[tickInfo[kIndex]++];
345+
callback = tock.callback;
346+
threw = true;
347+
try {
348+
callback();
349+
threw = false;
350+
} finally {
351+
if (threw)
352+
tickDone();
353+
}
354+
if (1e4 < tickInfo[kIndex])
354355
tickDone();
355356
}
356-
if (1e4 < tickInfo[kIndex])
357-
tickDone();
358-
}
359-
360-
tickDone();
357+
tickDone();
358+
_runMicrotasks();
359+
emitPendingUnhandledRejections();
360+
} while (tickInfo[kLength] !== 0);
361361
}
362362

363363
function _tickDomainCallback() {
364364
var callback, domain, threw, tock;
365365

366-
scheduleMicrotasks();
367-
368-
while (tickInfo[kIndex] < tickInfo[kLength]) {
369-
tock = nextTickQueue[tickInfo[kIndex]++];
370-
callback = tock.callback;
371-
domain = tock.domain;
372-
if (domain)
373-
domain.enter();
374-
threw = true;
375-
try {
376-
callback();
377-
threw = false;
378-
} finally {
379-
if (threw)
366+
do {
367+
while (tickInfo[kIndex] < tickInfo[kLength]) {
368+
tock = nextTickQueue[tickInfo[kIndex]++];
369+
callback = tock.callback;
370+
domain = tock.domain;
371+
if (domain)
372+
domain.enter();
373+
threw = true;
374+
try {
375+
callback();
376+
threw = false;
377+
} finally {
378+
if (threw)
379+
tickDone();
380+
}
381+
if (1e4 < tickInfo[kIndex])
380382
tickDone();
383+
if (domain)
384+
domain.exit();
381385
}
382-
if (1e4 < tickInfo[kIndex])
383-
tickDone();
384-
if (domain)
385-
domain.exit();
386-
}
386+
tickDone();
387+
_runMicrotasks();
388+
emitPendingUnhandledRejections();
389+
} while (tickInfo[kLength] !== 0);
390+
}
387391

388-
tickDone();
392+
function TickObject(c) {
393+
this.callback = c;
394+
this.domain = process.domain || null;
389395
}
390396

391397
function nextTick(callback) {
392398
// on the way out, don't bother. it won't get fired anyway.
393399
if (process._exiting)
394400
return;
395401

396-
var obj = {
397-
callback: callback,
398-
domain: process.domain || null
399-
};
400-
401-
nextTickQueue.push(obj);
402+
nextTickQueue.push(new TickObject(callback));
402403
tickInfo[kLength]++;
403404
}
404405

‎test/parallel/test-timers-first-fire.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ setTimeout(function() {
88
var ms = (hr[0] * 1e3) + (hr[1] / 1e6);
99
var delta = ms - TIMEOUT;
1010
console.log('timer fired in', delta);
11-
assert.ok(delta > 0, 'Timer fired early');
11+
assert.ok(delta > -0.5, 'Timer fired early');
1212
}, TIMEOUT);

0 commit comments

Comments
 (0)
Please sign in to comment.