Skip to content
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

Merged
merged 4 commits into from
Feb 21, 2020
Merged

Cache the result of toString in BigInteger #1228

merged 4 commits into from
Feb 21, 2020

Conversation

aarya123
Copy link
Contributor

@aarya123 aarya123 commented Feb 18, 2020

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.

@aarya123 aarya123 requested a review from a team as a code owner February 18, 2020 23:10
@devinsba
Copy link
Contributor

Hi, thanks for the contribution!

Did you consider instead or creating a new type, adding the caching to DDSpanContext directly?

Here:

And here:

@aarya123
Copy link
Contributor Author

aarya123 commented Feb 19, 2020

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.
The type that the TraceId and SpanId uses is an implementation detail and it seems more encompassing to me to make the change there as opposed to the interfaces that OpenTracing itself makes available to the users in order to protect contributors and users of this agent.

@tylerbenson
Copy link
Contributor

Caused by: org.gradle.api.GradleException: Rule violated for class datadog.opentracing.StringCachingBigInteger: instructions covered ratio is 0.4, but expected minimum is 0.6

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;
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@aarya123 aarya123 Feb 20, 2020

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

@tylerbenson tylerbenson added tag: community Community contribution tag: performance Performance related changes labels Feb 21, 2020
@tylerbenson tylerbenson merged commit b805bf5 into DataDog:master Feb 21, 2020
@randomanderson randomanderson added this to the 0.44.0 milestone Feb 26, 2020
@aarya123 aarya123 deleted the cachingBigInteger branch February 27, 2020 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tag: community Community contribution tag: performance Performance related changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants