Reuse SpanBuilder within a Thread by dougqh · Pull Request #9537 · DataDog/dd-trace-java

added 2 commits

September 12, 2025 16:02
Initial performance experiment

The idea is to store a CoreSpanBuilder per thread, since usually only SpanBuilder is in use at a given time per thread -- and CoreSpanBuilder isn't thread safe

This simple change provides a giant boost in small heaps
Improving Spring petclinic throughput from -39% to -19% with 80m heap

@dougqh

@dougqh

dougqh

@dougqh

PerfectSlayer

@dougqh

@dougqh

Refactored code, so tests work regardless of Config
…ace-java into dougqh/spanbuilder-pooling

@dougqh

@dougqh

To avoid breaking any potential code that builds multiple spans from the same SpanBuilder, updated the SpanBuilder pooling approach

Introduced a new method singleSpanBuilder which can build one and only one span, this method can be used by automatic instrumentation as an optimization.

singleSpanBuilder is now used inside the startSpan convenience methods, since we know they only build and return one span.  Any automatic instrumentation using startSpan gets the optimization for free.

buildSpan maintains its original semantics, so all existing continues to work as is.
…ace-java into dougqh/spanbuilder-pooling

@dougqh

@dougqh

@dougqh

In a microbenchmark, buildSpan was performing worse than previously.

To address, that shortcoming and to clean-up the code...
Made CoreSpanBuilder abstract and introduced two child classes: MultiSpanBuilder and ReusableSingleSpanBuilder

MultiSpanBuilder is used by buildSpan
ReusableSingleSpanBuilder is used by singleSpanBuilder / startSpan (indirectly)

@dougqh

Co-authored-by: Brice Dutheil <brice.dutheil@gmail.com>
…tion/api/AgentTracer.java

Co-authored-by: Brice Dutheil <brice.dutheil@gmail.com>

@dougqh

Co-authored-by: Brice Dutheil <brice.dutheil@gmail.com>

@dougqh

@dougqh

@dougqh

Fixing assertion with blackhole test
Moved the inUse tracking to start (rather than buildSpan)

@dougqh

@dougqh

@dougqh

- exposed bug with not setting inUse in init

@dougqh

@dougqh

@dougqh

AlexeyKuznetsov-DD

Seeing if removing naming conflict with Groovy tests, fix Jacoco coverage calculation

@dougqh

@dougqh

…ace-java into dougqh/spanbuilder-pooling

@dougqh

@dougqh

@dougqh dougqh deleted the dougqh/spanbuilder-pooling branch

October 22, 2025 19:18