Skip to content

Commit 2365c12

Browse files
dougqhdevflow.devflow-routing-intake
andauthored
Cache Tracer Host Entry to reduce allocation in RemoteHostnameAdder (#10968)
Caching host entry to reduce allocation spotless Merge branch 'master' into dougqh/cache-tracer-host Merge branch 'master' into dougqh/cache-tracer-host Flipped equals order hostname is type String while cachedHostEntry.objectValue() returns Object By calling equals on hostname, the equals call can be statically devirtualized Previously, the call would have used type profile based devirtualization, but that incurs an extra type check that isn't really needed Admittedly, there will still be a type check in string equals Merge branch 'master' into dougqh/cache-tracer-host Merge branch 'master' into dougqh/cache-tracer-host Merge branch 'master' into dougqh/cache-tracer-host Merge branch 'master' into dougqh/cache-tracer-host Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 9fd98dd commit 2365c12

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/RemoteHostnameAdder.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,32 @@
99
public final class RemoteHostnameAdder extends TagsPostProcessor {
1010
private final Supplier<String> hostnameSupplier;
1111

12+
private TagMap.Entry cachedHostEntry = null;
13+
1214
public RemoteHostnameAdder(Supplier<String> hostnameSupplier) {
1315
this.hostnameSupplier = hostnameSupplier;
1416
}
1517

1618
@Override
1719
public void processTags(
1820
TagMap unsafeTags, DDSpanContext spanContext, AppendableSpanLinks spanLinks) {
19-
if (spanContext.getSpanId() == spanContext.getRootSpanId()) {
20-
unsafeTags.put(DDTags.TRACER_HOST, hostnameSupplier.get());
21+
if (spanContext.getSpanId() != spanContext.getRootSpanId()) {
22+
return;
23+
}
24+
25+
String hostname = hostnameSupplier.get();
26+
if (hostname == null) {
27+
return;
2128
}
29+
30+
TagMap.Entry cachedHostEntry = this.cachedHostEntry;
31+
if (cachedHostEntry != null && hostname.equals(cachedHostEntry.objectValue())) {
32+
unsafeTags.set(cachedHostEntry);
33+
return;
34+
}
35+
36+
TagMap.Entry newEntry = TagMap.Entry.create(DDTags.TRACER_HOST, hostname);
37+
unsafeTags.set(newEntry);
38+
this.cachedHostEntry = newEntry;
2239
}
2340
}

0 commit comments

Comments
 (0)