HeadlinesBriefing favicon HeadlinesBriefing.com

JDK 28 Value Classes Preview Features Optimizations

Hacker News •
×

This post discusses preview features in JDK 28, specifically JEP 401, a major Valhalla milestone integrated as a preview feature. Value classes increase the ability to communicate program semantics and optimization opportunities available to the JVM. However, a belief that ordinary classes provide a performance floor and value classes cannot go below it is unfounded.

A well-intentioned program may force the JVM to convert between flattened and reference representations when methods interact, which can negate benefits. The main optimization advantage of value classes is giving up identity, allowing the runtime to choose suitable representations, flatten values to avoid pointer chasing, and scalarize components in registers or on the stack. Escape analysis becomes trivial without the requirement of identity.

Immutability enables flattening; JEP 539, Strict Field Initialization, lets the JVM rely on a final field being initialized before its enclosing object becomes observable. Because such a field cannot later be updated, the JVM may use a non-atomic flattened layout without risking a torn assignment. Mutable fields must preserve tear-free assignment.

If a mutable field contains a value too large for an atomic flattened update, the JVM must use a reference layout. Consider a Four Longs record with 32 bytes of payload; too large for an atomic flattened update. But in an Envelope record, the payload is a strictly initialized final field, allowing a non-atomic flattened layout.

If Envelope were mutable, the layout reverts to REGULAR to maintain consistency.