RFR: 8221723: Avoid storing zero to String.hash

Martin Buchholz martinrb at google.com
Mon Apr 1 17:13:16 UTC 2019

I'm confused.

We have always had
         if (h == 0 && value.length > 0) {
so we have already been special-casing empty strings (maybe we should

Java has guaranteed that string literals are unique strings, but the empty
string is not special in this regard.  Archiving any string literal and
restoring it later should be identity-preserving, no?

Whenever we test or benchmark zero-hash Strings, there are 3 cases that
should be covered - literal "", new String(), and (rare!) non-empty Strings
that happen to hash to 0.

We could avoid archiving those rare non-empty Strings that happen to hash
to 0 at archive time if it saved us a branch at runtime.

More information about the core-libs-dev mailing list