Skip to content

Commit d2cc4ec

Browse files
committed
Experimenting with introducing CoreSpanBuilder reuse
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
1 parent f02c1d1 commit d2cc4ec

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ public static CoreTracerBuilder builder() {
243243

244244
private final PropagationTags.Factory propagationTagsFactory;
245245

246+
// Cache used by buildSpan
247+
private final ThreadLocal<CoreSpanBuilder> tlSpanBuilder =
248+
new ThreadLocal<CoreSpanBuilder>() {
249+
@Override
250+
protected CoreSpanBuilder initialValue() {
251+
return new CoreSpanBuilder(CoreTracer.this);
252+
}
253+
};
254+
246255
@Override
247256
public ConfigSnapshot captureTraceConfig() {
248257
return dynamicConfig.captureTraceConfig();
@@ -968,7 +977,9 @@ long getTimeWithNanoTicks(long nanoTicks) {
968977
@Override
969978
public CoreSpanBuilder buildSpan(
970979
final String instrumentationName, final CharSequence operationName) {
971-
return new CoreSpanBuilder(this, instrumentationName, operationName);
980+
CoreSpanBuilder tlSpanBuilder = this.tlSpanBuilder.get();
981+
tlSpanBuilder.reset(instrumentationName, operationName);
982+
return tlSpanBuilder;
972983
}
973984

974985
@Override
@@ -1401,10 +1412,11 @@ private static <K, V> Map<V, K> invertMap(Map<K, V> map) {
14011412

14021413
/** Spans are built using this builder */
14031414
public static class CoreSpanBuilder implements AgentTracer.SpanBuilder {
1404-
private final String instrumentationName;
1405-
private final CharSequence operationName;
14061415
private final CoreTracer tracer;
14071416

1417+
private String instrumentationName;
1418+
private CharSequence operationName;
1419+
14081420
// Builder attributes
14091421
private TagMap.Ledger tagLedger;
14101422
private long timestampMicro;
@@ -1420,13 +1432,27 @@ public static class CoreSpanBuilder implements AgentTracer.SpanBuilder {
14201432
private List<AgentSpanLink> links;
14211433
private long spanId;
14221434

1423-
CoreSpanBuilder(
1424-
final CoreTracer tracer,
1425-
final String instrumentationName,
1426-
final CharSequence operationName) {
1435+
CoreSpanBuilder(CoreTracer tracer) {
1436+
this.tracer = tracer;
1437+
}
1438+
1439+
void reset(String instrumentationName, CharSequence operationName) {
14271440
this.instrumentationName = instrumentationName;
14281441
this.operationName = operationName;
1429-
this.tracer = tracer;
1442+
1443+
if (this.tagLedger != null) this.tagLedger.reset();
1444+
this.timestampMicro = 0L;
1445+
this.parent = null;
1446+
this.serviceName = null;
1447+
this.resourceName = null;
1448+
this.errorFlag = false;
1449+
this.spanType = null;
1450+
this.ignoreScope = false;
1451+
this.builderCiVisibilityContextData = null;
1452+
this.builderRequestContextDataIast = null;
1453+
this.builderCiVisibilityContextData = null;
1454+
this.links = null;
1455+
this.spanId = 0L;
14301456
}
14311457

14321458
@Override

0 commit comments

Comments
 (0)