RFR: 8306075: Micro-optimize Enum.hashCode [v3]

Pavel Rappo prappo at openjdk.org
Mon Apr 17 14:59:36 UTC 2023


On Mon, 17 Apr 2023 13:49:11 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> From that impl note it seemed like it was a big deal for hash code to never return 0 for an object. Could you maybe de-emphasize the importance of that HotSpot behavior in the note?
>
> All right, we can change "This allows to treat zero as the marker..." to "This makes zero a convenient marker...", I think?

To me, the proposed implementation looks similar to that of String::hashCode before Compact Strings (JEP 254).

The interesting detail here is the fact that unlike String::hashCode, which can legitimately return 0, Object::hashCode cannot, at least in HotSpot. This is great news, because to be performant, java.lang.Enum does not need to be bloated with extra mechanics, such as that boolean `hashIsZero` introduced for java.lang.String in 8221836: Avoid recalculating String.hash when zero.

If the essence of that can be compressed to a few simple sentences and possibly moved to an inline comment in Enum::hashCode, that _might_ be better.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13491#discussion_r1168853799


More information about the core-libs-dev mailing list