This document provides an introduction to JIT (Just-In-Time) optimizations in the JVM. It discusses how JIT compilers like C1 and C2 work, how tiered compilation balances startup time and performance. It also summarizes various JIT optimizations like inlining, escape analysis, dead code elimination and more. The document dives into examples and techniques used to perform optimizations like null check elimination. It concludes by recommending tools like JMH for benchmarking and exploring JVM internals through options, assembly code and other resources.
Convert to study materialsBETA
Transform any presentation into ready-made study materialselect from outputs like summaries, definitions, and practice questions.
3. JITJIT
Just-In-Time compilation
Compilation work happens during application execution
JIT'ing requires Pro鍖ling for more e鍖cient optimization
Because you don't want JIT everything
Aggressively optimize based on pro鍖le information
Tiered compilation
Runtime Overhead?
4. JITJIT
Just-In-Time compilation
Compilation work happens during application execution
JIT'ing requires Pro鍖ling for more e鍖cient optimization
Because you don't want JIT everything
Aggressively optimize based on pro鍖le information
Tiered compilation
Runtime Overhead?
6. JIT CompilersJIT Compilers
C1 Client C2 ServerVS
Fast compiler
Invocation count > 1500
Produces Compilations quickly
Generated code runs Slow
Pro鍖le is about counters: https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/oops/methodCounters.hpp
Smart compiler
Invocation count > 10000
Produces Compilations slowly
Generated code runs Fast
10x faster than Interpreter 2x faster than C1
7. JIT CompilersJIT Compilers
C1 Client C2 ServerVS
Fast compiler
Invocation count > 1500
Produces Compilations quickly
Generated code runs Slow
Pro鍖le is about counters: https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/oops/methodCounters.hpp
Smart compiler
Invocation count > 10000
Produces Compilations slowly
Generated code runs Fast
Pro鍖led Guided
Speculative
10x faster than Interpreter 2x faster than C1
8. Tiered CompilationTiered Compilation
Available in Java 7
Default in Java 8
Client Startup
Server Performance
Tiered : https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/runtime/advancedThresholdPolicy.hpp#L35-L158
9. Tiered CompilationTiered Compilation
Available in Java 7
Default in Java 8
Client Startup
Server Performance
Best of Both Worlds - C1 + C2
Tiered : https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/runtime/advancedThresholdPolicy.hpp#L35-L158
28. Null check EliminationNull check EliminationInto the rabbit hole
implicit exception: dispatches to 0x00007fd鍖5318274
29. Null check EliminationNull check Elimination
JVM usesJVM uses SIGSEGVSIGSEGV as control flowas control flow
Into the rabbit hole
Handle Signals: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/signals001.html#CIHBBDED
Speculative OptimizationSpeculative Optimization
FasterFaster IFIF not null are encountered (Uncommon Trap)not null are encountered (Uncommon Trap)
30. Null check EliminationNull check Elimination
JVM usesJVM uses SIGSEGVSIGSEGV as control flowas control flow
Into the rabbit hole
Handle Signals: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/signals001.html#CIHBBDED
Speculative OptimizationSpeculative Optimization
FasterFaster IFIF not null are encountered (Uncommon Trap)not null are encountered (Uncommon Trap)
Implicit Null Check?
Field and Array access is null checked
36. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
37. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
38. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
39. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Monomorphic
40. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Monomorphic Bimorphic
41. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Monomorphic Bimorphic Megamorphic
42. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Black Magic: https://shipilev.net/blog/2015/black-magic-method-dispatch/
Monomorphic Bimorphic Megamorphic
43. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Black Magic: https://shipilev.net/blog/2015/black-magic-method-dispatch/
Monomorphic Bimorphic Megamorphic
Class Hierarchy Analysis
44. Method InliningMethod Inlining
Inlining is the most important optimization
Combine caller and callee into one unit
Expands the scope for other optimizations
Bytecode size < 35 bytes
Bytecode size < 325 bytes for inlining hot methods
What about virtual call inlining?
Black Magic: https://shipilev.net/blog/2015/black-magic-method-dispatch/
Monomorphic Bimorphic Megamorphic
Class Hierarchy Analysis
Devirtualization
52. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
53. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
54. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
55. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
56. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
57. Dead Code EliminationDead Code Elimination
Dead-Code: http://hg.openjdk.java.net/code-tools/jmh/鍖le/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
Result is not used and the entire method is optimized away!
59. Escape AnalysisEscape Analysis
No synchronization lock when calling getNoEscapeSum method
Escape Analysis for Java: http://www.research.ibm.com/people/j/jdchoi/escape.ps
60. Escape AnalysisEscape Analysis
No synchronization lock when calling getNoEscapeSum method
No allocate a Sum object at all, just keep track of the individual
鍖elds of the object Escape Analysis for Java: http://www.research.ibm.com/people/j/jdchoi/escape.ps
61. DeoptimizationDeoptimization
The compiler can back to previous versions of compiled code
E鍖ect in performance when code is deoptimized
Deoptimize when previous optimizations are no longer valid
There are two cases of deoptimization
not entrant -> don't let new calls enter
zombi -> on this way to deadness
67. ConclusionConclusion
Please use JMH, Don't lie to yourself
Code will be Optimized and De-optimized
JVM require Warmup
JVM is Smart
Keep It Simple, Stupid!, is perfect for JIT to make his job
68. ConclusionConclusion
Please use JMH, Don't lie to yourself
Code will be Optimized and De-optimized
JVM require Warmup
JVM is Smart
Keep It Simple, Stupid!, is perfect for JIT to make his job
Learn Assembly