RFR: 8306075: Micro-optimize Enum.hashCode [v2]
Jorn Vernee
jvernee at openjdk.org
Mon Apr 17 21:18:46 UTC 2023
On Mon, 17 Apr 2023 13:40:02 GMT, Claes Redestad <redestad at openjdk.org> wrote:
> > > Why isn't `Enum::hashCode` simply doing `return ordinal;`?
> >
> >
> > See https://bugs.openjdk.org/browse/JDK-8050217
>
> Thanks! If there are apps where `Enum::hashCode` is performance sensitive then run-to-run stability may be a stronger argument than @stuart-marks et al argues it is here, though there might be some unspoken arguments about how it might affect iteration order et.c...
Identity hash code might theoretically be stable from run-to-run, but it's not like it's very stable in the first place. The hash code is generated using some thread-local state, so the value depends on how many times an identity hash code has been generated on the same thread before the hash code of the enum constant is generated (when `hashCode` is first called)
So, using ordinal as a hash code would, AFAICS, be no more unstable than the current implementation: if the code is altered (re-ordering the constants. Or calling hashCode for the first time at a different point in the application), the identity hash code can change. However, using the ordinal has the benefit of simplicity as well.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/13491#issuecomment-1512094546
More information about the core-libs-dev
mailing list