From 1908897b93a90ce0a44fe43de481b687817963f0 Mon Sep 17 00:00:00 2001 From: Claes Redestad Date: Tue, 2 Apr 2019 11:24:40 +0200 Subject: [PATCH] 8221723: Avoid storing zero to String.hash Reviewed-by: shade, prappo, jiangli --- src/java.base/share/classes/java/lang/String.java | 10 ++++++++-- .../org/openjdk/bench/java/lang/StringHashCode.java | 13 ++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java index 1f2d8c5fa92..f4f11fc6a54 100644 --- a/src/java.base/share/classes/java/lang/String.java +++ b/src/java.base/share/classes/java/lang/String.java @@ -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; } diff --git a/test/micro/org/openjdk/bench/java/lang/StringHashCode.java b/test/micro/org/openjdk/bench/java/lang/StringHashCode.java index e00cdb4b050..96dbb0c5bda 100644 --- a/test/micro/org/openjdk/bench/java/lang/StringHashCode.java +++ b/test/micro/org/openjdk/bench/java/lang/StringHashCode.java @@ -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. * * 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 hashcode0; + private String empty; @Setup public void setup() { hashcode = "abcdefghijkl"; 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() { 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(); + } }