-
Notifications
You must be signed in to change notification settings - Fork 277
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
Cache the result of toString in BigInteger #1228
Conversation
Hi, thanks for the contribution! Did you consider instead or creating a new type, adding the caching to Here:
And here:
|
Unfortunately, I don't think that would be a thorough enough of a fix. At the end of the day, the problems may not lie with the users of the tracing api library, but the implementors of integrations as well! There are many places where a span is cast to a DDSpan and then call getTraceId(), which isn't inherently wrong since in some contexts, since you do need the BigInteger for things like sampling. But there are quiet a few places that use the toString() method on that object, be it implicit or explicit. |
Looks like jacoco has issue with the new class's test coverage. You can add it to the ignore list here: https://github.com/DataDog/dd-trace-java/blob/master/dd-trace-ot/dd-trace-ot.gradle#L13 |
*/ | ||
public class StringCachingBigInteger extends BigInteger { | ||
|
||
private String cachedString; |
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.
Technically this isn't threadsafe, but in practice I don't think it matters.
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.
Yes, in this case, it will work, since the calculation is idempotent.
Each thread will eventually make the null -> non-null transition.
This is the same basic strategy employed by String for the hashCode calculation.
The downside is a potential for a bit of extra allocation compared to the "ideal", but that's a reasonable trade-off.
This will already save a great deal on allocation and keeping the coordination overhead down is also important.
It could be made volatile, but we'd still have the same fundamental race -- so I think this is good as is.
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.
keeping the coordination overhead down is also important.
Super agreed on this point. The overhead in a massively parallel/concurrent environment would not be worth locking this value
I'm currently testing out the new datadog profiling feature and am seeing a high number of MutableBigIntegers being allocated. Because it was pretty much exclusively due to
toString()
, I've created a simple wrapper that caches the result, which will (hopefully) reduce our overhead of logging the same trace id multiple times.