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

Aleksey Shipilev shade at openjdk.org
Tue Apr 18 09:05:50 UTC 2023


On Mon, 17 Apr 2023 21:12:58 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> (if anything, using the ordinal would be _more_ stable, since it is only affected by the order of the constants, rather than by other code that runs before the identity hash code is generated).

Here lies the major original concern -- that I share -- about hooking up `ordinal` as `hashCode`, I think. It opens up enum for the similar sort of algorithmic collisions, perhaps even deliberate ones, like we had with `String`-s. Arguably, switching enum hashcode from IHC to ordinal is a step backwards.

Of course, as John says above, this might change when Valhalla arrives, but in that case we would give up the collision-resistant enum hashcodes for clear benefit of enums being flattened: it would be the "one step back, two steps forward" kind of deal. Today, however, there seem to be to reason to give up this resistance without getting anything in return. The "return" might have been better performance, but this PR eliminates that part as well.

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

PR Comment: https://git.openjdk.org/jdk/pull/13491#issuecomment-1512723491


More information about the core-libs-dev mailing list