DEV Community

Cover image for Ahead of Time Code Compilation (JEP 544)
María Arias de Reyna
María Arias de Reyna

Posted on

Ahead of Time Code Compilation (JEP 544)

In this article we are going to deep dive into JEP 544 and how compiled code inside the Ahead-of-Time (AOT) cache is dramatically improving application startup and warm-up times.

Project Leyden did already introduce the concept of AOT Cache in JDK 24. The concept is simple: during a training run, the application is executed under a representative workload to produce an artifact containing bytecode, build-time metadata, selected heap objects, and runtime data like profiling data. Then the app runs in production using this cache, so the application starts partially warmed up.

If you want more details on how Project Leyden worked before JEP 544, you can check this three-article series.

JEP 544 – Ahead-of-Time Code Compilation (JDK 28+)

The major milestone for Leyden this year has been JEP 544: Ahead-of-Time Code Compilation. As the name suggests, compiled native code is now directly stored into the AOT cache. This means that optimized native machine code is immediately available upon application startup.

But what does this really mean?

Running code in the JVM

The following is a simplified explanation of a very complex process, if you want the full explanation you can always check the source code, which has a very good explanation in comments.

Usually, code that runs in the JVM starts by being in bytecode, from a class file. To generate code that can run on your hardware, the JVM has the JIT and two compilers (C1 and C2) that can generate native code.

When some method gets called for the first time, the JIT quickly translates that bytecode into native code. This is a very fast process, because it literally just translates each bytecode instruction into a pre-defined set of native instructions. This is what we call Tier 0. No optimization, no smart compilation, just blind translation.

Once the code runs, the JVM starts to save some information about that code. Does it have dead-code parts? What branches are being used more often? Can the code be reorganized to be more efficient? And every single time the method gets called, there is a counter that increases. After the method has been called enough times, it is considered hot, and therefore a good candidate to be optimized using the profile data saved during previous executions.

To compile using the profile data, it checks first how many other methods are waiting to be compiled. If the C2 compiler is busy, the C1 compiler will recompile the method into Tier2, which produces better code than Tier0, but has a rather fast compilation. If the C2 compiler is free, the C1 compiler will spend more time recompiling the method into Tier3, which has many more optimizations. Both Tier2 and Tier3 code will keep saving profiling statistics to improve future compilations.

Compilation Code diagram in the JVM

At some point, the method may be called so much that the JVM will consider another recompilation, with much more profiling information, to further optimize its execution. If the code is deemed trivial, then C1 will just compile into Tier1, which is a basic compilation with no fancy optimizations. The method will stay in Tier1 with no more changes and the compilers can forget about it. There's no reason to try to optimize a trivial code.

If it is not considered trivial, that’s when C2 will compile the method with the best optimizations available. On this Tier4 level, we no longer save profiling statistics, to avoid any overhead. This is peak optimized code, the JVM can’t make it better.

Some hot methods will be de-optimized into Tier3 again every now and then to gather new statistics and make sure the code is still optimally compiled. This makes a lot of sense in case the application circumstances change. For example, if you have a specific code for Black Friday, how the method behaves during Black Friday will be completely different than any other day of the year.

How does this translate to Leyden?

Before JEP 544, the AOT Code Cache stored profiling data and method counters. This means that we have already marked which methods are going to be hot and we already have the profiling information needed to compile them. We can quickly compile those methods and jump into higher tiers of compilation faster. The compiler threads will be busier during startup, but peak performance is reached sooner.

After JEP 544, the AOT Cache does not only store profiling data, which is still useful, but now it also stores actual compiled code in whatever tier they reached during the training run. That means that we can not only skip most of the startup time, but execute methods directly at a higher tier of compilation and reduce warm-up time.

Performance Benchmarks & Data Analysis

Let’s see now how good the AOT Cache improves warm-up by comparing response times from two different examples.

These tests were conducted using leyden-perf-test, driven by QDup and Hyperfoil on my laptop with standard hardware CPU pin settings to minimize noise. Which means, numbers may not be as good as if it was a dedicated server, but the trends are very clear.

Simple REST

First, take a look at a very simple REST endpoint using Quarkus that returns a hello world string. This is simple code, the JVM should be able to optimize it quickly.

As we can see in the following histogram graphs, with the AOT Cache we already start with a response time three times faster and we quickly reach peak performance before the request number 400, whereas the application with no cache is still optimizing after request 700.

Histogram of response times with no AOT

Histogram of response times with AOT

We can even see in the test with AOT cache available, that around request 550 there is some de-optimization that affects response time, and quickly disappears to optimize again. This is normal behaviour that would also happen in the run without cache, we just don't see it on the graph because we are still busy reaching peak performance.

We could be more nit-picky and look closer when is peak performance reached. Probably there is some improvement beyond what we can see from these graphs if we zoom in, and we may need to reach a thousand requests to have almost-instantaneous response times. But the general trend can be observed already: with the cache, startup response times are faster from the very first response, and warm-up comes sooner.

Simple Hibernate

The following test is very similar to the previous one, but instead of returning a hello world string, it has to retrieve the value from a local database. As the codebase is more complex, the initial response times are higher than in the previous example, and the warm-up takes longer.

We can see that the initial response times with the cache are close to one fourth of the original response time without cache. And while the run with the cache reaches peak performance before request number 900, the application with no cache has almost the same response time after request 1000, it is still busy profiling methods before optimizing them.

Histogram of response times with no AOT

Histogram of response times with AOT

These are just two examples, which may be considered even trivial examples. But the trend can be already seen: the more complex the program, the more the cache can improve startup and warm-up times compared to using no cache.

We will dive deeper in future articles with more examples and real use case scenarios.

If you are trying to add the AOT Cache to your deployments, I am interested in hearing from you. It doesn't matter if it was a good or a bad experience. Specially, bad experiences can help us make Leyden easier for everyone (including you).

Top comments (0)