Don't create Supplier when decoding byte (#3273)
This commit is contained in:
parent
37cd112e9d
commit
d044890a7c
|
|
@ -22,7 +22,7 @@ import org.openjdk.jmh.annotations.Warmup;
|
||||||
@Warmup(iterations = 5, time = 1)
|
@Warmup(iterations = 5, time = 1)
|
||||||
@Measurement(iterations = 15, time = 1)
|
@Measurement(iterations = 15, time = 1)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
@Fork(1)
|
@Fork(3)
|
||||||
@Threads(1)
|
@Threads(1)
|
||||||
public class SpanIdBenchmark {
|
public class SpanIdBenchmark {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ public final class OtelEncodingUtils {
|
||||||
static final int BYTE_BASE16 = 2;
|
static final int BYTE_BASE16 = 2;
|
||||||
static final int LONG_BASE16 = BYTE_BASE16 * LONG_BYTES;
|
static final int LONG_BASE16 = BYTE_BASE16 * LONG_BYTES;
|
||||||
private static final String ALPHABET = "0123456789abcdef";
|
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 char[] ENCODING = buildEncodingArray();
|
||||||
private static final byte[] DECODING = buildDecodingArray();
|
private static final byte[] DECODING = buildDecodingArray();
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ public final class OtelEncodingUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] buildDecodingArray() {
|
private static byte[] buildDecodingArray() {
|
||||||
byte[] decoding = new byte[ASCII_CHARACTERS];
|
byte[] decoding = new byte[NUM_ASCII_CHARACTERS];
|
||||||
Arrays.fill(decoding, (byte) -1);
|
Arrays.fill(decoding, (byte) -1);
|
||||||
for (int i = 0; i < ALPHABET.length(); i++) {
|
for (int i = 0; i < ALPHABET.length(); i++) {
|
||||||
char c = ALPHABET.charAt(i);
|
char c = ALPHABET.charAt(i);
|
||||||
|
|
@ -110,10 +110,12 @@ public final class OtelEncodingUtils {
|
||||||
* @return the resulting {@code byte}
|
* @return the resulting {@code byte}
|
||||||
*/
|
*/
|
||||||
public static byte byteFromBase16(char first, char second) {
|
public static byte byteFromBase16(char first, char second) {
|
||||||
Utils.checkArgument(
|
if (first >= NUM_ASCII_CHARACTERS || DECODING[first] == -1) {
|
||||||
first < ASCII_CHARACTERS && DECODING[first] != -1, () -> "invalid character " + first);
|
throw new IllegalArgumentException("invalid character " + first);
|
||||||
Utils.checkArgument(
|
}
|
||||||
second < ASCII_CHARACTERS && DECODING[second] != -1, () -> "invalid character " + second);
|
if (second >= NUM_ASCII_CHARACTERS || DECODING[second] == -1) {
|
||||||
|
throw new IllegalArgumentException("invalid character " + second);
|
||||||
|
}
|
||||||
int decoded = DECODING[first] << 4 | DECODING[second];
|
int decoded = DECODING[first] << 4 | DECODING[second];
|
||||||
return (byte) decoded;
|
return (byte) decoded;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
package io.opentelemetry.api.internal;
|
package io.opentelemetry.api.internal;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
/** General internal utility methods. */
|
/** General internal utility methods. */
|
||||||
|
|
@ -26,17 +25,4 @@ public final class Utils {
|
||||||
throw new IllegalArgumentException(errorMessage);
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue