[prev in list] [next in list] [prev in thread] [next in thread]
List: avro-commits
Subject: [avro] branch master updated: AVRO-2704: Cache Hashcode of UTF8 Strings (#783)
From: fokko () apache ! org
Date: 2020-04-04 9:44:13
Message-ID: 158599345350.16108.10883997042444839032 () gitbox ! apache ! org
[Download RAW message or body]
This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new d5d1cd7 AVRO-2704: Cache Hashcode of UTF8 Strings (#783)
d5d1cd7 is described below
commit d5d1cd70e920f45468bf696d3668b496a9adbab3
Author: belugabehr <12578579+belugabehr@users.noreply.github.com>
AuthorDate: Sat Apr 4 05:44:03 2020 -0400
AVRO-2704: Cache Hashcode of UTF8 Strings (#783)
Co-authored-by: David Mollitor <dmollitor@apache.org>
---
lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java | 12 +++++++++---
.../avro/src/test/java/org/apache/avro/util/TestUtf8.java | 12 ++++++++++++
2 files changed, 21 insertions(+), 3 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 e6d9251..2302276 \
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
@@ -48,6 +48,8 @@ public class Utf8 implements Comparable<Utf8>, CharSequence {
}
private byte[] bytes = EMPTY;
+ private int hash = 0;
+ private boolean hasHash = false;
private int length;
private String string;
@@ -117,6 +119,7 @@ public class Utf8 implements Comparable<Utf8>, CharSequence {
}
this.length = newLength;
this.string = null;
+ this.hasHash = false;
return this;
}
@@ -166,9 +169,12 @@ public class Utf8 implements Comparable<Utf8>, CharSequence {
@Override
public int hashCode() {
- int hash = 0;
- for (int i = 0; i < this.length; i++)
- hash = hash * 31 + bytes[i];
+ if (!hasHash) {
+ for (int i = 0; i < length; i++) {
+ hash = hash * 31 + bytes[i];
+ }
+ hasHash = true;
+ }
return hash;
}
diff --git a/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java \
b/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java index \
2692d7e..60c8f71 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
@@ -48,4 +48,16 @@ public class TestUtf8 {
assertEquals(4, u.getByteLength());
assertSame(content, u.getBytes());
}
+
+ @Test
+ public void testHashCodeReused() {
+ Utf8 u = new Utf8("a");
+ assertEquals(97, u.hashCode());
+
+ u.setByteLength(2);
+ u.set("zz");
+
+ assertEquals(97121, u.hashCode());
+ assertEquals(97121, u.hashCode());
+ }
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic