Your SlideShare is downloading. ×
Java Performance MythBusters
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java Performance MythBusters

5,279

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. …

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
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,279
On Slideshare
0
From Embeds
0
Number of Embeds
44
Actions
Shares
0
Downloads
39
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • Transcript

    • 1. Java Performance MythBuster @szarnekow (c) itemis
    • 2. Micro-Benchmarking✦ Fictional Test of a Small Code-Snippet✦ Compare Functionally Equivalent Implementations✦ Seek for Arguments in the “Java is Slow” Battle
    • 3. Disclaimer
    • 4. Disclaimer
    • 5. More DisclaimerDon’t do this at home.(even though it’s fun)
    • 6. #1: Final Variables Are Fasterint computeSmth(final int a) { final int result = a * a * a; return result;}
    • 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. #2: String + String is Slow! "a" + "b" vs.new StringBuilder("a") .append("b") .toString();
    • 9. public class Sample { static String s = "a" + "b"; }$javap -c Sample
    • 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. static String b = "b";static String s = "a" + b;
    • 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. #3: {Objects,Exceptions, Things} Are Too Slow!
    • 14. CaliperMicro-Benchmarking done right.
    • 15. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params
    • 16. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial
    • 17. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial ✦ Pretty Results
    • 18. CaliperMicro-Benchmarking done right. ✦ Warm-Up, Trails, Params ✦ Forked JVM per Trial ✦ Pretty Results ✦ Avoid “Heisenbenchmark”
    • 19. List as Queue
    • 20. List as Queue
    • 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. 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. Show Me The Numbers (a = 25000 / b = 25000)
    • 24. Show Me The Numbers (a = 25000 / b = 25000)
    • 25. Show Me The Numbers (a = 25000 / b = 25000)
    • 26. Show Me The Numbers (a = 25000 / b = 25000)
    • 27. Diagnose Some Useful VM Args$java -server -Xmx... -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompilation -XX:+PrintInlining ObjectOrientedAdder 25000 25000
    • 28. More VM Args -XX:CompileCommand$java -server -Xmx... -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompilation -XX:+PrintInlining -XX:CompileCommand=print,*ObjectOrientedAdder.sum* ObjectOrientedAdder 25000 25000
    • 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. Optimization Strategies✦ Inlining
    • 31. Optimization Strategies✦ Inlining✦ Intrinsics
    • 32. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis
    • 33. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling
    • 34. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling✦ Lock Fusion and Lock Elision
    • 35. Optimization Strategies✦ Inlining✦ Intrinsics✦ Escape Analysis✦ Loop Unrolling✦ Lock Fusion and Lock Elision✦ Many More ...
    • 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. Should I ...Always Use Objects, then?
    • 38. Should I ...Always Use Objects, then?
    • 39. Other Things ...
    • 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. 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.

    ×