Remove unnecessary string allocation on hot path (#3272)
This commit is contained in:
parent
34494e609b
commit
ff85102a46
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright The OpenTelemetry Authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package io.opentelemetry.api.trace;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Threads;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@Warmup(iterations = 5, time = 1)
|
||||
@Measurement(iterations = 15, time = 1)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Fork(1)
|
||||
@Threads(1)
|
||||
public class SpanIdBenchmark {
|
||||
|
||||
@Benchmark
|
||||
public byte[] getSpanIdBytes() {
|
||||
return SpanContext.getInvalid().getSpanIdBytes();
|
||||
}
|
||||
}
|
||||
|
|
@ -111,9 +111,9 @@ public final class OtelEncodingUtils {
|
|||
*/
|
||||
public static byte byteFromBase16(char first, char second) {
|
||||
Utils.checkArgument(
|
||||
first < ASCII_CHARACTERS && DECODING[first] != -1, "invalid character " + first);
|
||||
first < ASCII_CHARACTERS && DECODING[first] != -1, () -> "invalid character " + first);
|
||||
Utils.checkArgument(
|
||||
second < ASCII_CHARACTERS && DECODING[second] != -1, "invalid character " + second);
|
||||
second < ASCII_CHARACTERS && DECODING[second] != -1, () -> "invalid character " + second);
|
||||
int decoded = DECODING[first] << 4 | DECODING[second];
|
||||
return (byte) decoded;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.opentelemetry.api.internal;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/** General internal utility methods. */
|
||||
|
|
@ -25,4 +26,17 @@ 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue