We’ll dive into the mystic arts used by Java to gain significant performance improvements. Particularly into how Java's dynamic compilers (C1 and C2) work, optimizing our code and transitioning into different stages based on its profile. We'll also look at some of the primitive, complex and aggressive optimization techniques used by Java. Travel to a different realm of Java performance, Dr Strange style, to defeat the monster that is poor Java performance.
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.
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)
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
Dynamic compilation means that the compilation happens at runtime along side program execution
Captures data about code
invocation counters and backendge counters
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.
Explain call sites.
Monomorphic, biomorphic and metamorphic.
Sets uncommon trap.
Abstract class - deoptimsation if new type is discovered
Interfaces - no deoptimization is performed
Explain call sites.
Monomorphic, biomorphic and metamorphic.
Sets uncommon trap.
Abstract class - deoptimsation if new type is discovered
Interfaces - no deoptimization is performed
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.
JMH benchmarks are like junit tests but has its own annotations to force compilers to do inlining, measuring benchmark.
Write dynamic compilers faster with Java. Better code quality, extensible and quick to develop.
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.