RFR: 8221723: Avoid storing zero to String.hash
john.r.rose at oracle.com
Mon Apr 1 21:26:10 UTC 2019
On Apr 1, 2019, at 12:50 PM, dean.long at oracle.com wrote:
> Wouldn't it be better to write a non-0 value when the computed hash code is 0, so we don't have to recompute it? Is there some advantage to writing 0 instead of any other value, such as 1?
Zero is the easiest sentinel value because it's the default
value for int.
*Any* 32-bit sentinel you choose will be the legitimate hashCode
of *some* string. That is to say, the range of String.hashCode
is all 2^32 points of the int domain. That's why we need an extra
bit somewhere if we want to distinguish a sentinel (like zero) from
an identical legitimate hash code that has been cached.
Luckily, there is plenty of "slack" in the memory layout of String.
We can find a bit, if we want to go there.
More information about the core-libs-dev