Skip to content

Commit 19382d9

Browse files
committed
Changing traceSource to volatile rather than an AtomicInteger
This avoids allocated an AtomicInteger for every PTagsFactory. The concurrency needs are met through an AtomicFieldUpdater. In benchmarks, the AtomicFieldUpdater appears the same or better with lower memory overhead.
1 parent 7f66c79 commit 19382d9

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/propagation/ptags/PTagsFactory.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Objects;
21-
import java.util.concurrent.atomic.AtomicInteger;
21+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
2222
import javax.annotation.Nonnull;
2323

2424
public class PTagsFactory implements PropagationTags.Factory {
@@ -84,7 +84,10 @@ static class PTags extends PropagationTags {
8484
// extracted decision maker tag for easier updates
8585
private volatile TagValue decisionMakerTagValue;
8686

87-
private final AtomicInteger traceSource;
87+
private static final AtomicIntegerFieldUpdater<PTags> AFU_TRACE_SOURCE =
88+
AtomicIntegerFieldUpdater.newUpdater(PTags.class, "traceSource");
89+
90+
private volatile int traceSource;
8891
private volatile String debugPropagation;
8992

9093
// xDatadogTagsSize of the tagPairs, does not include the decision maker tag
@@ -152,7 +155,7 @@ public PTags(
152155
this.tagPairs = tagPairs;
153156
this.canChangeDecisionMaker = decisionMakerTagValue == null;
154157
this.decisionMakerTagValue = decisionMakerTagValue;
155-
this.traceSource = new AtomicInteger(traceSource);
158+
this.traceSource = traceSource;
156159
this.samplingPriority = samplingPriority;
157160
this.origin = origin;
158161
this.lastParentId = lastParentId;
@@ -230,7 +233,8 @@ private void doUpdateTraceSamplingPriority(int samplingPriority, int samplingMec
230233

231234
@Override
232235
public void addTraceSource(final int product) {
233-
traceSource.updateAndGet(
236+
AFU_TRACE_SOURCE.updateAndGet(
237+
this,
234238
currentValue -> {
235239
// If the product is already marked, return the same value (no change)
236240
if (ProductTraceSource.isProductMarked(currentValue, product)) {
@@ -248,7 +252,7 @@ public void addTraceSource(final int product) {
248252

249253
@Override
250254
public int getTraceSource() {
251-
return traceSource.get();
255+
return traceSource;
252256
}
253257

254258
@Override
@@ -386,7 +390,7 @@ int getXDatadogTagsSize() {
386390
size = PTagsCodec.calcXDatadogTagsSize(getTagPairs());
387391
size = PTagsCodec.calcXDatadogTagsSize(size, DECISION_MAKER_TAG, decisionMakerTagValue);
388392
size = PTagsCodec.calcXDatadogTagsSize(size, TRACE_ID_TAG, traceIdHighOrderBitsHexTagValue);
389-
int currentProductTraceSource = traceSource.get();
393+
int currentProductTraceSource = traceSource;
390394
if (currentProductTraceSource != ProductTraceSource.UNSET) {
391395
size =
392396
PTagsCodec.calcXDatadogTagsSize(

0 commit comments

Comments
 (0)