8221723: Avoid storing zero to String.hash

Reviewed-by: shade, prappo, jiangli
This commit is contained in:
Claes Redestad 2019-04-02 11:24:40 +02:00
parent 6bce53795b
commit 1908897b93
2 changed files with 20 additions and 3 deletions

View file

@ -1510,8 +1510,14 @@ public final class String
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
hash = h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
// Avoid issuing a store if the calculated value is also zero:
// in addition to a minor performance benefit, this allows storing
// Strings with zero hash code in read-only memory.
if (h != 0) {
hash = h;
}
}
return h;
}