Skip to content
Closed
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
9 changes: 8 additions & 1 deletion src/main/java/at/favre/lib/bytes/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ public static Bytes random(int length, Random random) {
private final ByteOrder byteOrder;
private final BytesFactory factory;
private transient int hashCodeCache;
/**
* Cache if the hash has been calculated as actually being zero, enabling us to avoid recalculating this.
*/
private transient boolean hashCodeIsZero; // Default to false;

Bytes(byte[] byteArray, ByteOrder byteOrder) {
this(byteArray, byteOrder, new Factory());
Expand Down Expand Up @@ -2221,8 +2225,11 @@ public boolean equalsContent(Bytes other) {

@Override
public int hashCode() {
if (hashCodeCache == 0) {
if (hashCodeCache == 0 && !hashCodeIsZero) {
hashCodeCache = Util.Obj.hashCode(internalArray(), byteOrder());
if (hashCodeCache == 0) {
hashCodeIsZero = true;
}
}
return hashCodeCache;
}
Expand Down