Skip to content

Commit

Permalink
Cache ImmutableKeyValuePairs#hashCode (#5307)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Mar 18, 2023
1 parent 7f9c0dd commit 3581d6e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,30 @@ public class AttributesBenchmark {
// pre-allocate the keys & values to remove one possible confounding factor
private static final List<AttributeKey<String>> keys = new ArrayList<>(10);
private static final List<String> values = new ArrayList<>(10);
private static final List<Attributes> attributes = new ArrayList<>();

static {
for (int i = 0; i < 10; i++) {
keys.add(AttributeKey.stringKey("key" + i));
values.add("value" + i);
AttributesBuilder builder = Attributes.builder();
for (int j = 0; j <= i; j++) {
builder.put(keys.get(j), values.get(j));
}
attributes.add(builder.build());
}
}

@Benchmark
@BenchmarkMode({Mode.AverageTime})
@Fork(1)
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@SuppressWarnings("ReturnValueIgnored")
public void computeHashCode() {
for (Attributes attributes : attributes) {
attributes.hashCode();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@Immutable
public abstract class ImmutableKeyValuePairs<K, V> {
private final Object[] data;
private int hashcode;

/**
* Stores the raw object data directly. Does not do any de-duping or sorting. If you use this
Expand Down Expand Up @@ -247,9 +248,13 @@ public boolean equals(@Nullable Object o) {

@Override
public int hashCode() {
int result = 1;
result *= 1000003;
result ^= Arrays.hashCode(data);
int result = hashcode;
if (result == 0) {
result = 1;
result *= 1000003;
result ^= Arrays.hashCode(data);
hashcode = result;
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package io.opentelemetry.sdk.metrics;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
Expand All @@ -32,6 +35,22 @@
@Fork(1)
public class MetricsBenchmarks {

private static final List<Attributes> ATTRIBUTES_LIST;

static {
int keys = 5;
int valuesPerKey = 20;

ATTRIBUTES_LIST = new ArrayList<>();
for (int key = 0; key < keys; key++) {
AttributesBuilder builder = Attributes.builder();
for (int value = 0; value < valuesPerKey; value++) {
builder.put("key_" + key, "value_" + value);
}
ATTRIBUTES_LIST.add(builder.build());
}
}

@State(Scope.Thread)
public static class ThreadState {

Expand Down Expand Up @@ -65,6 +84,14 @@ public void tearDown(ThreadParams threadParams) {
}
}

@Benchmark
@Threads(1)
public void recordToMultipleAttributes(ThreadState threadState) {
for (Attributes attributes : ATTRIBUTES_LIST) {
threadState.op.perform(attributes);
}
}

@Benchmark
@Threads(1)
public void oneThread(ThreadState threadState) {
Expand Down

0 comments on commit 3581d6e

Please sign in to comment.