-
Notifications
You must be signed in to change notification settings - Fork 7.3k
5-50% speed up setTimeout() #4193
5-50% speed up setTimeout() #4193
Conversation
This time the changes are minimalistic and come from a topic branch instead of master. |
This issue is related only to some systems though. Node's Date.now() performance gained ~15 times on my laptop after upgrading from Ubuntu 12.04 32 bit + PAE to Ubuntu 12.10 64 bit. So this patch will usually give only 5-6% performance to the setTimeout, and only in some cases it will be 1.5x gain. |
}; | ||
|
||
Timeout.prototype.unref = function() { | ||
if (!this._handle) { | ||
var delay = this._when - Date.now(); | ||
if (!this._idleStart) this._idleStart = Date.now(); | ||
var delay = this._idleStart + this._idleTimeout - Date.now(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache the result of Date.now() in a variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unref is going to be used rarely.
Also it should use actual Date.now in the "var delay = ..." statement, but it should be not set only once in the _idleStart.
So caching in this case is not needed and will be likely harmful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you mean. I'm suggesting this:
var now = Date.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
Which could be simplified further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I completely agree with you.
@AlexeyKupershtokh Simply push additional commit to your topic branch to apply @bnoordhuis' update. |
@bnoordhuis I've committed your proposed change with --author=bnoordhuis |
more details: #4225 (comment) |
@bnoordhuis is this pull request good enough for merging? what else should I do? |
Yeah, this does make a pretty big impact on
Running tests and other benchmarks now. Will land if there aren't any other problems. |
No discernible effect on http or net benchmarks. I was sort of hoping for some improvement there, since we do spend a lot of our time in But anyway, this helps a little and appears to do no harm. @AlexeyKupershtokh if you would be so kind as to sign the CLA, I will land this with pleasure. http://nodejs.org/cla.html |
Can one of the admins verify this patch? |
@isaacs done. |
@AlexeyKupershtokh You should probably set up your git config to use your github name and email address. It's a bit confusing in the commit history to see Landed on v0.10. Thanks! |
@isaacs yeah. It was one of my first PRs so I hadn't configured git by the time. Sorry for this. |
Main points are:
Benchmark results are the following:
node 0.8.12 + timers.js from 0.9.2:
node 0.8.12 + optimized timers.js from 0.9.2:
node 0.9.2 reproduces these results as well.