際際滷

際際滷Share a Scribd company logo
Introduction to JVMIntroduction to JVM
JIT OptimizationsJIT Optimizations
Diego Parra | @dpsoft | Core Team @ Kamon
AgendaAgenda
What is JIT?
Tiered Compilation
Optimizations
Conclusion
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?
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?
JIT LifecycleJIT Lifecycle
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
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
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
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
Common TransitionsCommon Transitions
Common TransitionsCommon Transitions
Common Transitions Patterns : /maddocig/tiered
Notes: https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/runtime/advancedThresholdPolicy.cpp
JIT OptimizationsJIT Optimizations
Compiler tacticsCompiler tactics
delayed compilation
tiered compilation
on-stack replacement
delayed reoptimization
program dependence graph representation
static single assignment representation
Speculative (profile-based)Speculative (profile-based)
optimistic nullness assertions
optimistic type assertions
optimistic type strengthening
optimistic array length
strengthening
untaken branch pruning
optimistic N-morphic inlining
branch frequency prediction
call frequency prediction
Proof-based techniquesProof-based techniques
exact type inference
memory value inference
memory value tracking
constant folding
reassociation
operator strength reduction
null check elimination
type test strength reduction
type test elimination
algebraic simpli鍖cation
common subexpression
elimination
integer range typing
Flow-sensitive rewritesFlow-sensitive rewrites
conditional constant propagation
dominating test detection
鍖ow-carried type narrowing
dead code elimination
Text
and much more..
Null check EliminationNull check Elimination
Null check EliminationNull check Elimination
In equivalent machine code
Null check EliminationNull check Elimination
In equivalent machine code
Null check EliminationNull check Elimination
In equivalent machine code
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Compilation Level
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Compilation Level
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Compilation Level
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Compilation Level
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+PrintCompilation
Compilation Level Deoptimizations
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintAssembly
-XX:CompileCommand=print,NullCheck::isPowerOfTwo
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintAssembly
-XX:CompileCommand=print,NullCheck::isPowerOfTwo
Someone said Assembly?
Null check EliminationNull check EliminationInto the rabbit hole
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintAssembly
-XX:CompileCommand=print,NullCheck::isPowerOfTwo
Someone said Assembly?
Null check EliminationNull check EliminationInto the rabbit hole
Null check EliminationNull check EliminationInto the rabbit hole
Null check EliminationNull check EliminationInto the rabbit hole
implicit exception: dispatches to 0x00007fd鍖5318274
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)
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
Method InliningMethod Inlining
Method InliningMethod Inlining
Method InliningMethod Inlining
-XX:+PrintInlining
Method InliningMethod Inlining
-XX:+PrintInlining
Method InliningMethod Inlining
-XX:+PrintInlining
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
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?
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?
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
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
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
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
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
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
On-Stack ReplacementOn-Stack Replacement
On-Stack ReplacementOn-Stack Replacement
On-Stack ReplacementOn-Stack Replacement
On-Stack ReplacementOn-Stack Replacement
-XX:+PrintCompilation -XX:+PrintInlining
On-Stack ReplacementOn-Stack Replacement
-XX:+PrintCompilation -XX:+PrintInlining
On-Stack ReplacementOn-Stack Replacement
-XX:+PrintCompilation -XX:+PrintInlining
Compilation is OSR
Dead Code EliminationDead Code Elimination
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
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
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
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
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
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!
Escape AnalysisEscape Analysis
Escape AnalysisEscape Analysis
No synchronization lock when calling getNoEscapeSum method
Escape Analysis for Java: http://www.research.ibm.com/people/j/jdchoi/escape.ps
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
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
Loop UnrollingLoop Unrolling
Loop UnrollingLoop Unrolling
Loop UnrollingLoop Unrolling
Unrolled!
IntrinsicsIntrinsics
Intrinsics: https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/class鍖le/vmSymbols.hpp#L603
Special code build-into the VM for a particular method
Often use special hardware capabilities
Not writen in Java
JITWatchJITWatch
Jitwatch: https://github.com/AdoptOpenJDK/jitwatch
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
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
ResourcesResources
Java Performance: http://shop.oreilly.com/product/0636920028499.do
Optimizing Java: http://shop.oreilly.com/product/0636920042983.do
Aleksey Shipil谷v (blog): https://shipilev.net/
Nitsan Wakart (blog): http://psy-lob-saw.blogspot.com.ar/
JVM Source Code: https://github.com/dmlloyd/openjdk
Open JDK Wiki: https://wiki.openjdk.java.net/dashboard.action
Questions?Questions?

More Related Content

Introduction to JVM JIT Optimizations