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. PR-URL: https://github.com/iojs/io.js/pull/985 Reviewed-By: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
This commit is contained in:
parent
9741291fe9
commit
e0835c9cda
26
src/node.js
26
src/node.js
@ -340,8 +340,7 @@
|
|||||||
function _tickCallback() {
|
function _tickCallback() {
|
||||||
var callback, threw, tock;
|
var callback, threw, tock;
|
||||||
|
|
||||||
scheduleMicrotasks();
|
do {
|
||||||
|
|
||||||
while (tickInfo[kIndex] < tickInfo[kLength]) {
|
while (tickInfo[kIndex] < tickInfo[kLength]) {
|
||||||
tock = nextTickQueue[tickInfo[kIndex]++];
|
tock = nextTickQueue[tickInfo[kIndex]++];
|
||||||
callback = tock.callback;
|
callback = tock.callback;
|
||||||
@ -356,15 +355,16 @@
|
|||||||
if (1e4 < tickInfo[kIndex])
|
if (1e4 < tickInfo[kIndex])
|
||||||
tickDone();
|
tickDone();
|
||||||
}
|
}
|
||||||
|
|
||||||
tickDone();
|
tickDone();
|
||||||
|
_runMicrotasks();
|
||||||
|
emitPendingUnhandledRejections();
|
||||||
|
} while (tickInfo[kLength] !== 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _tickDomainCallback() {
|
function _tickDomainCallback() {
|
||||||
var callback, domain, threw, tock;
|
var callback, domain, threw, tock;
|
||||||
|
|
||||||
scheduleMicrotasks();
|
do {
|
||||||
|
|
||||||
while (tickInfo[kIndex] < tickInfo[kLength]) {
|
while (tickInfo[kIndex] < tickInfo[kLength]) {
|
||||||
tock = nextTickQueue[tickInfo[kIndex]++];
|
tock = nextTickQueue[tickInfo[kIndex]++];
|
||||||
callback = tock.callback;
|
callback = tock.callback;
|
||||||
@ -384,8 +384,15 @@
|
|||||||
if (domain)
|
if (domain)
|
||||||
domain.exit();
|
domain.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
tickDone();
|
tickDone();
|
||||||
|
_runMicrotasks();
|
||||||
|
emitPendingUnhandledRejections();
|
||||||
|
} while (tickInfo[kLength] !== 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TickObject(c) {
|
||||||
|
this.callback = c;
|
||||||
|
this.domain = process.domain || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextTick(callback) {
|
function nextTick(callback) {
|
||||||
@ -393,12 +400,7 @@
|
|||||||
if (process._exiting)
|
if (process._exiting)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var obj = {
|
nextTickQueue.push(new TickObject(callback));
|
||||||
callback: callback,
|
|
||||||
domain: process.domain || null
|
|
||||||
};
|
|
||||||
|
|
||||||
nextTickQueue.push(obj);
|
|
||||||
tickInfo[kLength]++;
|
tickInfo[kLength]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,5 +8,5 @@ setTimeout(function() {
|
|||||||
var ms = (hr[0] * 1e3) + (hr[1] / 1e6);
|
var ms = (hr[0] * 1e3) + (hr[1] / 1e6);
|
||||||
var delta = ms - TIMEOUT;
|
var delta = ms - TIMEOUT;
|
||||||
console.log('timer fired in', delta);
|
console.log('timer fired in', delta);
|
||||||
assert.ok(delta > 0, 'Timer fired early');
|
assert.ok(delta > -0.5, 'Timer fired early');
|
||||||
}, TIMEOUT);
|
}, TIMEOUT);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user