Value Classes Still Need Compiler Sympathy
JDK 28's JEP 401 previews value classes, promising performance gains by eliminating identity. However, a deep dive reveals that value classes can sometimes slow programs down, especially when megamorphic call sites force materialization. This post examines three examples: flattening immutable fields, scalarizing allocations, and the impact of type erasure on generic interfaces. It shows that while value classes offer new optimization opportunities, they also introduce new costs that developers must understand.
A well-intentioned program may put the JVM into a situation where a flattened representation is faster for some methods, and a reference representation is faster for others.