-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
lib: improve debuglog() performance #32260
Conversation
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); | ||
}; |
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?
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
function _onceWrap(target, type, listener) { | |
const state = { fired: false, wrapFn: undefined, target, type, listener }; | |
const wrapped = onceWrapper.bind(state); | |
wrapped.listener = listener; | |
state.wrapFn = wrapped; | |
return wrapped; | |
} |
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.
083cb02
to
de4fdd1
Compare
Results again with current master:
|
de4fdd1
to
58d7130
Compare
PR-URL: nodejs#32260 Reviewed-By: James M Snell <jasnell@gmail.com>
58d7130
to
c24b74a
Compare
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #32260 Reviewed-By: James M Snell <jasnell@gmail.com>
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