Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to JVM JIT Optimizations

Avatar for dpsoft dpsoft
September 21, 2018

Introduction to JVM JIT Optimizations

Avatar for dpsoft

dpsoft

September 21, 2018

More Decks by dpsoft

Other Decks in Technology

Transcript

  1. JIT JIT Just-In-Time compilation Compilation work happens during application execution

    JIT'ing requires Profiling for more efficient optimization Because you don't want JIT everything Aggressively optimize based on profile information Tiered compilation Runtime Overhead?
  2. JIT JIT Just-In-Time compilation Compilation work happens during application execution

    JIT'ing requires Profiling for more efficient optimization Because you don't want JIT everything Aggressively optimize based on profile information Tiered compilation Runtime Overhead?
  3. JIT Compilers JIT Compilers C1 Client C2 Server VS Fast

    compiler Invocation count > 1500 Produces Compilations quickly Generated code runs Slow Profile 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
  4. JIT Compilers JIT Compilers C1 Client C2 Server VS Fast

    compiler Invocation count > 1500 Produces Compilations quickly Generated code runs Slow Profile 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 Profiled Guided Speculative 10x faster than Interpreter 2x faster than C1
  5. Tiered Compilation Tiered 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
  6. Tiered Compilation Tiered 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
  7. Common Transitions Common Transitions Common Transitions Patterns : https://www.slideshare.net/maddocig/tiered Notes:

    https://github.com/dmlloyd/openjdk/blob/jdk8u/jdk8u/hotspot/src/share/vm/runtime/advancedThresholdPolicy.cpp
  8. JIT Optimizations JIT Optimizations Compiler tactics Compiler 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 techniques Proof-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 simplification common subexpression elimination integer range typing Flow-sensitive rewrites Flow-sensitive rewrites conditional constant propagation dominating test detection flow-carried type narrowing dead code elimination Text and much more..
  9. Null check Elimination Null check Elimination Into the rabbit hole

    -XX:+PrintCompilation Compilation Level Deoptimizations
  10. Null check Elimination Null check Elimination Into the rabbit hole

    -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=print,NullCheck::isPowerOfTwo
  11. Null check Elimination Null check Elimination Into the rabbit hole

    -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=print,NullCheck::isPowerOfTwo Someone said Assembly?
  12. Null check Elimination Null check Elimination Into the rabbit hole

    -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:CompileCommand=print,NullCheck::isPowerOfTwo Someone said Assembly?
  13. Null check Elimination Null check Elimination Into the rabbit hole

    implicit exception: dispatches to 0x00007fdff5318274
  14. Null check Elimination Null check Elimination JVM uses JVM uses

    SIGSEGV SIGSEGV as control flow as control flow Into the rabbit hole Handle Signals: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/signals001.html#CIHBBDED Speculative Optimization Speculative Optimization Faster Faster IF IF not null are encountered (Uncommon Trap) not null are encountered (Uncommon Trap)
  15. Null check Elimination Null check Elimination JVM uses JVM uses

    SIGSEGV SIGSEGV as control flow as control flow Into the rabbit hole Handle Signals: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/signals001.html#CIHBBDED Speculative Optimization Speculative Optimization Faster Faster IF IF not null are encountered (Uncommon Trap) not null are encountered (Uncommon Trap) Implicit Null Check? Field and Array access is null checked
  16. Method Inlining Method 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
  17. Method Inlining Method 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?
  18. Method Inlining Method 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?
  19. Method Inlining Method 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
  20. Method Inlining Method 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
  21. Method Inlining Method 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
  22. Method Inlining Method 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
  23. Method Inlining Method 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
  24. Method Inlining Method 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
  25. Escape Analysis Escape Analysis No synchronization lock when calling getNoEscapeSum

    method Escape Analysis for Java: http://www.research.ibm.com/people/j/jdchoi/escape.ps
  26. Escape Analysis Escape Analysis No synchronization lock when calling getNoEscapeSum

    method No allocate a Sum object at all, just keep track of the individual fields of the object Escape Analysis for Java: http://www.research.ibm.com/people/j/jdchoi/escape.ps
  27. Deoptimization Deoptimization The compiler can back to previous versions of

    compiled code Effect 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
  28. Conclusion Conclusion 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
  29. Conclusion Conclusion 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
  30. Resources Resources 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