Skip to content

Commit 2770b7c

Browse files
baolongntBao-Long Nguyen-Trong
authored andcommitted
Switch to only init between the extreme HEX values (vs all 64K)
1 parent 5e93095 commit 2770b7c

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ 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-
25-
/**
26-
* Stores whether the character at that index is a valid HEX character. We lazy init the array
27-
* values to minimize impact during process startup esp. on mobile. A value of 0 means the
28-
* character validity has not been determined yet. A value of 1 means the character is a valid HEX
29-
* character. A value of -1 means the character is an invalid HEX character.
30-
*
31-
* @see #isValidBase16Character(char)
32-
*/
33-
private static final int[] VALID_HEX = new int[Character.MAX_VALUE];
24+
private static final boolean[] VALID_HEX = buildValidHexArray();
3425

3526
private static char[] buildEncodingArray() {
3627
char[] encoding = new char[512];
@@ -51,6 +42,17 @@ private static byte[] buildDecodingArray() {
5142
return decoding;
5243
}
5344

45+
private static boolean[] buildValidHexArray() {
46+
boolean[] validHex = new boolean[Character.MAX_VALUE];
47+
// Use the fact that the default initial boolean value is false
48+
// and only explicitly init the values between the extreme HEX values
49+
// This minimized impact during process startup esp. on mobile.
50+
for (int i = 48; i < 103; i++) {
51+
validHex[i] = i <= 57 || 97 <= i;
52+
}
53+
return validHex;
54+
}
55+
5456
/**
5557
* Returns the {@code long} value whose base16 representation is stored in the first 16 chars of
5658
* {@code chars} starting from the {@code offset}.
@@ -153,12 +155,7 @@ public static boolean isValidBase16String(CharSequence value) {
153155

154156
/** Returns whether the given {@code char} is a valid hex character. */
155157
public static boolean isValidBase16Character(char b) {
156-
int isValid = VALID_HEX[b];
157-
if (isValid == 0) {
158-
isValid = (48 <= b && b <= 57) || (97 <= b && b <= 102) ? 1 : -1;
159-
VALID_HEX[b] = isValid;
160-
}
161-
return isValid == 1;
158+
return VALID_HEX[b];
162159
}
163160

164161
private OtelEncodingUtils() {}

0 commit comments

Comments
 (0)