JAVA PERFORMANCE
IMPROVEMENTS
MYSTIC ARTSUKEN SHAH
SHINE EA
JAVA PERFORMANCE IMPROVEMENTS - DIMENSIONS
▸Fix Memory leaks
▸Fine tune Garbage collection
▸Improve ORM performance
▸ Fix Inefficient code
▸JIT and code cache
JIT OPTIMISATIONS - WTF?
DEMO
JIT - WHAT IS IT????
▸Dynamic compiler - Hotspot
▸Profile Guided Optimisation (PGO)
▸Aggressive optimisations
▸Tiered compilation mode
JIT - UNIT OF COMPILATION
▸Method
▸Loop
▸Hot methods
▸Hot loops
▸Warm methods and warm loops
JIT - HOT STUFF
-XX:+PRINTFLAGSFINAL
JIT - LIFE CYCLE
Interpreting
Profiling
Optimisation
De-optimisation
JIT - COMPILERS
C1 - CLIENT
COMPILER
▸ Fast acting compiler
▸ Invocation count > 2000
▸ 10x faster than interpreter
▸ Backendge count > 65000
C2 - SERVER
COMPILER
▸ Smart slow compiler
▸ Invocation count > 15000
▸ 2x faster than C1
▸ Backendge count > 40000
JIT - TIERED COMPILATION
0 3 4
0
0
2 3 4
3
Interpreter C1 C2
none counters details
1
preferred
C2 busy
Trivial
JIT OPTIMISATIONS - DEAD CODE, COMMON EXPRESSIONS
ELIMINATION
Dead code elimination
Common expression elimination
JIT OPTIMISATIONS - OSR (ON STACK REPLACEMENT)
For loop will be optimised by C1 compiler to level 3
JIT OPTIMISATIONS - LOOP PEELING
JIT OPTIMISATIONS - LOOP INVERSION
JIT OPTIMISATIONS - CODE MOTION, CONSTANT FOLDING
JIT OPTIMISATIONS - INTRINSICS
▸ Use hardware optimised compiled code
▸ System.arraycopy(), Math.sin() / cos() / tan(), System.currentMillis(),
Unsafe.compareAndSwapInt(), String.indexOf() /compareTo()/equals()
JIT OPTIMISATIONS - INLINING
JIT OPTIMISATIONS - CLASS HIERARCHY ANALYSIS & INLINING
Call site
A
PROCES
S()
A1
PROCES
S()
A2
PROCES
S()
SERVI
CE
JIT OPTIMISATIONS - CLASS HIERARCHY ANALYSIS & INLINING
FUNC
PLUS MINUS DIVIDE
Megamorphic
Inline till bimorphic scenario
Discovering new type leads to stop the
world deoptimisation
FUNC
PLUS
Monomorphic
Inline the method when
possible after CHA
Always has uncommon
trap for type.
JIT OPTIMISATIONS - CLASS HIERARCHY ANALYSIS & INLINING
DEMO
JIT OPTIMISATIONS - SPECULATIVE OPTIMISATIONS
▸Implicite null assertions
▸Type assertions
▸Untaken branch pruning
▸Optimistic megamorphic inline
JIT OPTIMISATIONS
JIT COMPILER LOGGING
▸Light logging
▸Full logging
▸Tools -> {JITWatch, jitwatch-intellij}
▸JMH Benchmark
JIT COMPILER LOGGING - LIGHT
-XX:+PRINTCOMPILATION
Time ID Flags Lvl Method Size
Flags -> b (blocking), s (static), n (native), % (OnStackReplacement), *
(Generating native wrapper), ! (found exception handler)
JIT COMPILER LOGGING - FULL
-XX:+UNLOCKDIAGNOSTICVMOPTIONS -XX:+LOGCOMPILATION -
XX:LOGFILE=<PATH_TO_LOG_FILE>
JIT COMPILER - MY OWN EFFORT
JIT OPTIMISATIONS - WHAT TO DO NEXT?
▸ Exhaust other options.
▸ Find out critical or most used code
paths
▸ Capture and analyse compilation logs
▸ Fix, test and measure performance
▸ Write good code (smaller methods,
unnecessary inheritance, make static
utilities)
JIT OPTIMISATIONS - WHAT IS JAVA DOING?
VS
JIT OPTIMISATIONS - WHAT IS JAVA DOING?
▸ JVMCI (JVM Compiler Interface)
http://openjdk.java.net/jeps/243
▸ Set of interfaces used by JVM to perform compilation.
▸ Use Java to write C1/C2 compilers.
▸ Project Graal http://openjdk.java.net/projects/graal/
#ThorRagnarok
JIT OPTIMISATIONS - WHAT IS JAVA DOING?
▸ AOT Compilation http://openjdk.java.net/jeps/295
#ThorRagnarok
▸ Java 9 is packed with jaotc tool.
▸ Restricted to Linux x64
JIT COMPILER - MORE INFO
Slides —> https://www.slideshare.net/SukenShah2/java-jit-compilers-
and-optimisations
Github —> https://github.com/suken/jit-reporter
LinkedIn —> https://www.linkedin.com/in/suken-shah-b805b937/
Email —> suken.3@gmail.com
THANK
YOU

Java performance jit

  • 1.
  • 2.
    JAVA PERFORMANCE IMPROVEMENTS- DIMENSIONS ▸Fix Memory leaks ▸Fine tune Garbage collection ▸Improve ORM performance ▸ Fix Inefficient code ▸JIT and code cache
  • 3.
  • 4.
    JIT - WHATIS IT???? ▸Dynamic compiler - Hotspot ▸Profile Guided Optimisation (PGO) ▸Aggressive optimisations ▸Tiered compilation mode
  • 5.
    JIT - UNITOF COMPILATION ▸Method ▸Loop ▸Hot methods ▸Hot loops ▸Warm methods and warm loops
  • 6.
    JIT - HOTSTUFF -XX:+PRINTFLAGSFINAL
  • 7.
    JIT - LIFECYCLE Interpreting Profiling Optimisation De-optimisation
  • 8.
    JIT - COMPILERS C1- CLIENT COMPILER ▸ Fast acting compiler ▸ Invocation count > 2000 ▸ 10x faster than interpreter ▸ Backendge count > 65000 C2 - SERVER COMPILER ▸ Smart slow compiler ▸ Invocation count > 15000 ▸ 2x faster than C1 ▸ Backendge count > 40000
  • 9.
    JIT - TIEREDCOMPILATION 0 3 4 0 0 2 3 4 3 Interpreter C1 C2 none counters details 1 preferred C2 busy Trivial
  • 10.
    JIT OPTIMISATIONS -DEAD CODE, COMMON EXPRESSIONS ELIMINATION Dead code elimination Common expression elimination
  • 11.
    JIT OPTIMISATIONS -OSR (ON STACK REPLACEMENT) For loop will be optimised by C1 compiler to level 3
  • 12.
    JIT OPTIMISATIONS -LOOP PEELING
  • 13.
    JIT OPTIMISATIONS -LOOP INVERSION
  • 14.
    JIT OPTIMISATIONS -CODE MOTION, CONSTANT FOLDING
  • 15.
    JIT OPTIMISATIONS -INTRINSICS ▸ Use hardware optimised compiled code ▸ System.arraycopy(), Math.sin() / cos() / tan(), System.currentMillis(), Unsafe.compareAndSwapInt(), String.indexOf() /compareTo()/equals()
  • 16.
  • 17.
    JIT OPTIMISATIONS -CLASS HIERARCHY ANALYSIS & INLINING Call site A PROCES S() A1 PROCES S() A2 PROCES S() SERVI CE
  • 18.
    JIT OPTIMISATIONS -CLASS HIERARCHY ANALYSIS & INLINING FUNC PLUS MINUS DIVIDE Megamorphic Inline till bimorphic scenario Discovering new type leads to stop the world deoptimisation FUNC PLUS Monomorphic Inline the method when possible after CHA Always has uncommon trap for type.
  • 19.
    JIT OPTIMISATIONS -CLASS HIERARCHY ANALYSIS & INLINING DEMO
  • 20.
    JIT OPTIMISATIONS -SPECULATIVE OPTIMISATIONS ▸Implicite null assertions ▸Type assertions ▸Untaken branch pruning ▸Optimistic megamorphic inline
  • 21.
  • 22.
    JIT COMPILER LOGGING ▸Lightlogging ▸Full logging ▸Tools -> {JITWatch, jitwatch-intellij} ▸JMH Benchmark
  • 23.
    JIT COMPILER LOGGING- LIGHT -XX:+PRINTCOMPILATION Time ID Flags Lvl Method Size Flags -> b (blocking), s (static), n (native), % (OnStackReplacement), * (Generating native wrapper), ! (found exception handler)
  • 24.
    JIT COMPILER LOGGING- FULL -XX:+UNLOCKDIAGNOSTICVMOPTIONS -XX:+LOGCOMPILATION - XX:LOGFILE=<PATH_TO_LOG_FILE>
  • 25.
    JIT COMPILER -MY OWN EFFORT
  • 26.
    JIT OPTIMISATIONS -WHAT TO DO NEXT? ▸ Exhaust other options. ▸ Find out critical or most used code paths ▸ Capture and analyse compilation logs ▸ Fix, test and measure performance ▸ Write good code (smaller methods, unnecessary inheritance, make static utilities)
  • 27.
    JIT OPTIMISATIONS -WHAT IS JAVA DOING? VS
  • 28.
    JIT OPTIMISATIONS -WHAT IS JAVA DOING? ▸ JVMCI (JVM Compiler Interface) http://openjdk.java.net/jeps/243 ▸ Set of interfaces used by JVM to perform compilation. ▸ Use Java to write C1/C2 compilers. ▸ Project Graal http://openjdk.java.net/projects/graal/ #ThorRagnarok
  • 29.
    JIT OPTIMISATIONS -WHAT IS JAVA DOING? ▸ AOT Compilation http://openjdk.java.net/jeps/295 #ThorRagnarok ▸ Java 9 is packed with jaotc tool. ▸ Restricted to Linux x64
  • 30.
    JIT COMPILER -MORE INFO Slides —> https://www.slideshare.net/SukenShah2/java-jit-compilers- and-optimisations Github —> https://github.com/suken/jit-reporter LinkedIn —> https://www.linkedin.com/in/suken-shah-b805b937/ Email —> suken.3@gmail.com
  • 31.

Editor's Notes

  • #5 Dynamic compilation means that the compilation happens at runtime along side program execution Captures data about code
  • #6 invocation counters and backendge counters
  • #17 Max Inline size for not hot methods = 35 bytes (250 invocations) Max Inline size for hot hot methods is 325 bytes, max inline level = 9, recursion = 1. Inline small methods. (max size = 325 bytes, max inline level = 9, recursion = 1) static, private and constructor calls.
  • #18 Explain call sites. Monomorphic, biomorphic and metamorphic. Sets uncommon trap. Abstract class - deoptimsation if new type is discovered Interfaces - no deoptimization is performed
  • #19 Explain call sites. Monomorphic, biomorphic and metamorphic. Sets uncommon trap. Abstract class - deoptimsation if new type is discovered Interfaces - no deoptimization is performed
  • #21 Adds implicit null checks when required (only after observing > 3 NPE) Adds implicit type assertions (monomorphic inline) Set uncommon traps for untaken branches of code. As explained earlier slide.
  • #23 JMH benchmarks are like junit tests but has its own annotations to force compilers to do inlining, measuring benchmark.
  • #29 Write dynamic compilers faster with Java. Better code quality, extensible and quick to develop.
  • #30 Ahead Of Time compilation can be used to compile some modules of java and/or your code as part of your builds avoiding runtime compilation. Its an experimental feature. It cannot replace c2 compile code as it doesn’t have any profile data. So no speculative optimisations.