Sub topic 3
The Two Compilers: C1 and C2
This chapter assumes you know basic Java classes, methods, loops, and interfaces. You should also know that javac produces bytecode and that the JVM runs this bytecode.
HotSpot uses two just-in-time (JIT) compilers. A JIT compiler translates frequently executed bytecode into native machine code while the program is running:
- C1, the client compiler, compiles quickly and produces moderately optimized code.
- C2, the server compiler, takes longer but applies more expensive optimizations to important, long-running code.
The JVM uses both compilers to balance startup time and peak performance.
Why HotSpot uses two compilers
Compiling every method with C2 would delay application startup. Many methods run only once or a few times, so spending significant compilation time on them would waste work.
Compiling everything with C1 would reduce compilation delay, but it would leave performance on the table for methods that run millions of times.
C1 provides useful native code early. C2 later uses runtime profiling data to produce more aggressively optimized code.
| Compiler | Main goal | Compilation cost | Typical result |
|---|---|---|---|
| C1 | Fast compilation and early speedup | Lower | Moderate optimization |
| C2 | Maximum performance for hot code | Higher | Aggressive optimization |
C1: fast compilation
C1 is designed for quick compilation. It can use profiling information, such as which branches execute often, but it generally performs fewer and less costly transformations than C2.
C1 is useful when:
- The application needs good startup performance.
- A method becomes hot, but its future behavior is still uncertain.
- The method may not run long enough to justify C2 compilation.
- The JVM needs compiled code while C2 is still working.
C1 can also add profiling instructions to compiled code. This information helps C2 understand the program's actual behavior.
C2: aggressive optimization
C2 targets methods that remain hot. It spends more time analyzing control flow, types, loops, and method calls. It can then apply optimizations such as:
- Inlining small method calls.
- Removing calculations whose results are never used.
- Optimizing loops.
- Eliminating some object allocations.
- Specializing code based on observed runtime types.
These optimizations depend on assumptions gathered during execution. If the assumptions later become false, HotSpot can deoptimize the method and return execution to a safer version.
How they work together
With tiered compilation enabled, a method commonly starts in the interpreter, moves to C1, and may later move to C2.
flowchart LR
I[Interpreter] --> C1[C1 compiled code]
C1 --> C2[C2 optimized code]
C2 --> D[Deoptimization if an assumption fails]
D --> IThe exact transition depends on invocation counts, loop activity, profiling data, and compiler availability. C1 is not simply a lower-quality replacement for C2. It is an early, lower-cost stage that helps the JVM respond quickly while it gathers better evidence.
public class CompilerChoice {
static long work(int value) {
return (long) value * value + 1;
}
public static void main(String[] args) {
long total = 0;
for (int i = 0; i < 50_000_000; i++) {
total += work(i % 1000);
}
System.out.println(total);
}
}Compile and run it with:
javac CompilerChoice.java
java -XX:+PrintCompilation CompilerChoiceThe output can show work and main compiled at different levels. Later chapters explain how to read those levels.
Exercise
Why does HotSpot usually prefer C1 before C2 for a method that becomes hot?
Show answer
C1 compiles quickly and provides useful native code while the JVM gathers profiling data and determines whether the method deserves C2's more expensive optimization.