This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
Improve performance of event and timer interfaces #9007
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request speeds up multi-listener event callbacks,
setImmediate
,setTimeout
, andsetInterval
by differentiating between zero-, one-, and two-argument callbacks. This technique was previously only applied to single-listener event callbacks—and even they have been sped up.Current situation
The
EventEmitter#emit
code has special cases for zero-, one-, and two-argument callbacks as follows:Improvements in this pull request
On the one hand, this pull request speeds up this technique by avoiding the use of
arguments
indexing, instead adding actual function argumentsarg1
andarg2
.On the other hand, this pull request also applies the same technique to listeners with multiple callbacks, as well as the timer functions
setImmediate
,setTimeout
, andsetInterval
.Below are my results of performance tests that examine these cases (ran on a 2010 MacBook Pro).
Note how the current master branch has worsened the performance of event listeners in comparison to v0.10, and this pull request compensates for that difference. In cases where the master branch improved performance, this pull request improves it even more.
Considerations
Given the importance of callbacks in Node, delivering maximum performance for common cases is crucial. The only drawback of this pull request seems to be the increased code length; but it brings consistency since the optimization technique was previously only applied to one particular case (and slightly suboptimally).
I added each change in a different commit, so you can decide which ones to merge. All commits are independent of each other, except for the second (ca3a08d), which depends on the first (e4d4c98).
If you want these commits squashed, or applied to a different branch, please let me know and I'll do so.