The OpenJDK community has advanced Java performance with the introduction of JEP 544, which implements ahead-of-time (AOT) code compilation to improve application startup and warmup times. This enhancement allows the HotSpot Java Virtual Machine (JVM) to load optimized native code immediately at startup, bypassing much of the traditional just-in-time (JIT) compilation overhead.
The process involves running a training phase where the application code is compiled into native code and stored in an AOT cache. Subsequent production runs can then use this cached code to achieve near-peak performance instantly. If the workload changes during production, HotSpot can dynamically recompile methods using JIT compilation to maintain optimal performance, combining the benefits of both AOT and JIT approaches.
This feature requires no changes to application code or major HotSpot configuration adjustments beyond enabling the AOT cache. It supports common garbage collectors and targets AArch64 and x64 architectures initially.
Java applications typically experience a startup phase where code is interpreted and profiled, followed by a warmup phase where frequently used methods are compiled to optimized native code. JEP 544 shifts much of this compilation work to an earlier training run, reducing the time spent in slower interpreted or partially optimized states during production.
While static compilation has been proposed as an alternative, it lacks the adaptability of dynamic compilation, which can respond to changing workloads and diverse runtime environments. JEP 544's approach preserves Java's dynamic features such as dynamic class loading and reflection while delivering faster startup.
Performance benchmarks show that using the AOT cache with compiled code can reduce startup times by up to 80% and significantly shorten warmup periods, enabling applications to reach steady-state performance more quickly.
The AOT cache stores both pre-linked classes and profiling data alongside the compiled native code. HotSpot ensures compatibility by verifying CPU architecture and garbage collector consistency between training and production runs. If mismatches occur, the JVM falls back to traditional JIT compilation.
Developers can generate and use AOT caches with familiar command-line options, and diagnostic tools are available to monitor AOT code usage and manage cache behavior.
Future work includes exploring configurations that minimize runtime interpretation and JIT compilation, tuning compiler options for AOT scenarios, and potentially enhancing portability across processor variants.
By integrating ahead-of-time compilation into the HotSpot JVM, OpenJDK aims to deliver faster startup and improved responsiveness for Java applications, particularly benefiting environments like microservices where quick initialization is critical.