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

Christian Wirth on Performance Paradoxes

Christian Wirth on Performance Paradoxes

More Decks by Enterprise Java User Group Austria

Other Decks in Technology

Transcript

  1. 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.
  2. 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.
  3. 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
  4. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

    6 Benchmarking  What operation is faster in Java? – Integer division – Double division
  5. 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  …
  6. 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
  7. 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(); }
  8. 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); } }
  9. 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?
  10. 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
  11. 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); }
  12. 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); } .... }
  13. 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
  14. 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; }
  15. 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; }
  16. 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)
  17. 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; }
  18. 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; }
  19. 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/
  20. 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
  21. 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
  22. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

    27 Truffle  Javascript  Python  R  Smalltalk  Ruby  …
  23. 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 [email protected] [email protected]