SCALA PROFILING                             (for Java developers)                                                     Fili...
Who am I                    • Filippo Pacifici                     • Twitter: OddId_                     • mail: filippo.pac...
What’s this all about?                    • Apply Java profiling methods to Scala                         programs         ...
Java profiling 101mercoledì 23 maggio 12
Java profiling 101                    • Goals:                     • troubleshoot performance problems                     ...
Java profiling 101                    • Ehi, I use Scala, why should I care about Java                         profiling?   ...
Java methods profiling •      Tracks methods invocations       •      Runtime instrumentation       •      Time analysismer...
Java memory profiling       •      Dumps heap content             •     Browse objects in the heap             •     Memory...
Profiling tools                    • Method profiling                     • Java Visual VM (embedded in JDK)                ...
Back to Scala...                    • We won’t find Scala specific constructs                     • Need to know how Scala i...
Scala Functionsmercoledì 23 maggio 12
Short discouraging                         comparative demomercoledì 23 maggio 12
Scala functions vs byte code                    • Classic functions converted in methods                    • First class ...
AbstractFunction      •     AbstractFunction2            •     takes 2 input parameters      •     One apply method per   ...
AbstractFunction                    • Find the call in the profile:                • anonfun => instance of the anonymous c...
Functions in the heap                    • Each instance of AbstractFunction present                         in the heap  ...
Where do we use them?                    • Our program did not contain any anonymous                         function, rig...
Performance impact                    • Is this a performance impact?                     • Scala compiler performs optimi...
Some examplesmercoledì 23 maggio 12
Scala data structures                             (Collections in heap dump)mercoledì 23 maggio 12
Scala Lists                    • A Scala view:                     • Linked lists (single link)                     • abst...
Scala Lists                    • A byte code view:                     • Case classes become inner classes:               ...
Scala Lists                • Heads and elements have the same typemercoledì 23 maggio 12
Scala Lists                    • What about mutable lists?                     • ListBuffer                       • Wrappe...
Scala Sets                    • Immutable sets                     • scala.collection.immutable.Set                     • ...
Scala Maps                    • Immutable:                     • small number of elements:                           scala...
Map and Sets examplesmercoledì 23 maggio 12
Primitive types boxingmercoledì 23 maggio 12
Primitive types and                               generics                    • Type parameters cannot be primitive in    ...
Basic tuning tipsmercoledì 23 maggio 12
Exploit tail recursion                    • Long recursion                     • Long stack                     • Performa...
Can’t exploit tail                               recursion?                    • If (and only if) you run out of stack spa...
Memory structure                    • Optimize for small, short lived objects                     • Anonymous functions:  ...
Memory structure                    • What about the perm gen?                     • Anonymous classes reused             ...
Which GC should I use?                    • Depends on your application requirements                     • The same consid...
Questions?mercoledì 23 maggio 12
Upcoming SlideShare
Loading in...5
×

Scala profiling

8,502

Published on

The presentation I gave at Riviera Scala Clojure Group on Scala profiling techniques (http://www.meetup.com/riviera-scala-clojure/events/54559732/)

Published in: Technology, News & Politics
2 Comments
15 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,502
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
2
Likes
15
Embeds 0
No embeds

No notes for slide

Scala profiling

  1. 1. SCALA PROFILING (for Java developers) Filippo Pacificimercoledì 23 maggio 12
  2. 2. Who am I • Filippo Pacifici • Twitter: OddId_ • mail: filippo.pacifici@gmail.com • Blog: http://outofmemoryblog.blogspot.com • One of the 9M devs thinking Java is not so bad • recently started looking at Scalamercoledì 23 maggio 12
  3. 3. What’s this all about? • Apply Java profiling methods to Scala programs • How do we deal with performance in Java? • Is it the same in Scala? • How do we optimize a JVM for Scala applications?mercoledì 23 maggio 12
  4. 4. Java profiling 101mercoledì 23 maggio 12
  5. 5. Java profiling 101 • Goals: • troubleshoot performance problems • estimate application performance • estimate application scalabilitymercoledì 23 maggio 12
  6. 6. Java profiling 101 • Ehi, I use Scala, why should I care about Java profiling? • Scala compiled in byte code and runs in a JVM • We can profile a Scala application as it was Java • We can use the same tools • I am not aware of alternativesmercoledì 23 maggio 12
  7. 7. Java methods profiling • Tracks methods invocations • Runtime instrumentation • Time analysismercoledì 23 maggio 12
  8. 8. Java memory profiling • Dumps heap content • Browse objects in the heap • Memory usage analysismercoledì 23 maggio 12
  9. 9. Profiling tools • Method profiling • Java Visual VM (embedded in JDK) • Yourkit, Dynatrace, etc. • Memory profiling • Eclipse MAT (www.eclipse.org/ mat) • Yourkit, Dynatrace, etc.mercoledì 23 maggio 12
  10. 10. Back to Scala... • We won’t find Scala specific constructs • Need to know how Scala is translated into bytecode • Goals: • Identify methods generated by Scala compilers • Characterize Scala data structures in memorymercoledì 23 maggio 12
  11. 11. Scala Functionsmercoledì 23 maggio 12
  12. 12. Short discouraging comparative demomercoledì 23 maggio 12
  13. 13. Scala functions vs byte code • Classic functions converted in methods • First class function do not exist in Java • Anonymous classes extending scala.runtime.AbstractFunction • apply method to execute.mercoledì 23 maggio 12
  14. 14. AbstractFunction • AbstractFunction2 • takes 2 input parameters • One apply method per combination of input and output types • example apply.mcFID • F= returns float • I takes one Int • D takes one Doublemercoledì 23 maggio 12
  15. 15. AbstractFunction • Find the call in the profile: • anonfun => instance of the anonymous class • main$1 => first anonymous class defined in main methodmercoledì 23 maggio 12
  16. 16. Functions in the heap • Each instance of AbstractFunction present in the heap • Very small impact • Statelessmercoledì 23 maggio 12
  17. 17. Where do we use them? • Our program did not contain any anonymous function, right? • AbstractFunction used: • For first class functions • For closures • For partially applied functions • To manage for loops blocks • To manage filter logic in for loopsmercoledì 23 maggio 12
  18. 18. Performance impact • Is this a performance impact? • Scala compiler performs optimizations: • Same anonymous functions reused (avoid multiple instantiations) • Anonymous functions doing the same thing are shared • Attention to partially applied: • New function created.mercoledì 23 maggio 12
  19. 19. Some examplesmercoledì 23 maggio 12
  20. 20. Scala data structures (Collections in heap dump)mercoledì 23 maggio 12
  21. 21. Scala Lists • A Scala view: • Linked lists (single link) • abstract class List + two case classes: :: and Nilmercoledì 23 maggio 12
  22. 22. Scala Lists • A byte code view: • Case classes become inner classes: • :: becomes $colon$colon • Nil becomes Nilmercoledì 23 maggio 12
  23. 23. Scala Lists • Heads and elements have the same typemercoledì 23 maggio 12
  24. 24. Scala Lists • What about mutable lists? • ListBuffer • Wrapper on a Linked List • Keeps an additional reference to the last element: last0mercoledì 23 maggio 12
  25. 25. Scala Sets • Immutable sets • scala.collection.immutable.Set • Case classes for different sizes • HashSet over 5 elementsmercoledì 23 maggio 12
  26. 26. Scala Maps • Immutable: • small number of elements: scala.collections.immutable.Map$MapN • N = number of elements • over 5 elements: scala.collection.immutable.HashMap • Mutable: scala.collection.mutable.HashMapmercoledì 23 maggio 12
  27. 27. Map and Sets examplesmercoledì 23 maggio 12
  28. 28. Primitive types boxingmercoledì 23 maggio 12
  29. 29. Primitive types and generics • Type parameters cannot be primitive in generic types. • Scala systematically boxes and unboxes them to Object • scala.runtime.BoxesRunTime methodsmercoledì 23 maggio 12
  30. 30. Basic tuning tipsmercoledì 23 maggio 12
  31. 31. Exploit tail recursion • Long recursion • Long stack • Performance impact on stack size • Scala compiler recognizes tail recursion • Recursive call must be the last operation of the method • Scala transforms it into iterative formmercoledì 23 maggio 12
  32. 32. Can’t exploit tail recursion? • If (and only if) you run out of stack space (frequent java.lang.StackOverflowError): • -Xss JVM option sets stack size • example: -Xss2048k • Normally limited at OS level • Each thread statically allocates stack size: • pay attentionmercoledì 23 maggio 12
  33. 33. Memory structure • Optimize for small, short lived objects • Anonymous functions: • small • frequently instantiated • Use a big young space • GC is fast and frequent • Objects do not get promotedmercoledì 23 maggio 12
  34. 34. Memory structure • What about the perm gen? • Anonymous classes reused • No insane usage of proxies • No specific issuesmercoledì 23 maggio 12
  35. 35. Which GC should I use? • Depends on your application requirements • The same consideration done for Java still hold • Need throughput : parallel GC • Need response time : CMS • You are brave : G1mercoledì 23 maggio 12
  36. 36. Questions?mercoledì 23 maggio 12

×