From eba2337e82a1b2cd355b6355b690034ee60aa332 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 24 Sep 2024 21:27:23 -0400 Subject: [PATCH] AVRO-4060: Use JDK to Hash Byte Array in UTF8 --- .../avro/src/main/java/org/apache/avro/util/Utf8.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java index b38d237f212..b7d88bfc3db 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java +++ b/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java @@ -174,8 +174,14 @@ public int hashCode() { if (h == 0) { byte[] bytes = this.bytes; int length = this.length; - 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 (bytes.length == length) { + h = Arrays.hashCode(bytes); + } else { + for (int i = 0; i < length; i++) { + h = h * 31 + bytes[i]; + } } this.hash = h; }