Sub topic 12
Deoptimization: Undoing a Bad Assumption
This chapter assumes basic Java programming, familiarity with JVM bytecode, and comfort running javac and java from the command line.
The HotSpot JIT compiler uses runtime information to make code faster. It may learn that a method usually receives objects of one class, that a branch is almost never taken, or that a call has one common target. The compiler can then generate specialized machine code based on those observations.
These observations are predictions, not guarantees. When a prediction becomes false, HotSpot uses deoptimization. Deoptimization transfers execution from optimized machine code back to the interpreter or to less-specialized compiled code.
A speculative optimization
Consider a method that calls an interface method:
public class DeoptDemo {
interface Shape {
double area();
}
static final class Square implements Shape {
public double area() {
return 4.0;
}
}
static final class Circle implements Shape {
public double area() {
return 3.0;
}
}
static double run(Shape shape) {
return shape.area();
}
public static void main(String[] args) {
double total = 0;
Shape square = new Square();
for (int i = 0; i < 5_000_000; i++) {
total += run(square);
}
Shape circle = new Circle();
for (int i = 0; i < 5_000_000; i++) {
total += run(circle);
}
System.out.println(total);
}
}During the first loop, run sees only Square. The JIT may specialize the call to Square.area() and avoid a general interface dispatch. The generated code includes a guard that checks whether the object still has the expected type.
The second loop passes a Circle. The guard fails. This failure is an uncommon trap, a controlled transfer from optimized code to a recovery path.
What happens during deoptimization
HotSpot records enough metadata to reconstruct the Java state at a safe point. This metadata describes values such as local variables, operand-stack entries, and the current method position.
When the assumption fails, HotSpot can:
- Stop using the specialized machine-code path.
- Reconstruct the method's logical Java state.
- Continue in the interpreter or another compiled version.
- Record the new behavior in its profile.
- Compile a more general version if the method becomes hot again.
The original compiled code may be marked not entrant, meaning that new calls must not enter it. Existing executions leave it through a safe recovery path. This is different from a Java exception. Deoptimization is an internal performance mechanism and normally does not change the program's result.
Observing the transition
Compile and run the example with compilation logging:
javac DeoptDemo.java
java -XX:+PrintCompilation DeoptDemoThe output varies by JDK build, workload, and machine. You may see a method compiled more than once. A previous version can later be marked made not entrant, which indicates that HotSpot stopped accepting new entries into that compiled version.
A single deoptimization is not automatically a problem. It becomes costly when an application repeatedly alternates between different types or branch patterns. Such behavior can cause recompilation and extra transitions between compiled and interpreted code.
Exercise
Why can a method be compiled again after it has already been compiled once?
Show answer
Its earlier compilation may depend on an assumption that later becomes false. HotSpot deoptimizes that version, gathers new profile data, and may compile a more general or differently optimized version.