Java Performance MythBusters

6,276 views
6,266 views

Published on

Inspired by Arno Haases great talk at JAX 2012, I assembled some performance "tipps" and took a look at those. Arno's permission to use the same title as he did in his talk is very much appreciated. Thanks a lot!

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,276
On SlideShare
0
From Embeds
0
Number of Embeds
4,715
Actions
Shares
0
Downloads
42
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Java Performance MythBusters

    1. 1. Java Performance MythBuster @szarnekow (c) itemis
    2. 2. Micro-Benchmarking✦ Fictional Test of a Small Code-Snippet✦ Compare Functionally Equivalent Implementations✦ Seek for Arguments in the “Java is Slow” Battle
    3. 3. Disclaimer
    4. 4. Disclaimer
    5. 5. More DisclaimerDon’t do this at home.(even though it’s fun)
    6. 6. #1: Final Variables Are Fasterint computeSmth(final int a) { final int result = a * a * a; return result;}
    7. 7. In C++, everytime someonewrites ">> 3" instead of "/ 8", I bet the compiler is like,"OH DAMN! I would have never thought of that!" (@jonshiring)
    8. 8. #2: String + String is Slow! "a" + "b" vs.new StringBuilder("a") .append("b") .toString();
    9. 9. public class Sample { static String s = "a" + "b"; }$javap -c Sample
    10. 10. public class Sample { static String s = "a" + "b"; }$javap -c SampleCompiled from "Sample.java"public class Sample extends java.lang.Object{static java.lang.String s;static {}; Code: 0:! dc! #10; //String ab l 2:! utstatic! #12; //Field s:Ljava/lang/String; p 5:! eturn r...
    11. 11. static String b = "b";static String s = "a" + b;
    12. 12. static String b = "b"; static String s = "a" + b;5:! ! new! #14; //class StringBuilder8:! ! dup9:! ! ldc! #16; //String a11:! invokespecial! #18; //StringBuilder."<init>"14:! getstatic! #12; //Field b17:! invokevirtual! #22; //StringBuilder.append20:! invokevirtual! #26; //StringBuilder.toString
    13. 13. #3: {Objects,Exceptions, Things} Are Too Slow!
    14. 14. CaliperMicro-Benchmarking done right.
    15. 15. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params
    16. 16. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial
    17. 17. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial ✦ Pretty Results
    18. 18. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial ✦ Pretty Results ✦ Avoid “Heisenbenchmark”
    19. 19. List as Queue
    20. 20. List as Queue
    21. 21. The Art of Adding Numbers int sum() { int result = 0; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { result = result + j; } } return result; }
    22. 22. The Art of Adding Numbers ✦ Plain Imperative: Nested Loops ✦ Extracted Methods (addAll, add) ✦ Parameter Objects (AddParam, AddResult) ✦ Strategies (addAll(strategy) ✦ Fields (this.result = this.a + this.b) ✦ Control Flow By Exception (throw result)
    23. 23. Show Me The Numbers (a = 25000 / b = 25000)
    24. 24. Show Me The Numbers (a = 25000 / b = 25000)
    25. 25. Show Me The Numbers (a = 25000 / b = 25000)
    26. 26. Show Me The Numbers (a = 25000 / b = 25000)
    27. 27. Diagnose Some Useful VM Args$java -server -Xmx... -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompilation -XX:+PrintInlining ObjectOrientedAdder 25000 25000
    28. 28. More VM Args -XX:CompileCommand$java -server -Xmx... -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompilation -XX:+PrintInlining -XX:CompileCommand=print,*ObjectOrientedAdder.sum* ObjectOrientedAdder 25000 25000
    29. 29. -XX:+PrintInliningObjectOrientedAdder::sum (27 bytes)@ 9 ObjectOrientedAdder::addAll (39 bytes) inline (hot) @ 13 AddParam::<init> (15 bytes) inline (hot) @ 1 java.lang.Object::<init> (1 bytes) inline (hot) @ 19 ObjectOrientedAdder::add (17 bytes) inline (hot) @ 5 AddParam::getA (5 bytes) inline (hot) @ 9 AddParam::getB (5 bytes) inline (hot) @ 13 AddResult::<init> (10 bytes) inline (hot) @ 1 java.lang.Object::<init> (1 byte) inline(hot) @ 22 AddResult::getResult (5 bytes) inline (hot)
    30. 30. Optimization Strategies✦ Inlining
    31. 31. Optimization Strategies✦ Inlining✦ Intrinsics
    32. 32. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis
    33. 33. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling
    34. 34. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling✦ Lock Fusion and Lock Elision
    35. 35. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling✦ Lock Fusion and Lock Elision✦ Many More ...
    36. 36. ... As of Oct 2009compiler tactics flow-sensitive rewrites global code shapingdelayed compilation conditional constant propagation inlining (graph integration)tiered compilation dominating test detection global code motionon-stack replacement flow-carried type narrowing heat-based code layoutdelayed reoptimization dead code elimination switch balancingprogram dependence graph representation throw inliningstatic single assignment representation language-specific techniques class hierarchy analysis control flow graph transformationspeculative (profile-based) techniques devirtualization local code schedulingoptimistic nullness assertions symbolic constant propagation local code bundlingoptimistic type assertions autobox elimination delay slot fillingoptimistic type strengthening escape analysis graph-coloring register allocationoptimistic array length strengthening lock elision linear scan register allocationuntaken branch pruning lock fusion live range splittingoptimistic N-morphic inlining de-reflection copy coalescingbranch frequency prediction constant splittingcall frequency prediction memory and placement transformation copy removal expression hoisting address mode matchingproof-based techniques expression sinking instruction peepholingexact type inference redundant store elimination DFA-based code generatormemory value inference adjacent store fusionmemory value tracking card-mark eliminationconstant folding merge-point splittingreassociationoperator strength reduction loop transformationsnull check elimination loop unrollingtype test strength reduction loop peelingtype test elimination safepoint eliminationalgebraic simplification iteration range splittingcommon subexpression elimination range check eliminationinteger range typing loop vectorization
    37. 37. Should I ...Always Use Objects, then?
    38. 38. Should I ...Always Use Objects, then?
    39. 39. Other Things ...
    40. 40. Some Advice✦ [..] Write Readable and Clear Code. [..] (David Keenan)✦ [..] slavishly follow a principle of simple, clear coding that avoids clever optimizations [..] (Caliper FAQ)✦ Performance advice has a short shelf-life (B. Goetz)
    41. 41. Credits: I’m thankful to Arno Haase, who allowed to highjack the title of his great session at JAX 2012 which inspired me for this talk.

    ×