JDK 28 已将 JEP 401 作为预览特性集成,这是 Valhalla 项目的重要里程碑 1。值类(value classes)通过放弃对象身份,为 JVM 提供了更多优化自由 1。此外,JEP 539(JVM 中的严格字段初始化)允许 JVM 依赖 final 字段在封闭对象可观察之前已被初始化 1。
然而,在泛型类型擦除导致的多态调用等场景下,值类仍需编译器配合才能避免性能退化 1。文章通过三个示例展示了当前 JVM 对值类的优化能力及局限 1。例如,FourLongs 包含 32 字节有效载荷,对于当前 JVM 中的原子扁平化更新来说过大;且在 megamorphic 调用场景下,每次 reproduce 调用会分配 192 字节 1。针对这一局限,解决方案是在 Frobber 接口中显式重新声明类型化方法,从而将分配减少到零 1。编译和运行示例需使用 --enable-preview 和 --release 28 选项 1。
JEP 401, representing a major milestone for the Valhalla project, has been integrated as a preview feature in JDK 28 1. This update introduces value classes, which grant the JVM increased optimization flexibility by abandoning object identity 1. Alongside this, JEP 539 implements strict field initialization within the JVM, enabling the system to depend on final fields being initialized before the enclosing object becomes observable 1.
While these features offer benefits, value classes still require compiler sympathy to avoid performance degradation in certain scenarios, such as polymorphic calls caused by generic type erasure 1. An examination of three examples illustrates the current optimization capabilities and limitations of the JVM regarding value classes 1. Specifically, FourLongs has a 32-byte payload, which is too large for atomic flattened updates in the current JVM 1. Moreover, in megamorphic call scenarios, each reproduce call allocates 192 bytes 1. However, developers can reduce this allocation to zero by explicitly redeclaring typed methods within the Frobber interface 1. Compiling and running these examples requires the --enable-preview and --release 28 options 1.
评论
还没有评论,欢迎留下第一条。