Skip to content

Week 6 - The JIT: C1, C2, Graal, Tiered Compilation

Conceptual Core

HotSpot runs your bytecode in an interpreter at first, profiles it, then compiles hot methods with C1 (fast, dumb) and very hot ones with C2 (slow, smart). This is tiered compilation. Graal is an alternative C2 written in Java itself. Almost every "Java is slow" claim dies on first contact with steady-state JIT output.

Mechanical Detail

  • Compilation tiers 0–4. Read -XX:+PrintCompilation, -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining.
  • Inlining heuristics: small methods, monomorphic call sites, profile-guided. The infamous MaxInlineLevel (default 9) and MaxInlineSize (default 35 bytes).
  • Escape analysis and scalar replacement - the JIT can prove an object doesn't escape and stack-allocate (effectively) by inlining its fields into locals. This is why "premature optimization" advice exists: most allocations the JIT eliminates anyway.
  • Deoptimization - when a speculation fails (a call site that was monomorphic gets a second type), C2 throws away its compiled code and falls back to the interpreter. -XX:+PrintCompilation shows made not entrant / made zombie.
  • JVMCI and Graal: enable with -XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler -XX:+EnableJVMCI.
  • Project Leyden (in flight) - AOT condensers, ahead-of-time class loading + linking + (eventually) compilation, in the OpenJDK proper. Watch JEPs 483, 514, 515.
  • GraalVM native-image - full AOT to a native binary. No JIT, no warmup, ~10x smaller. Trade: reflection/dynamic proxies need explicit hints, peak throughput is often lower than warmed-up C2.

Lab

Write a polymorphic dispatch site (Shape.area() over 1 / 2 / 3 implementations). Run with -XX:+PrintInlining for each cardinality. Observe monomorphic → bimorphic → megamorphic transitions. Reproduce a deopt by introducing a 4th type after warmup.

Idiomatic Drill

Read Cliff Click's "A Crash Course in Modern Hardware" talk. Understand why "what the JIT can do" depends on what the CPU can do.

Production Hardening Slice

Build the same simple Spring Boot endpoint two ways: standard JVM and GraalVM native-image. Measure: cold start, p50/p99 latency under load, RSS, image size. Document the trade in a one-pager.

Comments