From 05c95100ab074cabe683e2cf5c7d10ef0cafc366 Mon Sep 17 00:00:00 2001 From: Tyler Benson Date: Wed, 25 Sep 2019 18:14:40 -0700 Subject: [PATCH 1/2] Minor optimization to avoid using BigInteger if possible --- .../src/main/java/datadog/opentracing/DDSpan.java | 11 ++++++++++- .../opentracing/DDSpanSerializationTest.groovy | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java b/dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java index c7602b29557..3562602fac8 100644 --- a/dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java +++ b/dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java @@ -417,6 +417,7 @@ public String toString() { } protected static class UInt64IDStringSerializer extends StdSerializer { + private static final int LONG_PARSE_LIMIT = String.valueOf(Long.MAX_VALUE).length(); public UInt64IDStringSerializer() { this(null); @@ -430,7 +431,15 @@ public UInt64IDStringSerializer(final Class 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. + // 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)); + } } } } diff --git a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy index 39daa4b428d..7e24951f271 100644 --- a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy +++ b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy @@ -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 @@ -128,7 +127,9 @@ class DDSpanSerializationTest extends Specification { value | _ BigInteger.ZERO | _ BigInteger.ONE | _ + new BigInteger("8223372036854775807") | _ BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.ONE) | _ BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _ + BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE) | _ } } From 82d60c27f4f17ea0474e98196ee5ffc8d343a320 Mon Sep 17 00:00:00 2001 From: Tyler Benson Date: Fri, 27 Sep 2019 15:04:41 -0700 Subject: [PATCH 2/2] Fix codeNarc --- .../groovy/datadog/opentracing/DDSpanSerializationTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy index 7e24951f271..cab3819ef9f 100644 --- a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy +++ b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy @@ -127,7 +127,7 @@ class DDSpanSerializationTest extends Specification { value | _ BigInteger.ZERO | _ BigInteger.ONE | _ - new BigInteger("8223372036854775807") | _ + 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) | _