The document discusses topics related to just-in-time (JIT) compilation in the Java Virtual Machine (JVM). It explains that the JIT compiler is triggered when methods are invoked many times based on invocation and back-edge counters, compiling frequently used code to machine code for improved performance. It also discusses why ahead-of-time compilation is not well-suited for Java due to its dynamic nature, and what can cause compiled code to be deoptimized, such as class initialization failures, null pointer exceptions, and unstable control flow.
5. 01
public class SimpleProgram {
static int blackhole;
public static void main(String[] args) {
int[] nums = randomInts(5_000);
for ( int i = 0; i < 100; ++i ) {
long startTime = System.nanoTime();
blackhole = sum(nums);
long endTime = System.nanoTime();
System.out.printf("%dt%d%n", i, endTime - startTime);
}
}
…
}
16. BUT WHEN?
BACKEDGE (LOOP) COUNTER
Counter > Backedge Threshold
Hot Loops
Counter > Invocation Threshold
Hot Methods
Invocation + Backedge Counters > Compile Threshold
Medium Hot Methods with Medium Hot Loops
INVOCATION COUNTER
29. MyClass.getStatic()
if ( !vm.is_init(MyClass)) {
vm.init(MyClass);
}
MyClass.getStatic()
Bloats size by 20%
Slows down by 5-10%
INITIALIZATION CHECKING
http://joeduffyblog.com/2015/12/19/safe-native-code/
30. JIT RUNS LATE
MOST CLASSES ARE ALREADY INITIALIZED
MyClass.getStatic()
MyClass.getStatic()
vm.is_init(MyClass)
initialized
uncommon_trap(:uninitialized)
uninitialized
At compile time…
32. UNINITIALIZED FOREVER
public class UninitializedForever {
public static void main(String[] args) {
for ( int i = 0; i < 10_000_000; ++i ) {
try {
new Uninitialized();
} catch ( Throwable t ) {
// ignore
}
}
}
}
static class Uninitialized {
static {
if ( true ) {
throw new RuntimeException();
}
}
}
02
43. UNREACHED / UNSTABLE IF
public static volatile Object thing = null;
public static void main(final String[] args) {
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
Thread.sleep(5_000); // wait for JIT
thing = new Object();
for ( int i = 0; i < 20_000; ++i ) {
hotMethod();
}
Thread.sleep(5_000); // wait for JIT again
}
static final void hotMethod() {
if ( thing == null )
System.out.print("");
else
System.out.print("");
}
04