This article discusses preview features in JDK 28, particularly focusing on JEP 401, a significant milestone for Valhalla, which introduces value classes. These classes enhance the ability to communicate program semantics and optimize performance in the Java Virtual Machine (JVM).
The author expresses concern over a misconception that value classes will always improve performance compared to ordinary classes. They clarify that while value classes can optimize certain operations, they may also lead to performance issues if not used correctly. The JVM can choose different representations for value classes, which can lead to performance variations depending on the context of their use.
The article outlines the optimization advantages of value classes, such as the lack of identity, which allows the JVM to flatten values and optimize memory usage. It presents examples of how the JVM handles different scenarios with value classes, including the implications of strict field initialization and the performance characteristics of various data structures.
The author highlights a case where converting records to value classes led to unexpected performance regressions, prompting an investigation into the compiler's behavior. They explain how generics in Java can complicate optimizations and how the JVM's handling of method calls can affect performance.
The article concludes by emphasizing the importance of understanding the compiler's capabilities and limitations when working with value classes, suggesting that developers should consider the semantic implications of declaring a class as a value class. The author encourages readers to inspect the generated assembly code to verify the performance characteristics discussed.
For further exploration, the author provides instructions on how to compile and analyze the code snippets mentioned in the article.