Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: improve performance of nextTick #985

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 44 additions & 43 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@
callback: runMicrotasksCallback,
domain: null
});

tickInfo[kLength]++;
microtasksScheduled = true;
}
Expand All @@ -340,65 +339,67 @@
function _tickCallback() {
var callback, threw, tock;

scheduleMicrotasks();

while (tickInfo[kIndex] < tickInfo[kLength]) {
tock = nextTickQueue[tickInfo[kIndex]++];
callback = tock.callback;
threw = true;
try {
callback();
threw = false;
} finally {
if (threw)
do {
while (tickInfo[kIndex] < tickInfo[kLength]) {
tock = nextTickQueue[tickInfo[kIndex]++];
callback = tock.callback;
threw = true;
try {
callback();
threw = false;
} finally {
if (threw)
tickDone();
}
if (1e4 < tickInfo[kIndex])
tickDone();
}
if (1e4 < tickInfo[kIndex])
tickDone();
}

tickDone();
tickDone();
_runMicrotasks();
emitPendingUnhandledRejections();
} while (tickInfo[kLength] !== 0);
}

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

scheduleMicrotasks();

while (tickInfo[kIndex] < tickInfo[kLength]) {
tock = nextTickQueue[tickInfo[kIndex]++];
callback = tock.callback;
domain = tock.domain;
if (domain)
domain.enter();
threw = true;
try {
callback();
threw = false;
} finally {
if (threw)
do {
while (tickInfo[kIndex] < tickInfo[kLength]) {
tock = nextTickQueue[tickInfo[kIndex]++];
callback = tock.callback;
domain = tock.domain;
if (domain)
domain.enter();
threw = true;
try {
callback();
threw = false;
} finally {
if (threw)
tickDone();
}
if (1e4 < tickInfo[kIndex])
tickDone();
if (domain)
domain.exit();
}
if (1e4 < tickInfo[kIndex])
tickDone();
if (domain)
domain.exit();
}
tickDone();
_runMicrotasks();
emitPendingUnhandledRejections();
} while (tickInfo[kLength] !== 0);
}

tickDone();
function TickObject(c) {
this.callback = c;
this.domain = process.domain || null;
}

function nextTick(callback) {
// on the way out, don't bother. it won't get fired anyway.
if (process._exiting)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does breaking this out into a separate class really make a difference? I can't think of a good reason why that would be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, V8 does a better job creating hidden classes for objects constructed with a function constructor / can pre-allocate the expected number of properties for them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code that estimates the number of properties a function constructor will introduce.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V8 has a similar optimization for object literals but maybe it gets thrown off by the computed value. I'll take your and Trevor's word for it that it's faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis I think it has to do with accessing the global process object. Though I'm not sure why it would have affect with only the object literal implementation.

return;

var obj = {
callback: callback,
domain: process.domain || null
};

nextTickQueue.push(obj);
nextTickQueue.push(new TickObject(callback));
tickInfo[kLength]++;
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-timers-first-fire.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ setTimeout(function() {
var ms = (hr[0] * 1e3) + (hr[1] / 1e6);
var delta = ms - TIMEOUT;
console.log('timer fired in', delta);
assert.ok(delta > 0, 'Timer fired early');
assert.ok(delta > -0.5, 'Timer fired early');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, does that mean the timer indeed fires early? How can that be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. it fires ~0.05ms early on occasion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea and I've never seen that. Nothing comes to mind, no probable culprit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were there no CI runs against this? Are we sure this won't cause subtle bugs?

}, TIMEOUT);