lib: improve debuglog() performance#32260
Merged
mscdex merged 1 commit intonodejs:masterfrom May 30, 2020
Merged
Conversation
Collaborator
himself65
reviewed
Mar 14, 2020
lib/internal/util/debuglog.js
Outdated
Comment on lines
+63
to
74
Member
There was a problem hiding this comment.
What about this?
Suggested change
| function debuglog(set, cb) { | |
| let debug = (...args) => { | |
| // Only invokes debuglogImpl() when the debug function is | |
| // called for the first time. | |
| debug = debuglogImpl(set); | |
| if (typeof cb === 'function') | |
| cb(debug); | |
| debug(...args); | |
| }; | |
| return (...args) => { | |
| debug(...args); | |
| }; | |
| function debuglog(set) { | |
| let debug = (...args) => { | |
| // Only invokes debuglogImpl() when the debug function is | |
| // called for the first time. | |
| debug = debuglogImpl(set); | |
| debug(...args); | |
| }; | |
| const state = { called: false }; | |
| return ((...args) => { | |
| if(!this.called) { | |
| debug(...args); | |
| this.called = true; | |
| } else { | |
| debug(...args); | |
| } | |
| }).bind(state); |
just like
Lines 426 to 432 in ec204d8
Member
There was a problem hiding this comment.
because I don't think let debug = fn('xxx', v => debug = v) is a good code style
Contributor
Author
There was a problem hiding this comment.
The suggested change is basically what the code was doing before. Having a callback is the only way to be able to avoid the wrapper function and utilizing it provides a further, noticeable performance improvement.
Member
There was a problem hiding this comment.
Fairly certain this is because of the way V8 optimizes but that's only a guess. It's interesting that it's such a big perf difference.
jasnell
approved these changes
Mar 16, 2020
4 tasks
083cb02 to
de4fdd1
Compare
Contributor
Author
|
Results again with current master: |
Collaborator
de4fdd1 to
58d7130
Compare
PR-URL: nodejs#32260 Reviewed-By: James M Snell <jasnell@gmail.com>
58d7130 to
c24b74a
Compare
This was referenced Jun 12, 2020
codebytere
pushed a commit
that referenced
this pull request
Jun 27, 2020
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
Merged
codebytere
pushed a commit
that referenced
this pull request
Jun 30, 2020
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
addaleax
pushed a commit
that referenced
this pull request
Sep 27, 2020
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
addaleax
pushed a commit
that referenced
this pull request
Sep 28, 2020
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Example benchmark results:
Improvements come from optimizing the existing function returned by
debuglog()but also by adding an optional callback that can be used to get a reference to the actual underlying debug output function to avoid calling into the unnecessary wrapper function.Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes