Skip to content

Commit ef6412b

Browse files
committed
Remove buckets from hashcode for performance and simplicity.
1 parent 0723de6 commit ef6412b

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogram.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.List;
2828
import java.util.OptionalLong;
2929

30-
import static org.elasticsearch.exponentialhistogram.ExponentialHistogramUtils.bucketIteratorHash;
3130
import static org.elasticsearch.exponentialhistogram.ExponentialHistogramUtils.bucketIteratorsEqual;
3231

3332
/**
@@ -152,8 +151,8 @@ public int hashCode() {
152151
int hash = scale();
153152
hash = 31 * hash + Double.hashCode(sum());
154153
hash = 31 * hash + zeroBucket().hashCode();
155-
hash = 31 * hash + bucketIteratorHash(negativeBuckets().iterator());
156-
hash = 31 * hash + bucketIteratorHash(positiveBuckets().iterator());
154+
// we intentionally don't include the hash of the buckets here, because that is likely expensive to compute
155+
// we assume that the sum is a good enough differentiator for most use cases
157156
return hash;
158157
}
159158

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtils.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,4 @@ static boolean bucketIteratorsEqual(BucketIterator a, BucketIterator b) {
7373
}
7474
return a.hasNext() == b.hasNext();
7575
}
76-
77-
static int bucketIteratorHash(BucketIterator it) {
78-
int hash = 0;
79-
while (it.hasNext()) {
80-
hash = 31 * hash + Long.hashCode(it.peekIndex());
81-
hash = 31 * hash + Long.hashCode(it.peekCount());
82-
it.advance();
83-
}
84-
return hash;
85-
}
8676
}

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramEqualityTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public void testEquality() {
6868
}
6969

7070
public void testHashQuality() {
71+
switch (modification) {
72+
// we intentionally don't include the buckets in the hash, so skip them in tests
73+
case POSITIVE_BUCKETS, NEGATIVE_BUCKETS:
74+
return;
75+
}
76+
7177
ExponentialHistogram histo = randomHistogram();
7278
// of 10 tries, at least one should produce a different hash code
7379
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)