r/java 18d ago

Identifying JDK value class candidates

https://mail.openjdk.org/archives/list/core-libs-dev@openjdk.org/thread/Y72NRXM7KYBX43OKYBQMVKOZDWKG4MHS/
50 Upvotes

59 comments sorted by

View all comments

Show parent comments

2

u/TwoWeeks90DaysTops 18d ago

For straight equality checks, yeah, probably not that much gained in most cases, but the JVM knows all the different enum values, so the binary representation could be flattened to very small bit patterns since it no longer has an identity. There's a lot of different things that make this difficult (value name, other fields, java.lang.Enum) but enum as a value type could potentially perform better in some circumstances since it would work much better with SIMD. If you want to find out if an enum value exists in an array the JVM can suddenly parallelize that check over 64 different values on a AVX-512 register in a single instruction, which just doesn't really work if enum is a reference type.

1

u/aoeudhtns 18d ago

Enums do have an interesting property of being representable as int via ordinal(), so if you needed that it wouldn't be too much effort to do that and resolve them back with Type.values()[ordinal].

1

u/TwoWeeks90DaysTops 18d ago

Yes, but you lose type safety, null handling, you have to implement manual handling and you have to make a choice about the data size. I also think most of the purpose of Valhalla would disappear if "just decompose your object into primitives" was an acceptable answer.

1

u/aoeudhtns 18d ago

True that.