JVM Optimization 101 Sebastian Zarnekow itemis Static vs Dynamic - - PowerPoint PPT Presentation

jvm optimization 101
SMART_READER_LITE
LIVE PREVIEW

JVM Optimization 101 Sebastian Zarnekow itemis Static vs Dynamic - - PowerPoint PPT Presentation

JVM Optimization 101 Sebastian Zarnekow itemis Static vs Dynamic Compilation AOT vs JIT JIT Compilation Compiled when needed Maybe immediately before execution ...or when the VM decides its important ...or never? JIT Compilation


slide-1
SLIDE 1

JVM Optimization 101

Sebastian Zarnekow
 


itemis

slide-2
SLIDE 2

Static vs Dynamic Compilation

AOT vs JIT

slide-3
SLIDE 3

JIT Compilation

Compiled when needed Maybe immediately before execution ...or when the VM decides it’s important ...or never?

slide-4
SLIDE 4

JIT Compilation

Makes bytecode fast Profiling scenarios at runtime Invariants (known types, constants, null values) Statistics (used branches, calls, polymorphism) Decisions based on executed code paths De-optimization if the world changes

slide-5
SLIDE 5

Optimization Strategies

(Examples by Vladimir Ivanov)

slide-6
SLIDE 6

Inlining

int sum(int max) {
 int res = 0;
 for (int i = 0; i < max; i++) { res = plus(res, i); } return res; } int plus(int a, int b) { return a + b; }

slide-7
SLIDE 7

Inlining

int sum(int max) {
 int res = 0;
 for (int i = 0; i < max; i++) { res = res + i; } return res; } int plus(int a, int b) { return a + b; }

slide-8
SLIDE 8

Inlining

Gather Statistics Class Hierarchy Analysis Combine Caller and Callee
 (Call Site + Call) De-Virtualize and Optimize

slide-9
SLIDE 9

public int constant() { return 3; } public int method() {
 Tuple t = new Tuple(1, 2);
 return reduce(t);
 }
 public int reduce(Tuple t) {
 return t.first + getSecond(t);
 }
 public int getSecond(Tuple t) {
 return t.second;
 }

Escape Analysis

slide-10
SLIDE 10

Escape Analysis

public int method() {
 Tuple t = new Tuple(1, 2); return reduce(t); }
 public int reduce(Tuple t) { return t.first + getSecond(t); } public int getSecond(Tuple t) { return t.second; }

slide-11
SLIDE 11

Escape Analysis

public int method() {
 Tuple t = new Tuple(1, 2); return t.first + t.second; }
 public int reduce(Tuple t) { return t.first + getSecond(t); } public int getSecond(Tuple t) { return t.second; }

slide-12
SLIDE 12

Escape Analysis

public int method() {
 Tuple t = new Tuple(1, 2); return 3; t.first + t.second; }
 public int reduce(Tuple t) { return t.first + getSecond(t); } public int getSecond(Tuple t) { return t.second; }

slide-13
SLIDE 13

Object Scope Analysis Avoid Unnecessary Objects on Heap Global Escape, Arg Escape, No Escape Eliminates Object Allocations and Unnecessary Locks May Eliminate Unnecessary Defensive Copies

Escape Analysis

slide-14
SLIDE 14

Methods with long running loops, back-branching Compile and replace while running Only rarely used in large systems Loops vs Benchmarks

On Stack Replacement (OSR)

slide-15
SLIDE 15

public void foo(int[] arr, int a) { for(int i=0; i<arr.length; i++) { arr[i] += a; } }

Loop Unrolling

slide-16
SLIDE 16

public void foo(int[] arr, int a) { for(int i=0; i<arr.length; i+=4){ arr[i] += a arr[i+1] += a; arr[i+2] += a; arr[i+3] += a; } ... }

Loop Unrolling

slide-17
SLIDE 17

pubic void m(Object newValue) { synchronized(this) { f1 = newValue; } synchronized(this) { f2 = newValue; } }

Locks

slide-18
SLIDE 18

pubic void m(Object newValue) { synchronized(this) { f1 = newValue; } synchronized(this) { f2 = newValue; } }

Locks

slide-19
SLIDE 19

pubic String m(Object newValue) { StringBuffer sb = new SB(); sb.append(newValue); sb.append(newValue); return sb.toString(); }

Locks

slide-20
SLIDE 20

Intrinsics

This is not the code that you’re looking for.

Known to the JIT compiler Bytecode is ignored inserts optimized native code for current platform String::equals, String::indexOf, Object::hashCode, …

slide-21
SLIDE 21

Inlining Escape Analysis Intrinsics Loop Unrolling Lock Fusion and Lock Elision

Optimization Strategies

slide-22
SLIDE 22

... and counting

compiler tactics delayed compilation tiered compilation

  • n-stack replacement

delayed reoptimization program dependence graph representation static single assignment representation speculative (profile-based) techniques

  • ptimistic nullness assertions
  • ptimistic type assertions
  • ptimistic type strengthening
  • ptimistic array length strengthening

untaken branch pruning

  • ptimistic N-morphic inlining

branch frequency prediction call frequency prediction proof-based techniques exact type inference memory value inference memory value tracking constant folding reassociation

  • perator strength reduction

null check elimination type test strength reduction type test elimination algebraic simplification common subexpression elimination integer range typing flow-sensitive rewrites conditional constant propagation dominating test detection flow-carried type narrowing dead code elimination language-specific techniques class hierarchy analysis devirtualization symbolic constant propagation autobox elimination escape analysis lock elision lock fusion de-reflection memory and placement transformation expression hoisting expression sinking redundant store elimination adjacent store fusion card-mark elimination merge-point splitting loop transformations loop unrolling loop peeling safepoint elimination iteration range splitting range check elimination loop vectorization global code shaping inlining (graph integration) global code motion heat-based code layout switch balancing throw inlining control flow graph transformation local code scheduling local code bundling delay slot filling graph-coloring register allocation linear scan register allocation live range splitting copy coalescing constant splitting copy removal address mode matching instruction peepholing DFA-based code generator

slide-23
SLIDE 23

Diagnostics

Print compiled methods


  • XX:+PrintCompilation

Print info about inlining


  • XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining

Print assembly code


  • XX:+PrintAssembly
slide-24
SLIDE 24

Questions?

slide-25
SLIDE 25

Tell me what you think.

slide-26
SLIDE 26

http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance- enhancements-7.html https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques http://www.slideshare.net/iwanowww/jitcompiler-in-jvm-by http://www.slideshare.net/RafaelWinterhalter/an-introduction-to-jvm-performance http://www.slideshare.net/SergeyKuksenko/quantum-performance- effects-44390719?related=1 http://www.slideshare.net/dougqh/jvm-mechanics-when-does-the https://wikis.oracle.com/display/HotSpotInternals/PerformanceTacticIndex

Resources

slide-27
SLIDE 27

Off Topic

For Xtext users

slide-28
SLIDE 28

Take the survey

Win an iPad!

survey.xtext.org