This is an automated email from the ASF dual-hosted git repository.
jackie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new 72b8fc1 accelerate ByteArray.hashCode (#7622)
72b8fc1 is described below
commit 72b8fc14a2b78d5910333a2500fb65bd1aba3c8d
Author: Richard Startin <[email protected]>
AuthorDate: Sun Oct 24 02:09:37 2021 +0100
accelerate ByteArray.hashCode (#7622)
---
.../java/org/apache/pinot/spi/utils/ByteArray.java | 18 +++++++++++++++++-
.../java/org/apache/pinot/spi/utils/ByteArrayTest.java | 14 ++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/pinot-spi/src/main/java/org/apache/pinot/spi/utils/ByteArray.java
b/pinot-spi/src/main/java/org/apache/pinot/spi/utils/ByteArray.java
index 3181bbc..74a271e 100644
--- a/pinot-spi/src/main/java/org/apache/pinot/spi/utils/ByteArray.java
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/utils/ByteArray.java
@@ -94,7 +94,23 @@ public class ByteArray implements Comparable<ByteArray>,
Serializable {
@Override
public int hashCode() {
- return Arrays.hashCode(_bytes);
+ int hash = 1;
+ int i = 0;
+ for (; i + 7 < _bytes.length; i += 8) {
+ hash = -1807454463 * hash
+ + 1742810335 * _bytes[i]
+ + 887503681 * _bytes[i + 1]
+ + 28629151 * _bytes[i + 2]
+ + 923521 * _bytes[i + 3]
+ + 29791 * _bytes[i + 4]
+ + 961 * _bytes[i + 5]
+ + 31 * _bytes[i + 6]
+ + _bytes[i + 7];
+ }
+ for (; i < _bytes.length; i++) {
+ hash = 31 * hash + _bytes[i];
+ }
+ return hash;
}
@Override
diff --git
a/pinot-spi/src/test/java/org/apache/pinot/spi/utils/ByteArrayTest.java
b/pinot-spi/src/test/java/org/apache/pinot/spi/utils/ByteArrayTest.java
index cf10745..6d37d88 100644
--- a/pinot-spi/src/test/java/org/apache/pinot/spi/utils/ByteArrayTest.java
+++ b/pinot-spi/src/test/java/org/apache/pinot/spi/utils/ByteArrayTest.java
@@ -20,6 +20,7 @@ package org.apache.pinot.spi.utils;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.concurrent.ThreadLocalRandom;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@@ -28,6 +29,19 @@ import static org.testng.Assert.assertTrue;
public class ByteArrayTest {
+ @Test(description = "hash code may have been used for partitioning so must
be stable")
+ public void testHashCode() {
+ // ensure to test below 8
+ byte[] bytes = new byte[ThreadLocalRandom.current().nextInt(8)];
+ ThreadLocalRandom.current().nextBytes(bytes);
+ assertEquals(Arrays.hashCode(bytes), new ByteArray(bytes).hashCode());
+ for (int i = 0; i < 10_000; i++) {
+ bytes = new byte[ThreadLocalRandom.current().nextInt(2048)];
+ ThreadLocalRandom.current().nextBytes(bytes);
+ assertEquals(Arrays.hashCode(bytes), new ByteArray(bytes).hashCode());
+ }
+ }
+
@Test
public void testCompare() {
byte[] foo = "foo".getBytes(StandardCharsets.UTF_8);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]