Project Valhalla, Explained: How a Decade of Work Arrives in JDK 28
4 hours ago
- #Performance
- #JVM
- #Java
- Project Valhalla's JEP 401, introducing value classes and objects, has been integrated into OpenJDK and is targeting JDK 28 as a preview feature.
- Value classes enable 'codes like a class, works like an int'—allowing user-defined types to be memory-efficient like primitives while retaining class-like features such as methods and constructors.
- The key distinction is the absence of identity in value objects, changing == to compare substitutability (field equality) rather than identity, and disallowing synchronization.
- Performance improvements include scalarization (breaking down objects into fields during JIT compilation) and heap flattening (storing objects densely in arrays/fields), enhancing cache locality.
- JDK 28's preview includes value class declarations, migration of JDK value-based classes (e.g., Integer), and enhanced boxing, but excludes specialized generics and non-nullable types.
- Specialized generics, which would allow flat collections like ArrayList<Point>, are planned for future releases and require addressing type erasure limitations.
- The development involved over a decade of iteration, including discarded concepts like 'primitive classes' with dual projections, before settling on the current model.
- Practical impact: High-performance domains (e.g., data processing, gaming) can achieve dense memory layouts without sacrificing abstraction, but full benefits depend on future enhancements.
- Backward compatibility is maintained for most code, except for identity-dependent operations (e.g., synchronization, == on Integers), which may require adjustments.
- The feature represents a foundational shift in Java's object model, moving away from the assumption that all objects have identity, enabling long-term optimizations.