Sub topic 10
Inlining and the Optimizations It Unlocks
This chapter assumes that you know basic Java syntax, JVM bytecode, and the idea that HotSpot compiles frequently executed code at run time. It also assumes that you have seen the C1 and C2 compiler tiers.
Inlining is a JIT optimization that replaces a method call with the method's body. For example, instead of generating a call to square(x), the compiler can place x * x directly inside the caller. The result is still logically the same, but the compiler can see more of the surrounding code.
public class InlineDemo {
static int square(int value) {
return value * value;
}
static long sumSquares(int count) {
long total = 0;
for (int i = 0; i < count; i++) {
total += square(i % 1000);
}
return total;
}
public static void main(String[] args) {
System.out.println(sumSquares(50_000_000));
}
}Compile and run the example with:
javac InlineDemo.java
java InlineDemoAfter square is inlined into sumSquares, C2 can optimize the combined code. It may remove some call overhead, propagate values between expressions, simplify branches, eliminate unused work, and keep values in registers. Inlining does not guarantee every one of these optimizations. It exposes opportunities that would be hidden when the methods remain separate.
How HotSpot decides to inline
Inlining uses compiler heuristics rather than a simple rule such as “inline every small method.” HotSpot considers factors such as:
- The method's bytecode size.
- How often the call site executes.
- Whether the call is recursive.
- Whether the call target is known.
- Whether the compiled method has enough code-cache space.
- Whether earlier inlining has already made the caller large.
A small, frequently called method is a strong candidate. A large method may not be inlined even if it runs often, because copying its body can make the caller slower to compile and harder to optimize.
The C1 compiler performs faster, simpler inlining. C2 spends more time using profiling data to make deeper decisions. The final result can depend on the current tier, available compiler resources, and the program's observed behavior.
Virtual calls and type profiles
An interface or virtual method call can have several possible targets:
interface Operation {
int apply(int value);
}
static int calculate(Operation operation, int value) {
return operation.apply(value);
}If profiling shows that calculate receives only one implementation, C2 can use a guarded, speculative inline. It checks that the object has the expected type and then executes the inlined method body. This is called a type guard or guarded devirtualization.
If several implementations are common, HotSpot may inline a few targets, use a type test, or leave the call as a normal virtual call. If the call site is too unpredictable, inlining becomes less useful.
Inlining is also connected to deoptimization. A speculative inline may assume that one class dominates a call site. If another class later appears, HotSpot can invalidate the compiled assumption, return to a safe interpreter or compiled frame, and compile a new version.
Exercise
Why can inlining improve an optimization that is not inside the inlined method itself?
Show answer
Inlining exposes the method's expressions to the caller, so the compiler can optimize across the former method boundary. It can combine calculations, remove branches, and eliminate unnecessary values.