Slide 1

Slide 1 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1

Slide 2

Slide 2 text

Java Performance Paradoxes Christian Wirth Oracle Labs Austria

Slide 3

Slide 3 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 3 The following is intended to provide some insight into a line of research in Oracle Labs. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described in connection with any Oracle product or service remains at the sole discretion of Oracle. Any views expressed in this presentation are my own and do not necessarily reflect the views of Oracle.

Slide 4

Slide 4 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 4 Oracle Labs Austria The Mission of Oracle Labs is straightforward: Identify, explore, and transfer new technologies that have the potential to substantially improve Oracle's business.

Slide 5

Slide 5 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 5 Oracle Labs Austria  Yes, there is an Oracle Labs Office in Linz! – Oracle (Sun) cooperation since 2001 with JKU Linz, Institute for Systems Software (Prof. Mössenböck) – ~5 Employees, ~10 Students – Offices located in the Science Park, JKU – Research topic: Compilers, Virtual Machines  Yes, we are hiring! – Full-Time at Oracle – PhD / Master at JKU

Slide 6

Slide 6 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 6 Benchmarking  What operation is faster in Java? – Integer division – Double division

Slide 7

Slide 7 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 7 What went wrong?  Warmup – JIT compilation  Not using result – Compiler can optimize calculation  Timer resolution  …

Slide 8

Slide 8 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 8 Benchmarking – How to NOT do it  So You Want to Write a Micro-Benchmark (John Rose) – https://wikis.oracle.com/display/HotSpotInternals/MicroBenchmarks  How to NOT Write a Microbenchmark (Cliff Click) – http://www.azulsystems.com/events/javaone_2002/microbenchmarks.pdf

Slide 9

Slide 9 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 9 Java Microbenchmark Harness  OpenJDK Code Tools  http://openjdk.java.net/projects/code-tools/jmh/  Control Benchmark execution with annotations @GenerateMicroBenchmark public int micro1() { return Math.random(); }

Slide 10

Slide 10 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 10 Performance VS Style?  Coding styles suggest – Many small functions – Getters and setters – …  What about performance? Aren’t calls expensive? public double inlineCall(Counter counter) { counter.value++; if ((counter.value % 1000) == 0) { return calculateSin(counter.value); } else { return calculateCos(counter.value); } }

Slide 11

Slide 11 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 11 Method call, abstract, interface  A call can be resolved: – directly – virtually – via an interface  Does that make a difference in performance?

Slide 12

Slide 12 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 12 Method call, abstract, interface exampleCall1Base 473070.344 ops/ms exampleCall1Instance 471343.231 ops/ms exampleCall1Interface 420757.830 ops/ms  Try 1  Try 2 (loading Process2) exampleCall1Base 410035.398 ops/ms exampleCall1Instance 472614.148 ops/ms exampleCall1Interface 409092.825 ops/ms

Slide 13

Slide 13 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 13 Virtual Calls public abstract class Processor { public abstract int process(int value); } Processor[] processors = new Processor[N]; public int callVirtual (Counter counter) { counter.value++; Processor proc = processors [counter.value&MASK]; return proc.process(counter.value); }

Slide 14

Slide 14 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 14 Virtual Calls - Monomorphic

Slide 15

Slide 15 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 15 Virtual Calls - Duomorphic

Slide 16

Slide 16 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 16 Virtual Calls - Polymorphic

Slide 17

Slide 17 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 17 What would you suggest? class Key { .... public boolean equals(Key other) { return this.id == other.id && this.ref == other.ref; .... } class Test { private Key cachedKey = new Key(...); boolean isInCache(int id, Object ref) { Key key = new Key(id, ref); return key.equals(cachedKey); } .... }

Slide 18

Slide 18 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 18 Escape Analysis  Analyze the scope of a new object's uses and decide whether to allocate it on the Java heap.  Done by Server Compiler (C2) since Java SE 6u23.  Can enable optimizations like: – Stack Allocation (not currently) – Scalar Replacement – Lock Elision

Slide 19

Slide 19 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 19 Flow-sensitive VS Flow-insensitive protected Key cachedKey; public Value get(int id, Object ref) { Key key = new Key(id, ref); if (!key.equals(cachedKey) { cachedValue = new Value(id, ref); cachedKey = key; } return cachedValue; }

Slide 20

Slide 20 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 20 Flow-sensitive VS Flow-insensitive protected Key cachedKey; public Value get(int id, Object ref) { Key key = new Key(id, ref); if (!key.equals(cachedKey) { cachedValue = new Value(id, ref); cachedKey = key; } return cachedValue; }

Slide 21

Slide 21 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 21 Partial Escape Analysis  Control flow sensitive Analysis  Moves object allocation to branch(es) where object escapes.  No allocation occurs in other branches – optimizations possible there https://wiki.openjdk.java.net/display/Graal/Graal+Partial+Escape+Analysis Lukas Stadler’s PhD thesis (yet to be published)

Slide 22

Slide 22 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 22 Partial Escape Analysis protected Key cachedKey; public Value get(int id, Object ref) { Key key = new Key(idx, ref); if (!key.equals(cachedKey) { cachedValue = new Value(idx, ref); cachedKey = key; } return cachedValue; }

Slide 23

Slide 23 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 23 Partial Escape Analysis protected Key cachedKey; public Value get(int id, Object ref) { if (! (cachedKey.id == id && cachedKey.ref == ref) ) { cachedValue = new Value(idx, ref); cachedKey = new Key(idx, ref); } return cachedValue; }

Slide 24

Slide 24 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 24 Graal a quest for the JVM to leverage its own J An interpreter and compiler written in Java – Maintainable – Extendable – Excellent code quality – Multi-Language Interpreter Framework (Truffle) – http://openjdk.java.net/projects/graal/

Slide 25

Slide 25 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 25 i = read(); j = read(); // 2 and 3 g = cond ? 1.5 : "12"; (i*j) + g; // 9 or "612" Truffle a multi-language interpreter Framework

Slide 26

Slide 26 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 26 i = read(); j = read(); g = cond ? 1.5 : "12"; (i*j) + g; // int => double Truffle a multi-language interpreter Framework

Slide 27

Slide 27 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 27 Truffle  Javascript  Python  R  Smalltalk  Ruby  …

Slide 28

Slide 28 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 28 Oracle Labs Linz  You have an unsolved, compiler/VM related performance issue? – Contact us! – We are looking for (prospective?) Oracle customers with hard, real- world performance challenges  (Reminder: we are hiring) Oracle Labs Austria Altenberger Straße 69 4040 Linz thomas.wuerthinger@oracle.com christian.wirth@oracle.com

Slide 29

Slide 29 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 29

Slide 30

Slide 30 text

Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 30