Sub topic 3
Speculative Optimization and Uncommon Traps
Assume you know Java methods, interfaces, loops, bytecode, and the java command. This chapter focuses on what HotSpot does after it has collected runtime profiles.
Speculative optimization and uncommon traps
The JIT compiler uses observed behavior to make predictions. For example, if a virtual call has always received Square, C2 may inline Square.area() and guard the code with a type check. This is speculative optimization: the compiled code assumes a likely condition to run faster.
An uncommon trap is a deliberately rare exit from optimized code. If a Circle later reaches that call, the guard fails. HotSpot transfers execution to a recovery path and may invalidate the optimized method.
interface Shape {
double area();
}
static double areaOf(Shape shape) {
return shape.area(); // May be inlined for one common implementation
}Deoptimization: undoing a bad assumption
Deoptimization replaces optimized execution with interpreted or less-optimized execution. HotSpot reconstructs local variables, the operand stack, and the current bytecode position from compiler metadata. It can then continue without changing the program's result.
The method may later be compiled again using a broader profile. Deoptimization costs time, so frequent type changes or exception paths can reduce the benefit of speculation.
Compile queues and the code cache
The application thread usually does not perform the full compilation. It places a request in a compile queue. Background compiler threads process that queue while the program continues running. Too many requests can delay compilation.
Compiled native code is stored in the code cache. The cache also holds generated stubs and runtime support code. If it fills, HotSpot may stop compiling new methods, so code-cache capacity matters for large applications.
Inspecting, tuning, and measuring
Use diagnostic flags to observe behavior:
javac DeoptDemo.java
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompilation DeoptDemo
java -Xint DeoptDemo
java -XX:TieredStopAtLevel=1 DeoptDemo-Xint disables JIT compilation. TieredStopAtLevel=1 limits tiered compilation to C1. These are comparison tools, not normal production settings.
Measure after warmup. The first iterations include class loading, interpretation, profiling, compilation, and possible deoptimization. Use repeated forks and a framework such as JMH rather than timing one main invocation.
Beyond C1 and C2
JDK 11 also includes experimental AOT support through jaotc on supported platforms. AOT code is produced before startup, which can reduce startup work but limits adaptation to actual runtime profiles. Graal is an alternative compiler available through GraalVM or JVMCI configurations; it is not the default C2 compiler in a standard JDK 11 installation.
Exercise
Why can code compiled for one frequently observed implementation become invalid later?
Show answer
The compiler may have speculated that the observed type or branch would remain common. A new type or branch can violate that assumption, triggering deoptimization.