[SDK] Tracer::StartSpan benchmark and optimizations#4248
Conversation
…n span start options changed
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4248 +/- ##
==========================================
- Coverage 81.13% 81.12% -0.00%
==========================================
Files 444 444
Lines 18848 18861 +13
==========================================
+ Hits 15291 15300 +9
- Misses 3557 3561 +4
🚀 New features to boost your workflow:
|
…te the NonRecordingSpan. Fix iwyu warnings. Cleanup
|
|
||
| // clang-format off | ||
| // | ||
| // ~/build/sdk/test/trace/tracer_benchmark --benchmark_repetitions=5 --benchmark_display_aggregates_only=true |
There was a problem hiding this comment.
The optimizations in this PR result in a 11-23% reduction in latency for StartSpan compared to main.
| Benchmark | Baseline (mean ± stddev) | Optimized (mean ± stddev) | Difference % |
|---|---|---|---|
BM_StartSpanTracerDisabled |
6.85 ± 0.025 ns | 5.24 ± 0.046 ns | -23.5% |
BM_StartSpan |
187 ± 1.22 ns | 152 ± 2.74 ns | -18.7% |
BM_StartSpanWithScope |
255 ± 1.09 ns | 218 ± 1.75 ns | -14.5% |
BM_StartSpanWithImplicitParent |
172 ± 0.45 ns | 153 ± 0.80 ns | -11.0% |
BM_StartSpanWithExplicitParentContext |
173 ± 0.34 ns | 154 ± 0.90 ns | -11.0% |
BM_StartSpanWithExplicitRootContext |
187 ± 0.68 ns | 148 ± 0.69 ns | -20.9% |
| // BM_TraceIdRatioBasedSamplerShouldSample_median 4.00 ns 4.00 ns 5 | ||
| // BM_TraceIdRatioBasedSamplerShouldSample_stddev 0.032 ns 0.032 ns 5 | ||
| // BM_TraceIdRatioBasedSamplerShouldSample_cv 0.80 % 0.80 % 5 | ||
| // BM_SpanCreation_mean 205 ns 205 ns 5 |
There was a problem hiding this comment.
Comparison with main and this PR on span sampler benchmark. The NoopSpanCreation case is where the tracer is enabled but the sampler decides to not sample the span (AlwaysOffSampler).
| Benchmark | Baseline (mean ± stddev) | Optimized (mean ± stddev) | Difference % |
|---|---|---|---|
BM_SpanCreation |
235 ± 0.62 ns | 205 ± 1.00 ns | -12.8% |
BM_NoopSpanCreation |
59.3 ± 0.42 ns | 27.7 ± 0.14 ns | -53.3% |
…for more consistent results
…r call of GetSpanContext by removing the call to HasKey since it gets a context value anyways
| inline SpanContext GetSpanContext(const context::Context &context) noexcept | ||
| { | ||
| if (!context.HasKey(kSpanKey)) | ||
| const context::ContextValue context_value = context.GetValue(kSpanKey); |
There was a problem hiding this comment.
removed context.HasKey call since it just calls context.GetValue(key) and checks for the nostd::monostate. The span key lookup and context value copy can be done once.
| // parent of the currently active Span (if any) in the current context. | ||
| // | ||
| nostd::variant<SpanContext, context::Context> parent = SpanContext::GetInvalid(); | ||
| nostd::variant<SpanContext, context::Context> parent = context::Context{}; |
There was a problem hiding this comment.
For most callers the value of parent is never changed. For callers that set a parent the initial value is often default constructed then replaced. To keep this change ABI safe we don't add a nostd::monostate (which is really what we want) and instead construct the cheapest object by default. The SpanContext is a complex object with a shared_ptr<TraceState> that is initialized to a global TraceState default object. The full Context is simpler and only has a shared_ptr<DataList> member that is default initialized to null.
…ibutes from the sampler. cleanup
Fixes # (issue)
The
Tracer::StartSpanfunction currently performs some unnecessary dynamic memory allocation (allocating a SpanContext and potentially a DefaultSpan) and copying of global static shared pointers by constructing temporary span contexts (which call TraceState::GetDefault()).This latency can be removed without breaking changes. The added benchmark showed a latency reduction of ~11-23% for the range of span creation scenarios (tracer disabled, span with no parent, implicit parent, and explicit parent).
Changes
DefaultSpanas a non-recording span with a stack allocated SpanContextFor significant contributions please make sure you have completed the following items:
CHANGELOG.mdupdated for non-trivial changes