Skip to content

Commit f53315a

Browse files
author
Bao-Long Nguyen-Trong
committed
Lazy init valix hex array
Init of a 64k boolean array take a long time (>30ms P90) during Android app launch.
1 parent 36ca9b8 commit f53315a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

api/all/src/main/java/io/opentelemetry/api/internal/OtelEncodingUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public final class OtelEncodingUtils {
2121
private static final int NUM_ASCII_CHARACTERS = 128;
2222
private static final char[] ENCODING = buildEncodingArray();
2323
private static final byte[] DECODING = buildDecodingArray();
24-
private static final boolean[] VALID_HEX = buildValidHexArray();
24+
25+
private static volatile boolean[] validHex = null;
2526

2627
private static char[] buildEncodingArray() {
2728
char[] encoding = new char[512];
@@ -152,7 +153,15 @@ public static boolean isValidBase16String(CharSequence value) {
152153

153154
/** Returns whether the given {@code char} is a valid hex character. */
154155
public static boolean isValidBase16Character(char b) {
155-
return VALID_HEX[b];
156+
if (validHex == null) {
157+
synchronized (OtelEncodingUtils.class) {
158+
if (validHex == null) {
159+
validHex = buildValidHexArray();
160+
}
161+
}
162+
}
163+
164+
return validHex[b];
156165
}
157166

158167
private OtelEncodingUtils() {}

0 commit comments

Comments
 (0)