Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache ImmutableKeyValuePairs#hashCode #5307

Merged
merged 1 commit into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to mark as volatile for thread safety?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think so. Reading volatiles isn't free, and its not the end of the world if the hashcode gets computed multiple times.


/**
* 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