Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
lib: improve debuglog() performance#32260
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
mscdex commented Mar 14, 2020 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
nodejs-github-bot commented Mar 14, 2020
lib/internal/util/debuglog.js Outdated
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.
What about this?
| functiondebuglog(set,cb){ | |
| letdebug=(...args)=>{ | |
| // Only invokes debuglogImpl() when the debug function is | |
| // called for the first time. | |
| debug=debuglogImpl(set); | |
| if(typeofcb==='function') | |
| cb(debug); | |
| debug(...args); | |
| }; | |
| return(...args)=>{ | |
| debug(...args); | |
| }; | |
| functiondebuglog(set){ | |
| letdebug=(...args)=>{ | |
| // Only invokes debuglogImpl() when the debug function is | |
| // called for the first time. | |
| debug=debuglogImpl(set); | |
| debug(...args); | |
| }; | |
| conststate={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
| function_onceWrap(target,type,listener){ | |
| conststate={fired: false,wrapFn: undefined, target, type, listener }; | |
| constwrapped=onceWrapper.bind(state); | |
| wrapped.listener=listener; | |
| state.wrapFn=wrapped; | |
| returnwrapped; | |
| } |
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.
because I don't think let debug = fn('xxx', v => debug = v) is a good code style
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.
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.
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.
Thanks, I understand
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.
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.
mscdex commented May 30, 2020
Results again with current master: |
nodejs-github-bot commented May 30, 2020
PR-URL: nodejs#32260 Reviewed-By: James M Snell <[email protected]>
PR-URL: #32260 Reviewed-By: James M Snell <[email protected]>
PR-URL: #32260 Reviewed-By: James M Snell <[email protected]>
PR-URL: #32260 Reviewed-By: James M Snell <[email protected]>
PR-URL: #32260 Reviewed-By: James M Snell <[email protected]>
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