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

AVRO-4060: Use JDK to Hash Byte Array in UTF8 #3175

Merged
merged 1 commit into from
Feb 2, 2025
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
17 changes: 14 additions & 3 deletions lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public Utf8(byte[] bytes) {
this.length = length;
}

Utf8(String string, int length) {
this(string);
this.length = length;
}

/**
* Return UTF-8 encoded bytes. Only valid through {@link #getByteLength()}
* assuming the bytes have been fully copied into the underlying buffer from the
Expand Down Expand Up @@ -173,9 +178,15 @@ public int hashCode() {
if (h == 0) {
byte[] bytes = this.bytes;
int length = this.length;
h = 1;
for (int i = 0; i < length; i++) {
h = h * 31 + bytes[i];
// If the array is filled, use the underlying JDK hash functionality.
// Starting with JDK 21, the underlying implementation is vectorized.
if (length > 7 && bytes.length == length) {
h = Arrays.hashCode(bytes);
Copy link
Member

Choose a reason for hiding this comment

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

How does Arrays.hashCode(bytes) behave when the length is smaller ?
Doesn't it fall back to serial execution internally for anything that is not vectorizable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does fall back, but in my micro benchmarks, I found that it is better to implement this skip logic before bothering to jump into the method call itself especially since the length needs to be interrogated anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is to say, the underlying JDK implementation will not vectorize if the array is less than 8 bytes, so there's no reason to jump into Arrays.hashCode(bytes), it just falls back into the serial execution anyway. There is some other overhead involved as well, so it is just best to bypass it if the array is sufficiently small.

} else {
h = 1;
for (int i = 0; i < length; i++) {
h = h * 31 + bytes[i];
}
}
this.hash = h;
}
Expand Down
20 changes: 20 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ void hashCodeReused() {
assertEquals(4122302, u.hashCode());
}

/**
* There are two different code paths that hashcode() can call depending on the
* state of the internal buffer. If the buffer is full (string length is equal
* to buffer length) then the JDK hashcode function can be used. However, if the
* buffer is not full (string length is less than the internal buffer length),
* then the JDK does not support this prior to JDK 23 and a scalar
* implementation is the only option today. This difference can be resolved with
* JDK 23 as it supports both cases.
*/
@Test
void hashCodeBasedOnCapacity() {
// string = 8; buffer = 8
Utf8 fullCapacity = new Utf8("abcdefgh", 8);

// string = 8; buffer = 9
Utf8 partialCapacity = new Utf8("abcdefghX", 8);

assertEquals(fullCapacity.hashCode(), partialCapacity.hashCode());
}

@Test
void oversizeUtf8() {
Utf8 u = new Utf8();
Expand Down
Loading