Don't create Supplier when decoding byte (#3273)

This commit is contained in:
Anuraag Agrawal 2021-06-04 08:31:40 +09:00 committed by GitHub
parent 37cd112e9d
commit d044890a7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 21 deletions

View File

@ -22,7 +22,7 @@ import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Fork(3)
@Threads(1)
public class SpanIdBenchmark {

View File

@ -14,7 +14,7 @@ public final class OtelEncodingUtils {
static final int BYTE_BASE16 = 2;
static final int LONG_BASE16 = BYTE_BASE16 * LONG_BYTES;
private static final String ALPHABET = "0123456789abcdef";
private static final int ASCII_CHARACTERS = 128;
private static final int NUM_ASCII_CHARACTERS = 128;
private static final char[] ENCODING = buildEncodingArray();
private static final byte[] DECODING = buildDecodingArray();
@ -28,7 +28,7 @@ public final class OtelEncodingUtils {
}
private static byte[] buildDecodingArray() {
byte[] decoding = new byte[ASCII_CHARACTERS];
byte[] decoding = new byte[NUM_ASCII_CHARACTERS];
Arrays.fill(decoding, (byte) -1);
for (int i = 0; i < ALPHABET.length(); i++) {
char c = ALPHABET.charAt(i);
@ -110,10 +110,12 @@ public final class OtelEncodingUtils {
* @return the resulting {@code byte}
*/
public static byte byteFromBase16(char first, char second) {
Utils.checkArgument(
first < ASCII_CHARACTERS && DECODING[first] != -1, () -> "invalid character " + first);
Utils.checkArgument(
second < ASCII_CHARACTERS && DECODING[second] != -1, () -> "invalid character " + second);
if (first >= NUM_ASCII_CHARACTERS || DECODING[first] == -1) {
throw new IllegalArgumentException("invalid character " + first);
}
if (second >= NUM_ASCII_CHARACTERS || DECODING[second] == -1) {
throw new IllegalArgumentException("invalid character " + second);
}
int decoded = DECODING[first] << 4 | DECODING[second];
return (byte) decoded;
}

View File

@ -5,7 +5,6 @@
package io.opentelemetry.api.internal;
import java.util.function.Supplier;
import javax.annotation.concurrent.Immutable;
/** General internal utility methods. */
@ -26,17 +25,4 @@ public final class Utils {
throw new IllegalArgumentException(errorMessage);
}
}
/**
* Throws an {@link IllegalArgumentException} if the argument is false. This method is similar to
* {@code Preconditions.checkArgument(boolean, Object)} from Guava.
*
* @param isValid whether the argument check passed.
* @param errorMessage the supplier of the message to use for the exception.
*/
public static void checkArgument(boolean isValid, Supplier<String> errorMessage) {
if (!isValid) {
throw new IllegalArgumentException(errorMessage.get());
}
}
}