Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public static UTF8BytesString create(ByteBuffer utf8BytesBuffer) {

private final String string;
private byte[] utf8Bytes;
// Lazy hashCode cache. {@link String} already caches its own {@code hashCode} internally, but
// the inter-class call through {@code string.hashCode()} still costs a virtual dispatch + the
// String's own cached-hash field read + branch -- caching here saves all of that on subsequent
// calls. Benign race in the same shape as {@link #utf8Bytes}: two threads computing the same
// hash in parallel both produce the same value, and {@code int} writes are atomic per JLS so a
// reader cannot observe a partial value.
private int cachedHashCode;

private UTF8BytesString(String string) {
this.string = string;
Expand Down Expand Up @@ -114,7 +121,12 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return this.string.hashCode();
int h = this.cachedHashCode;
if (h == 0) {
h = this.string.hashCode();
this.cachedHashCode = h;
}
return h;
}

@Override
Expand Down
Loading