mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Cache the return value of Arrays.hashCode().
This commit is contained in:
@@ -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 {
|
|||||||
|
|
||||||
@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 {
|
|||||||
|
|
||||||
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 {
|
|||||||
|
|
||||||
@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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user