-
Notifications
You must be signed in to change notification settings - Fork 405
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
Performance: By using const over static, improve noop span creation performance another 10% #1270
Conversation
Codecov ReportPatch coverage is
📢 Thoughts on this report? Let us know!. |
de72f74
to
ee342ac
Compare
/// Create an invalid empty span context | ||
pub fn empty_context() -> Self { | ||
SpanContext::new( |
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.
There are a lot of places using empty_context()
that can be replaced with SpanContext::NONE
. I haven't changed them in this PR, but my plan would be to change them all in a future PR and then remove the empty_context()
function. Anyone see any problems with that approach?
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.
Nice!
@@ -20,6 +20,14 @@ use thiserror::Error; | |||
pub struct TraceFlags(u8); | |||
|
|||
impl TraceFlags { | |||
/// Trace flags with the `sampled` flag set to `0`. |
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.
Style suggestion: I usually like to put const
s near the bottom instead of near the top, since they're usually relatively simple and don't change often.
- Changed NOOP_SPAN from a lazy static to a const - Remove branch in build_with_context for the noop tracer. The current context's span() method already returns &NOOP_SPAN in the case when no active span is present, which already has the right span context value to use. - Changed other constituents of span context to use const default values too.
ee342ac
to
065537f
Compare
Just to understand more, the performance improvement here is for the first call to |
Good question. It's not so much about optimizing the first call which accesses a lazy static (to save the initialization), its more about optimizing every call to the lazy static which has to check that that initialization has been done before proceeding. When it's a constant, there is nothing to initialize, so there is no need for a call to check that it has been initialized. The generated assembly for this sample code should make it clearer.
|
Oh, and I should have set the optimization flag for the compiler for release, above. When I do that, the results are even more obvious. :)
vs
|
Thanks, the assembly makes it much clear. |
Improves performance for span creation when the SDK is not configured another 10%.
(Depends on #1268 to go first.)
Changes
build_with_context
for the noop tracer. the current context'sspan()
method already returns &NOOP_SPAN in the case when no active span is present, which already has the right span context value to use.static
to aconst
const
default values too.Merge requirement checklist
CHANGELOG.md
files updated for non-trivial, user-facing changes