8221723: Avoid storing zero to String.hash
Reviewed-by: shade, prappo, jiangli
This commit is contained in:
parent
6bce53795b
commit
1908897b93
@ -1510,8 +1510,14 @@ public final class String
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int h = hash;
|
int h = hash;
|
||||||
if (h == 0 && value.length > 0) {
|
if (h == 0 && value.length > 0) {
|
||||||
hash = h = isLatin1() ? StringLatin1.hashCode(value)
|
h = isLatin1() ? StringLatin1.hashCode(value)
|
||||||
: StringUTF16.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;
|
return h;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -42,11 +42,13 @@ public class StringHashCode {
|
|||||||
|
|
||||||
private String hashcode;
|
private String hashcode;
|
||||||
private String hashcode0;
|
private String hashcode0;
|
||||||
|
private String empty;
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() {
|
||||||
hashcode = "abcdefghijkl";
|
hashcode = "abcdefghijkl";
|
||||||
hashcode0 = new String(new char[]{72, 90, 100, 89, 105, 2, 72, 90, 100, 89, 105, 2});
|
hashcode0 = new String(new char[]{72, 90, 100, 89, 105, 2, 72, 90, 100, 89, 105, 2});
|
||||||
|
empty = new String();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,4 +68,13 @@ public class StringHashCode {
|
|||||||
public int notCached() {
|
public int notCached() {
|
||||||
return hashcode0.hashCode();
|
return hashcode0.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Benchmark testing String.hashCode() with the empty string. Since the
|
||||||
|
* empty String has hashCode = 0, this value is always recalculated.
|
||||||
|
*/
|
||||||
|
@Benchmark
|
||||||
|
public int empty() {
|
||||||
|
return empty.hashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user