Skip to content
Snippets Groups Projects
Commit e23f6461 authored by akwizgran's avatar akwizgran
Browse files

Cache the return value of Arrays.hashCode().

parent b72a90be
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,8 @@ public class Bytes { ...@@ -7,6 +7,8 @@ public class Bytes {
private final byte[] bytes; private final byte[] bytes;
private int hashCode = -1;
public Bytes(byte[] bytes) { public Bytes(byte[] bytes) {
this.bytes = bytes; this.bytes = bytes;
} }
...@@ -17,7 +19,10 @@ public class Bytes { ...@@ -17,7 +19,10 @@ public class Bytes {
@Override @Override
public int hashCode() { public int hashCode() {
return Arrays.hashCode(bytes); // Thread-safe because if two or more threads check and update the
// value, they'll calculate the same value
if(hashCode == -1) hashCode = Arrays.hashCode(bytes);
return hashCode;
} }
@Override @Override
......
...@@ -9,6 +9,8 @@ public abstract class UniqueId { ...@@ -9,6 +9,8 @@ public abstract class UniqueId {
protected final byte[] id; protected final byte[] id;
private int hashCode = -1;
protected UniqueId(byte[] id) { protected UniqueId(byte[] id) {
assert id.length == LENGTH; assert id.length == LENGTH;
this.id = id; this.id = id;
...@@ -20,6 +22,9 @@ public abstract class UniqueId { ...@@ -20,6 +22,9 @@ public abstract class UniqueId {
@Override @Override
public int hashCode() { public int hashCode() {
return Arrays.hashCode(id); // Thread-safe because if two or more threads check and update the
// value, they'll calculate the same value
if(hashCode == -1) hashCode = Arrays.hashCode(id);
return hashCode;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment