Sub topic 10
Faster warmup with cached method profiles
This chapter assumes you can build and use an AOT cache with the record, create and run steps, and that you know the JVM's tiers at a high level: the interpreter, then the C1 and C2 JIT compilers. It requires JDK 25 or later.
The cost the class cache does not remove
The AOT cache from earlier chapters removes class loading, verification and linking from each run. A second cost remains. After a class loads, the JVM still has to learn which code is hot.
A fresh JVM starts by interpreting bytecode. While it does, it collects a method profile: invocation counts, branch outcomes, and which concrete types appear at call sites. The JIT compiler uses the profile to decide what to compile and how to optimize it. A call shape.area() that has only ever seen Circle can be compiled as a direct call. A branch that is never taken can be dropped from the compiled code.
Collecting that profile takes time. The application runs slower than it will later, because the JIT has not yet seen enough behavior to generate good code. This period is called warmup.
What JEP 515 changes
JEP 515, "Ahead-of-Time Method Profiling", adds the method profiles from the training run to the AOT cache. On the production run the JVM reads them at startup. The JIT compiler can start from the profile that a previous run collected, instead of waiting to build its own.
The cache stores profile data only. It does not store compiled machine code, so the JIT still compiles your hot methods in every run. It simply starts compiling with better information and can compile sooner.
Using it
There is no new flag. When you create the cache with JDK 25 or later, the profiles collected during the training run go into the cache with the class data. Use the one-step command from the previous chapter.
This program has a polymorphic call site and a hot loop, and it prints how long each batch takes.
public class Warmup {
interface Shape { double area(); }
record Circle(double r) implements Shape {
public double area() { return Math.PI * r * r; }
}
record Square(double s) implements Shape {
public double area() { return s * s; }
}
public static void main(String[] args) {
Shape[] shapes = { new Circle(1), new Square(2) };
for (int batch = 0; batch < 5; batch++) {
long start = System.nanoTime();
double sum = 0;
for (int i = 0; i < 200_000; i++) {
sum += shapes[i % 2].area();
}
long micros = (System.nanoTime() - start) / 1_000;
System.out.printf("batch %d: %d us (sum=%.1f)%n", batch, micros, sum);
}
}
}Build the cache with a training run, then compare runs with and without it:
javac Warmup.java
jar --create --file warm.jar --main-class Warmup *.class
# Training run: records classes and method profiles, then writes the cache
java -XX:AOTCacheOutput=warm.aot -cp warm.jar Warmup
# Without the cache
java -cp warm.jar Warmup
# With the cache
java -XX:AOTCache=warm.aot -cp warm.jar WarmupCompare the first batches in each output. With the cache, the early batches should be closer to the later ones. A program this small shows a modest difference, and timings vary between runs. Repeat each command several times and compare typical values, not a single run.
Where the gain shows up
| Situation | Expected benefit |
|---|---|
| Short-lived tools and command-line programs | Class cache helps most; profiles add a little |
| Services that reach peak speed after seconds of traffic | Profiles shorten the slow period before peak |
| Long-running programs that run for hours | Little effect on total performance |
Peak throughput is not the target. A long-running application ends up with the same optimized code either way. The change is in how quickly it gets there.
Gotchas
- The training run shapes the profile. If the training run exercises only the
Circlebranch, the stored profile saysCircleis the only type. If production usesSquare, the JIT's early assumptions are wrong. The JVM detects this and recompiles (it deoptimizes), so the program stays correct, but you lose part of the gain. Train with realistic traffic. - Short training runs give thin profiles. A training run that exits after a few milliseconds records little. Keep it running long enough for the hot paths to execute many times.
- The cache must still be valid. If the JVM rejects the cache, as described in the validation chapter, you lose the profiles together with the class data. Use
-XX:AOTMode=onto make this fail loudly.
Exercise
A team builds the AOT cache by running only a health-check endpoint during training. Their production traffic uses many other code paths. What happens to the method profiles, and what should they change?
Show answer
The cache holds profiles that describe only the health-check paths, so the JIT gets little useful information for the code that production actually runs. That code is profiled and compiled as it would be without the cache. Where the stored profile is wrong, the JVM deoptimizes and recompiles, so the program stays correct but the warmup gain is small. They should drive the training run with representative traffic that exercises the main request paths, and keep it running long enough for those paths to become hot.