-
Notifications
You must be signed in to change notification settings - Fork 330
Cache Tracer Host Entry to reduce allocation in RemoteHostnameAdder #10968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f3d5093
29f5205
d8d553e
dd4a4c7
e452935
722fd7a
3d18d1d
6831f69
9e331ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,15 +9,32 @@ | |
| public final class RemoteHostnameAdder extends TagsPostProcessor { | ||
| private final Supplier<String> hostnameSupplier; | ||
|
|
||
| private TagMap.Entry cachedHostEntry = null; | ||
|
|
||
| public RemoteHostnameAdder(Supplier<String> hostnameSupplier) { | ||
| this.hostnameSupplier = hostnameSupplier; | ||
| } | ||
|
|
||
| @Override | ||
| public void processTags( | ||
| TagMap unsafeTags, DDSpanContext spanContext, AppendableSpanLinks spanLinks) { | ||
| if (spanContext.getSpanId() == spanContext.getRootSpanId()) { | ||
| unsafeTags.put(DDTags.TRACER_HOST, hostnameSupplier.get()); | ||
| if (spanContext.getSpanId() != spanContext.getRootSpanId()) { | ||
| return; | ||
| } | ||
|
|
||
| String hostname = hostnameSupplier.get(); | ||
| if (hostname == null) { | ||
| return; | ||
| } | ||
|
|
||
| TagMap.Entry cachedHostEntry = this.cachedHostEntry; | ||
| if (cachedHostEntry != null && hostname.equals(cachedHostEntry.objectValue())) { | ||
| unsafeTags.set(cachedHostEntry); | ||
| return; | ||
| } | ||
|
Comment on lines
+30
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, is it better to check to cache before checking if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. My interpretation of the code is that a null return from hostNameSupplier isn't really expected, so checking the cache first probably does make sense.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see now. I was being conservative in my approach. I wasn't assuming that hostnameSupplier.get() always produces the same value, so I have to call it anyway to check if the cached value matches the current value. I suppose as written, I could let a null hostName flow into cachedHostEntry.objectValue().equals. That would allow for static devirtualization rather than profiled based devirtualization, but I have to keep the null check first.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation, makes sense to me! |
||
|
|
||
| TagMap.Entry newEntry = TagMap.Entry.create(DDTags.TRACER_HOST, hostname); | ||
| unsafeTags.set(newEntry); | ||
| this.cachedHostEntry = newEntry; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is subtle change in behavior from before.
TagMap.put would have failed with an exception when given null.
I've decided to just ignore hostnameSupplier.get returning null instead.