Skip to content

Minor optimization to avoid using BigInteger if possible #1016

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

Merged
merged 2 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ public String toString() {
}

protected static class UInt64IDStringSerializer extends StdSerializer<String> {
private static final int LONG_PARSE_LIMIT = String.valueOf(Long.MAX_VALUE).length();

public UInt64IDStringSerializer() {
this(null);
Expand All @@ -430,7 +431,15 @@ public UInt64IDStringSerializer(final Class<String> stringClass) {
public void serialize(
final String value, final JsonGenerator gen, final SerializerProvider provider)
throws IOException {
gen.writeNumber(new BigInteger(value));
final int length = value.length();
// BigInteger's are expensive, so lets try to avoid using them if possible.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use JsonGenerator::writeNumber(String)?
The doc says it isn't support for all generators, but if it is available it would save us parsing & allocating.

My other thought was a custom Number class to represent UInt64, but JsonGenerator doesn't support that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fair enough. I was hoping that it would be implemented more efficiently, but apparently, it is less efficient.

// This is a rough approximation for optimization.
// There are some values that would pass this test that could be parsed with Long.parseLong.
if (length > LONG_PARSE_LIMIT || (length == LONG_PARSE_LIMIT && value.startsWith("9"))) {
gen.writeNumber(new BigInteger(value));
} else {
gen.writeNumber(Long.parseLong(value));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package datadog.opentracing


import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.collect.Maps
import datadog.trace.agent.test.utils.ConfigUtils
Expand Down Expand Up @@ -128,7 +127,9 @@ class DDSpanSerializationTest extends Specification {
value | _
BigInteger.ZERO | _
BigInteger.ONE | _
8223372036854775807G | _
BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.ONE) | _
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _
BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE) | _
}
}