Cache the result of toString in BigInteger
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
@@ -716,7 +716,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
// case
BigInteger value;
do {
- value = new BigInteger(63, ThreadLocalRandom.current());
+ value = new StringCachingBigInteger(63, ThreadLocalRandom.current());
} while (value.signum() == 0);
return value;

ADD
dd-trace-ot/src/main/java/datadog/opentracing/StringCachingBigInteger.java
+package datadog.opentracing;
+
+import java.math.BigInteger;
+import java.util.Random;
+
+/**
+ * Because we are using BigInteger for Trace and Span Id, the toString() operator may result in heavy
+ * computation and string allocation overhead. In order to limit this, we are caching the result
+ * of toString, thereby taking advantage of the immutability of BigInteger.
+ */
+public class StringCachingBigInteger extends BigInteger {
+
+ private String cachedString;
+
+ public StringCachingBigInteger(byte[] val, int off, int len) {
+ super(val, off, len);
+ }
+
+ public StringCachingBigInteger(byte[] val) {
+ super(val);
+ }
+
+ public StringCachingBigInteger(int signum, byte[] magnitude, int off, int len) {
+ super(signum, magnitude, off, len);
+ }
+
+ public StringCachingBigInteger(int signum, byte[] magnitude) {
+ super(signum, magnitude);
+ }
+
+ public StringCachingBigInteger(String val, int radix) {
+ super(val, radix);
+ }
+
+ public StringCachingBigInteger(String val) {
+ super(val);
+ }
+
+ public StringCachingBigInteger(int numBits, Random rnd) {
+ super(numBits, rnd);
+ }
+
+ public StringCachingBigInteger(int bitLength, int certainty, Random rnd) {
+ super(bitLength, certainty, rnd);
+ }
+
+ @Override
+ public String toString() {
+ if (cachedString == null) {
+ this.cachedString = super.toString();
+ }
+ return cachedString;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return super.equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return super.hashCode();
+ }
+}

Cache the result of toString in BigInteger