SlideShare a Scribd company logo
Tales About Scala Performance

© Copyright Performize-IT LTD.
About Me
My Name: Haim Yadid
Hard to Pronounce
Luckily it is meaningful
Haim => Life
Yadid => Friend

hybrid nick: lifey

this

::

::

::

::

::

:: Nil
© Copyright Performize-IT LTD.
Performize-IT

© Copyright Performize-IT LTD.
Performize-IT
Optimizing Software since 2007
Performance Bottlenecks

OutOfMemory

Crashes

Concurrency

GC Tuning

Training&Mentoring

© Copyright Performize-IT LTD.
Contact Me
lifey@performize-it.com
blog.performize-it.com
www.performize-it.com
https://github.com/lifey
http://il.linkedin.com/in/haimyadid
@lifeyx

© Copyright Performize-IT LTD.
Once Upon A Time

© Copyright Performize IT LTD.
Benchmarks by Google

So we are
done

© Copyright Performize-IT LTD.
So what is this talk about?

Best practices

Micro benchmarks?

Understanding

© Copyright Performize-IT LTD.
Understand
How to
Find performance problems
How to solve them
Reach a well performing production system

Prerequisites:
Familiarity with the JVM
Basic knowledge of Scala

© Copyright Performize-IT LTD.
Performance is all about
Methodology
Monitoring

Hotspots
Isolation
Analysis
Solution

Tools are your

Best Friends for this task

© Copyright Performize-IT LTD.
Scala Runs on the JVM
All JVM capabilities and tools still apply
Take your best friends with you

© Copyright Performize-IT LTD.
Premature Optimization
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely
I shall not optimize prematurely

© Copyright Performize-IT LTD.
Monitoring the JVM
Java management extensions (JMX)
on the same machine(Attach)
Remotely via command line params
Tools
JConsole
JVisualVM
Mission Control

© Copyright Performize-IT LTD.
Remote Monitoring - JMX
Add params to command line of profiled app
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=<port>
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

Recommend authentication and security, refer to
http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html
Production

© Copyright Performize-IT LTD.
A Tale about a Stack

© Copyright Performize IT LTD.
Your First Scala Function
Functional Programming
recursion
Easy to understand
Probably your 1st program in Scala will look like:

def sumOfSquares(st:Int , end : Int ) = {
if (st>end) 0
else st*st + sumOfSquares(st+1,end)
}

© Copyright Performize-IT LTD.
And your first exception will be:

java.lang.StackOverflowError

at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:8)
at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9)
at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9)
at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9)
at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9)
at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9)

© Copyright Performize-IT LTD.
Tail Recursion
Recursive call to the function must be
the value returned

	
  if	
  (number	
  ==	
  1)	
  1
	
  else	
  number	
  *	
  factorial	
  (number	
  -­‐	
  1)

© Copyright Performize-IT LTD.
Favor tail recursion
The JVM does not optimize recursion
Meaning extra call for every iteration
Limit on recursion depth
Scala compiler can optimize tail recursion!!
@tailrec def sumOfSquares(st:Int , end : Int,
sum = 0 ) = {
if (st>end) sum
else sumOfSquares(st+1,end,sum + st*st)
}

© Copyright Performize-IT LTD.
@tailrec Annotation
A compile time directive
fail compilation if tail recursion optimization cannot
be applied
Use whenever the fact tail recursion is used is
mandatory for performance and functionality

© Copyright Performize-IT LTD.
Stack Size
Ranges from 256k-1024k
Depending on platform and JVM version
What is it in your system?
java -XX:+PrintFlagsFinal -version |& grep ThreadStackSize

Tune thread stack to your needs
Example: -Xss1312k
Production

© Copyright Performize-IT LTD.
Stacks in Scala
Scala stack is just like Java Stack
jstack is your best friend
Scala terminology may be obscured
E.g. List will look like $colon$colon

© Copyright Performize-IT LTD.
JStack
Part of the JDK
Dumps stack traces of all live threads
Synopsis: jstack -l
Use when
Get a snapshot for program activity
detect deadlocks

© Copyright Performize-IT LTD.
Takipi’s Stackifier
www.stackifier.com

© Copyright Performize-IT LTD.
Humpty Dumpty sat on a heap,
Humpty Dumpty had anOutOfMemory flip.
All the king’s horses and all the king’s men
Couldn’t put Humpty together again
Max

Used

Heap
© Copyright Performize IT LTD.
In a Perfect World.....
Heap(Or Perm Gen) is depleted
-XX:+HeapDumpOnOutOfMemoryError
Scala code does not have larger memory footprint
Scala code may have larger permgen footprint

Production

© Copyright Performize-IT LTD.
MAT
MAT - Memory Analyzer Tool
A very powerful tool analyzing heap dumps

Use to investigate :
Memory leaks
OutOfMemory errors
Memory footprint

Alternatives
Yourkit /JProbe/JProfiler (Commercial)
VisualVM(JDK)
JHat(JDK)

© Copyright Performize-IT LTD.
MAT-name-resolver
Add-on for MAT
Helps MAT understand Scala
Developed by Iulian Dragos from Typesafe
Github project https://github.com/dragos/MAT-name-resolver

© Copyright Performize-IT LTD.
List[Int] ?

© Copyright Performize-IT LTD.
OutOfMemory Perm Space
Class byte code resides in PermGen
Scala will use more perm space
You can write small piece of code
which will create a lot of byte-code

© Copyright Performize-IT LTD.
@ScalaSignature
@ScalaSignature(bytes="...
Meta data needed for:
Reflection
Compilation

Larger class files

© Copyright Performize-IT LTD.
More classes
Each closure is actually a JVM class
Implicit conversions are classes
Companion objects are also classes

© Copyright Performize-IT LTD.
Well
ClosureExample$.class
object ClosureExample extends App {
val f = (x: Int) => x*x
println (s"closure ${f(5)}");
}

package com.performizeit.scalapeno.demos;

ClosureExample.class

import scala.Function0;
import scala.Function1;
package com.performizeit.scalapeno.demos;
import scala.LowPriorityImplicits;
import scala.Predef.;
import scala.App;
import scala.StringContext;
import scala.App.class;
import scala.reflect.ScalaSignature;
import scala.DelayedInit;
import scala.runtime.AbstractFunction0;
import scala.Function0;
import scala.runtime.BoxedUnit;
import scala.Function1;
import scala.runtime.BoxesRunTime;
import scala.Serializable;
package com.performizeit.scalapeno.demos;
import scala.collection.mutable.ListBuffer;
@ScalaSignature(bytes="006001035:Q!001002t002-tab0217pgV024X-022=b[BdWM003002004t005)A-Z7pg*021QAB001ng016fG.0319f]>T!a002005002031A,'OZ8s[&TX-033;013003%t1aY8n007001001"001D
import scala.runtime.AbstractFunction1.mcII.sp;
007016003t1QA004002t002=021ab0217pgV024X-022=b[BdWmE002016!Y001"!005013016003IQ021aE001006g016fG.Y005003+I021a!0218z%0264007CAt030023tA"CA002BaBDQAG007005002mta
import scala.Serializable;
import scala.runtime.BoxedUnit;
001P5oSRtD#A006t017ui!031!C001=005ta-F001 !021t002Et022n005005022"!003$v]016$030n03482!tt2%003002%%t031021J034;tr031j001025!003 003t1007005")
import scala.runtime.AbstractFunction1.mcII.sp;
public final class ClosureExample
public final classpackage com.performizeit.scalapeno.demos;
ClosureExample$
{
public
implements Appfinal class ClosureExample$$anonfun$1 extends AbstractFunction1.mcII.sp
public static void main(String[] paramArrayOfString)
implements Serializable
{
import scala.Function1;
{
{
public static final MODULE$;
import scala.LowPriorityImplicits;
ClosureExample..MODULE$.main(paramArrayOfString);
public static scala.Predef.;
private Function1<Object, Object> f;
import final long serialVersionUID = 0L;
}
private final long executionStart;
import scala.StringContext;
public final scala.runtime.AbstractFunction0;
private String[] importint apply(int x)
scala$App$$_args;
public static void delayedInit(Function0<BoxedUnit> paramFunction0)
{
private final ListBuffer<Function0<BoxedUnit>> scala$App$$initCode;
import scala.runtime.BoxedUnit;
{
return apply$mcII$sp(x); }
import scala.runtime.BoxesRunTime;
ClosureExample..MODULE$.delayedInit(paramFunction0);
public int apply$mcII$sp(int x) { return x * x; }
static
}
{
public final class ClosureExample$delayedInit$body extends AbstractFunction0
new (); }
{
public static String[] args()
}
private final ClosureExample. $outer;
{

ClosureExample$$anonfun$1.class

ClosureExample$delayedInit$body.class

return ClosureExample..MODULE$.args();
public long executionStart() Object apply()
public final
{
{
return this.executionStart; }
this.$outer.f_$eq(new ClosureExample..anonfun.1());
public static void scala$App$_setter_$executionStart_$eq(long paramLong)
public String[] scala$App$$_args() { return this.scala$App$$_args; }
Predef..MODULE$.println(new StringContext(Predef..MODULE$.wrapRefArray((Object[])new String[] { "closure ", "" })).s(Predef..MODULE$.genericWrapArray(new Object[]
{
public void scala$App$$_args_$eq(String[] x$1) { this.scala$App$$_args = x$1;})));
{ BoxesRunTime.boxToInteger(this.$outer.f().apply$mcII$sp(5)) }
ClosureExample..MODULE$.scala$App$_setter_$executionStart_$eq(paramLong);
public ListBuffer<Function0<BoxedUnit>> scala$App$$initCode() { return this.scala$App$$initCode; }
}
public void scala$App$_setter_$executionStart_$eq(long x$1) { this.executionStart = x$1; }
return BoxedUnit.UNIT;
public void scala$App$_setter_$scala$App$$initCode_$eq(ListBuffer x$1) { this.scala$App$$initCode = x$1; }
}
public static long executionStart()
public String[] args() { return App.class.args(this); }
{
public void delayedInit(Function0<BoxedUnit> body) { App.class.delayedInit(this, body); }
public ClosureExample$delayedInit$body(ClosureExample. $outer)
return ClosureExample..MODULE$.executionStart();
public void main(String[] args) { App.class.main(this, args); }
{
}
public Function1<Object, Object> f() { return this.f; }
public void f_$eq(Function1 x$1) { this.f = x$1; }
public static Function1<Object, Object> f()
{
return ClosureExample..MODULE$.f();
}
}

public static class delayedInit$body extends AbstractFunction0
{
private final ClosureExample. $outer;
public final Object apply()
{
this.$outer.f_$eq(new ClosureExample..anonfun.1());
Predef..MODULE$.println(new StringContext(Predef..MODULE$.wrapRefArray((Object[])new String[] { "closure ", "" })).s(Predef..MODULE$.genericWrapArray(new Object[] { BoxesRunTime.boxToInteger(this.$outer.f().apply
$mcII$sp(5)) })));
return BoxedUnit.UNIT;
}
public delayedInit$body(ClosureExample. $outer)
{
}
}
}

© Copyright Performize-IT LTD.
@specialized
Generics implemented by type erasure
For primitive types this means : Boxing/Unboxing
Performance hit
Large memory footprint

@specialized annotation enables specialized
implementations

© Copyright Performize-IT LTD.
What about code cache?
Code cache hold optimized assembly code
Should be large enough to hold
If you need more perm gen
You may need more code cache
-XX:CodeCacheSize=
Monitor it via JMX

Production

© Copyright Performize-IT LTD.
@specialized Nightmare

class SpecializeNightmare {
trait S1[@specialized A, @specialized B] { def f(p1:A): Unit }
}

Generates
165 classes
Don’t try with 3,4,5

© Copyright Performize-IT LTD.
OutOfMemory Perm Gen Space
Congrats you have a perm gen OOM
-XX:MaxPermSize=1024m
(Or -J-XX:MaxPermSize=1024m
if you use Scala command line)

Production

© Copyright Performize-IT LTD.
Oh dear! Oh dear! I shall
be too late!

© Copyright Performize IT LTD.
-optimise
A scalac command line parameter
Performs optimizations of bytecode
Inlining boxing/unboxing elimination etc
Improves performance
Slower compilation

Production

© Copyright Performize-IT LTD.
Inlining
Scala uses information it has in compile time
To know which methods can be inlined
It can do better job than the JVM
Automatic when you -optimise

Production

© Copyright Performize-IT LTD.
Inlining Visibility
On scala compiler level
Add -Ylog:inline to see what inlined
scalac -optimise -Ylog:inline -d ../bin

/ClosureExampleInline.scala |& grep inlined

com/performizeit/scalapeno/demos

[log inliner] inlined
ClosureExampleInline.<init> // 1 inlined:
ClosureExampleInline.delayedInit
[log inliner] inlined com.performizeit.scalapeno.demos.ClosureExampleInline$$anonfun$f
$1.apply // 1 inlined: com.performizeit.scalapeno.demos.anonfun$f$1.apply$mcII$sp
com.performizeit.scalapeno.demos.

com.performizeit.scalapeno.demos.

© Copyright Performize-IT LTD.
Inlining Visibility JVM
JIT Compiler compiler options
Not recommended for production
-XX:+PrintCompilation
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintInlining

! Prod

© Copyright Performize-IT LTD.
@inline
You may direct the compiler to inline a method
Usually you will not need it the compiler will do it
anyway.
Or the JVM will do it anyway
No real need to clutter the code....

@inline final def f = (x: Int) => x*x

© Copyright Performize-IT LTD.
Member accessors
Get/Set
getters to a val fields
getters&setters to var fields
Will you pay for this?

Nope !
JVM inlines accessor methods (by default)
If you insist on penalty
-XX:-UseFastAccessorMethods

© Copyright Performize-IT LTD.
Parallel Collections
ParArray
ParVector
mutable.ParHashMap
mutable.ParHashSet
immutable.ParHashMap
immutable.ParHashSet
ParRange
ParTrieMap

© Copyright Performize-IT LTD.
Parallel Collections
Apply only when has a location is a hotspot
Very easy to use
behind the scenes ForkJoinFramework (Java 6)
Dangerous when code :
Only
has side effects
Non associative

when proven
to improve

Easy to use
val v = Vector(Range(0,10000000)).flatten
v.par.map(_ + 1)

© Copyright Performize-IT LTD.
Profiler - JVisualVM
Part of the JDK
A profiler
Use when
Want to identify hotspot
Analyze memory allocation bottlenecks

Alternatives
Yourkit (Commercial)
JProbe(Commercial)
JProfiler(Commercial)

© Copyright Performize-IT LTD.
Sampling vs Instrumentation
Sampling - sample application threads and stack traces
to get statistics
Instrumentation - modify byte code to record times and
invocation counts

© Copyright Performize-IT LTD.
Scala Stacks revisited
while (true) {
var a = List(Range(0,1000)).flatten
// println(a)
for (i <- 1 to 10 ) {
a = a :+ i
println(a.last)
}
}

© Copyright Performize-IT LTD.
Garbage Collection

© Copyright Performize IT LTD.
Immutability
Immutability may cause more objects allocation
Not necessary a performance hit
Short lived objects
GC handles them efficiently
Escape analysis

Parallelization!!!

© Copyright Performize-IT LTD.
VisualVM (allocation hotspots)
Find locations
large amounts of bytes are being allocated.
large number of objects being allocation

© Copyright Performize-IT LTD.
Large (im)mutable state
You have a huge graph which changes gradually
Eventually end up in Old Generation
A small change may cause huge impact on state
That may screw up GC

© Copyright Performize-IT LTD.
GC Visibility
GC can be visualized partially through JMX
The best way to do get the whole picture is by GC
logs
-Xloggc:<log file name>
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps

Java 7 supports a “rolling appender”
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=<#files>
-XX:GCLogFileSize=<number>M

Prod

© Copyright Performize-IT LTD.
GCViewer
Analysis GC logs
Use when:
Experience GC problems
Is GC efficient ?(throughput )
Does GC stops application ( pause time)

Alternatives
Cesnum (Commercial)

© Copyright Performize-IT LTD.
And They Lived Happily
Ever After

© Copyright Performize IT LTD.
slides /: (_ + _)
Don’t be afraid of Scala
You will be able to optimize large scale apps
Optimize where needed
You need to (Java =>) Scala Yourself
ATM - Know Java to optimize Scala

© Copyright Performize-IT LTD.
Q&A

© Copyright Performize IT LTD.
The End

© Copyright Performize IT LTD.

More Related Content

Similar to Tales About Scala Performance

RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
Apache MXNet EcoSystem - ACNA2018
Apache MXNet EcoSystem - ACNA2018Apache MXNet EcoSystem - ACNA2018
Apache MXNet EcoSystem - ACNA2018
Apache MXNet
 
Open mp
Open mpOpen mp
Open mp
Gopi Saiteja
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
AMD Developer Central
 
Speed up your Machine Learning workflows with build-in algorithms
Speed up your Machine Learning workflows with build-in algorithmsSpeed up your Machine Learning workflows with build-in algorithms
Speed up your Machine Learning workflows with build-in algorithms
Julien SIMON
 
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
Amazon Web Services
 
Lambda The Extreme: Test-Driving a Functional Language
Lambda The Extreme: Test-Driving a Functional LanguageLambda The Extreme: Test-Driving a Functional Language
Lambda The Extreme: Test-Driving a Functional Language
Accenture | SolutionsIQ
 
Developer Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern softwareDeveloper Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern software
Kosala Nuwan Perera
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
Guillaume Laforge
 
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
Amazon Web Services
 
Decompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 PresentationDecompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 Presentation
James Hamilton
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
Mark Menard
 
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
Amazon Web Services
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector
 
Compile ahead of time. It's fine?
Compile ahead of time. It's fine?Compile ahead of time. It's fine?
Compile ahead of time. It's fine?
Dmitry Chuyko
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
HamletDRC
 
APEX Connect 2019 - successful application development
APEX Connect 2019 - successful application developmentAPEX Connect 2019 - successful application development
APEX Connect 2019 - successful application development
Connor McDonald
 
APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101
Connor McDonald
 
From Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMakerFrom Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMaker
Amazon Web Services
 

Similar to Tales About Scala Performance (20)

RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Apache MXNet EcoSystem - ACNA2018
Apache MXNet EcoSystem - ACNA2018Apache MXNet EcoSystem - ACNA2018
Apache MXNet EcoSystem - ACNA2018
 
Open mp
Open mpOpen mp
Open mp
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
Keynote (Nandini Ramani) - The Role of Java in Heterogeneous Computing & How ...
 
Speed up your Machine Learning workflows with build-in algorithms
Speed up your Machine Learning workflows with build-in algorithmsSpeed up your Machine Learning workflows with build-in algorithms
Speed up your Machine Learning workflows with build-in algorithms
 
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
Speed up your Machine Learning workflows with built-in algorithms - Tel Aviv ...
 
Lambda The Extreme: Test-Driving a Functional Language
Lambda The Extreme: Test-Driving a Functional LanguageLambda The Extreme: Test-Driving a Functional Language
Lambda The Extreme: Test-Driving a Functional Language
 
Developer Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern softwareDeveloper Best Practices - The secret sauce for coding modern software
Developer Best Practices - The secret sauce for coding modern software
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
Simplify Machine Learning with the Deep Learning AMI | AWS Floor28
 
Decompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 PresentationDecompiling Java - SCAM2009 Presentation
Decompiling Java - SCAM2009 Presentation
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
BDA301 Working with Machine Learning in Amazon SageMaker: Algorithms, Models,...
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Compile ahead of time. It's fine?
Compile ahead of time. It's fine?Compile ahead of time. It's fine?
Compile ahead of time. It's fine?
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
APEX Connect 2019 - successful application development
APEX Connect 2019 - successful application developmentAPEX Connect 2019 - successful application development
APEX Connect 2019 - successful application development
 
APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101APEX Connect 2019 - SQL Tuning 101
APEX Connect 2019 - SQL Tuning 101
 
From Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMakerFrom Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMaker
 

More from Haim Yadid

Loom me up Scotty! Project Loom - What's in it for Me?
Loom me up Scotty!  Project Loom - What's in it for Me?Loom me up Scotty!  Project Loom - What's in it for Me?
Loom me up Scotty! Project Loom - What's in it for Me?
Haim Yadid
 
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
Haim Yadid
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Haim Yadid
 
“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection
Haim Yadid
 
Java Memory Structure
Java Memory Structure Java Memory Structure
Java Memory Structure
Haim Yadid
 
Basic JVM Troubleshooting With Jmx
Basic JVM Troubleshooting With JmxBasic JVM Troubleshooting With Jmx
Basic JVM Troubleshooting With Jmx
Haim Yadid
 
The Freelancer Journey
The Freelancer JourneyThe Freelancer Journey
The Freelancer Journey
Haim Yadid
 
Building microservices with Kotlin
Building microservices with KotlinBuilding microservices with Kotlin
Building microservices with Kotlin
Haim Yadid
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
Haim Yadid
 
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
Haim Yadid
 
mjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingmjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profiling
Haim Yadid
 
Java 8 Launch - MetaSpaces
Java 8 Launch - MetaSpacesJava 8 Launch - MetaSpaces
Java 8 Launch - MetaSpaces
Haim Yadid
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped Lock
Haim Yadid
 
The Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFuturesThe Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFutures
Haim Yadid
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
Haim Yadid
 
A short Intro. to Java Mission Control
A short Intro. to Java Mission ControlA short Intro. to Java Mission Control
A short Intro. to Java Mission Control
Haim Yadid
 
Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions
Haim Yadid
 
Israeli JUG - IL JUG presentation
Israeli JUG -  IL JUG presentation Israeli JUG -  IL JUG presentation
Israeli JUG - IL JUG presentation
Haim Yadid
 

More from Haim Yadid (18)

Loom me up Scotty! Project Loom - What's in it for Me?
Loom me up Scotty!  Project Loom - What's in it for Me?Loom me up Scotty!  Project Loom - What's in it for Me?
Loom me up Scotty! Project Loom - What's in it for Me?
 
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
 
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the UglyKotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
Kotlin Backend Development 6 Yrs Recap. The Good, the Bad and the Ugly
 
“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection
 
Java Memory Structure
Java Memory Structure Java Memory Structure
Java Memory Structure
 
Basic JVM Troubleshooting With Jmx
Basic JVM Troubleshooting With JmxBasic JVM Troubleshooting With Jmx
Basic JVM Troubleshooting With Jmx
 
The Freelancer Journey
The Freelancer JourneyThe Freelancer Journey
The Freelancer Journey
 
Building microservices with Kotlin
Building microservices with KotlinBuilding microservices with Kotlin
Building microservices with Kotlin
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
JVM Garbage Collection logs, you do not want to ignore them! - Reversim Summi...
 
mjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingmjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profiling
 
Java 8 Launch - MetaSpaces
Java 8 Launch - MetaSpacesJava 8 Launch - MetaSpaces
Java 8 Launch - MetaSpaces
 
Java 8 - Stamped Lock
Java 8 - Stamped LockJava 8 - Stamped Lock
Java 8 - Stamped Lock
 
The Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFuturesThe Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFutures
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
A short Intro. to Java Mission Control
A short Intro. to Java Mission ControlA short Intro. to Java Mission Control
A short Intro. to Java Mission Control
 
Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions Java Enterprise Edition Concurrency Misconceptions
Java Enterprise Edition Concurrency Misconceptions
 
Israeli JUG - IL JUG presentation
Israeli JUG -  IL JUG presentation Israeli JUG -  IL JUG presentation
Israeli JUG - IL JUG presentation
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 

Tales About Scala Performance

  • 1. Tales About Scala Performance © Copyright Performize-IT LTD.
  • 2. About Me My Name: Haim Yadid Hard to Pronounce Luckily it is meaningful Haim => Life Yadid => Friend hybrid nick: lifey this :: :: :: :: :: :: Nil © Copyright Performize-IT LTD.
  • 4. Performize-IT Optimizing Software since 2007 Performance Bottlenecks OutOfMemory Crashes Concurrency GC Tuning Training&Mentoring © Copyright Performize-IT LTD.
  • 6. Once Upon A Time © Copyright Performize IT LTD.
  • 7. Benchmarks by Google So we are done © Copyright Performize-IT LTD.
  • 8. So what is this talk about? Best practices Micro benchmarks? Understanding © Copyright Performize-IT LTD.
  • 9. Understand How to Find performance problems How to solve them Reach a well performing production system Prerequisites: Familiarity with the JVM Basic knowledge of Scala © Copyright Performize-IT LTD.
  • 10. Performance is all about Methodology Monitoring Hotspots Isolation Analysis Solution Tools are your Best Friends for this task © Copyright Performize-IT LTD.
  • 11. Scala Runs on the JVM All JVM capabilities and tools still apply Take your best friends with you © Copyright Performize-IT LTD.
  • 12. Premature Optimization I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely I shall not optimize prematurely © Copyright Performize-IT LTD.
  • 13. Monitoring the JVM Java management extensions (JMX) on the same machine(Attach) Remotely via command line params Tools JConsole JVisualVM Mission Control © Copyright Performize-IT LTD.
  • 14. Remote Monitoring - JMX Add params to command line of profiled app -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=<port> -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false Recommend authentication and security, refer to http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html Production © Copyright Performize-IT LTD.
  • 15. A Tale about a Stack © Copyright Performize IT LTD.
  • 16. Your First Scala Function Functional Programming recursion Easy to understand Probably your 1st program in Scala will look like: def sumOfSquares(st:Int , end : Int ) = { if (st>end) 0 else st*st + sumOfSquares(st+1,end) } © Copyright Performize-IT LTD.
  • 17. And your first exception will be: java.lang.StackOverflowError at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:8) at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9) at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9) at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9) at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9) at com.performizeit.scalapeno.demos.TailRecusionTale$.calculateSumOfSquares(TailRecusionTale.scala:9) © Copyright Performize-IT LTD.
  • 18. Tail Recursion Recursive call to the function must be the value returned  if  (number  ==  1)  1  else  number  *  factorial  (number  -­‐  1) © Copyright Performize-IT LTD.
  • 19. Favor tail recursion The JVM does not optimize recursion Meaning extra call for every iteration Limit on recursion depth Scala compiler can optimize tail recursion!! @tailrec def sumOfSquares(st:Int , end : Int, sum = 0 ) = { if (st>end) sum else sumOfSquares(st+1,end,sum + st*st) } © Copyright Performize-IT LTD.
  • 20. @tailrec Annotation A compile time directive fail compilation if tail recursion optimization cannot be applied Use whenever the fact tail recursion is used is mandatory for performance and functionality © Copyright Performize-IT LTD.
  • 21. Stack Size Ranges from 256k-1024k Depending on platform and JVM version What is it in your system? java -XX:+PrintFlagsFinal -version |& grep ThreadStackSize Tune thread stack to your needs Example: -Xss1312k Production © Copyright Performize-IT LTD.
  • 22. Stacks in Scala Scala stack is just like Java Stack jstack is your best friend Scala terminology may be obscured E.g. List will look like $colon$colon © Copyright Performize-IT LTD.
  • 23. JStack Part of the JDK Dumps stack traces of all live threads Synopsis: jstack -l Use when Get a snapshot for program activity detect deadlocks © Copyright Performize-IT LTD.
  • 25. Humpty Dumpty sat on a heap, Humpty Dumpty had anOutOfMemory flip. All the king’s horses and all the king’s men Couldn’t put Humpty together again Max Used Heap © Copyright Performize IT LTD.
  • 26. In a Perfect World..... Heap(Or Perm Gen) is depleted -XX:+HeapDumpOnOutOfMemoryError Scala code does not have larger memory footprint Scala code may have larger permgen footprint Production © Copyright Performize-IT LTD.
  • 27. MAT MAT - Memory Analyzer Tool A very powerful tool analyzing heap dumps Use to investigate : Memory leaks OutOfMemory errors Memory footprint Alternatives Yourkit /JProbe/JProfiler (Commercial) VisualVM(JDK) JHat(JDK) © Copyright Performize-IT LTD.
  • 28. MAT-name-resolver Add-on for MAT Helps MAT understand Scala Developed by Iulian Dragos from Typesafe Github project https://github.com/dragos/MAT-name-resolver © Copyright Performize-IT LTD.
  • 29. List[Int] ? © Copyright Performize-IT LTD.
  • 30. OutOfMemory Perm Space Class byte code resides in PermGen Scala will use more perm space You can write small piece of code which will create a lot of byte-code © Copyright Performize-IT LTD.
  • 31. @ScalaSignature @ScalaSignature(bytes="... Meta data needed for: Reflection Compilation Larger class files © Copyright Performize-IT LTD.
  • 32. More classes Each closure is actually a JVM class Implicit conversions are classes Companion objects are also classes © Copyright Performize-IT LTD.
  • 33. Well ClosureExample$.class object ClosureExample extends App { val f = (x: Int) => x*x println (s"closure ${f(5)}"); } package com.performizeit.scalapeno.demos; ClosureExample.class import scala.Function0; import scala.Function1; package com.performizeit.scalapeno.demos; import scala.LowPriorityImplicits; import scala.Predef.; import scala.App; import scala.StringContext; import scala.App.class; import scala.reflect.ScalaSignature; import scala.DelayedInit; import scala.runtime.AbstractFunction0; import scala.Function0; import scala.runtime.BoxedUnit; import scala.Function1; import scala.runtime.BoxesRunTime; import scala.Serializable; package com.performizeit.scalapeno.demos; import scala.collection.mutable.ListBuffer; @ScalaSignature(bytes="006001035:Q!001002t002-tab0217pgV024X-022=b[BdWM003002004t005)A-Z7pg*021QAB001ng016fG.0319f]>T!a002005002031A,'OZ8s[&TX-033;013003%t1aY8n007001001"001D import scala.runtime.AbstractFunction1.mcII.sp; 007016003t1QA004002t002=021ab0217pgV024X-022=b[BdWmE002016!Y001"!005013016003IQ021aE001006g016fG.Y005003+I021a!0218z%0264007CAt030023tA"CA002BaBDQAG007005002mta import scala.Serializable; import scala.runtime.BoxedUnit; 001P5oSRtD#A006t017ui!031!C001=005ta-F001 !021t002Et022n005005022"!003$v]016$030n03482!tt2%003002%%t031021J034;tr031j001025!003 003t1007005") import scala.runtime.AbstractFunction1.mcII.sp; public final class ClosureExample public final classpackage com.performizeit.scalapeno.demos; ClosureExample$ { public implements Appfinal class ClosureExample$$anonfun$1 extends AbstractFunction1.mcII.sp public static void main(String[] paramArrayOfString) implements Serializable { import scala.Function1; { { public static final MODULE$; import scala.LowPriorityImplicits; ClosureExample..MODULE$.main(paramArrayOfString); public static scala.Predef.; private Function1<Object, Object> f; import final long serialVersionUID = 0L; } private final long executionStart; import scala.StringContext; public final scala.runtime.AbstractFunction0; private String[] importint apply(int x) scala$App$$_args; public static void delayedInit(Function0<BoxedUnit> paramFunction0) { private final ListBuffer<Function0<BoxedUnit>> scala$App$$initCode; import scala.runtime.BoxedUnit; { return apply$mcII$sp(x); } import scala.runtime.BoxesRunTime; ClosureExample..MODULE$.delayedInit(paramFunction0); public int apply$mcII$sp(int x) { return x * x; } static } { public final class ClosureExample$delayedInit$body extends AbstractFunction0 new (); } { public static String[] args() } private final ClosureExample. $outer; { ClosureExample$$anonfun$1.class ClosureExample$delayedInit$body.class return ClosureExample..MODULE$.args(); public long executionStart() Object apply() public final { { return this.executionStart; } this.$outer.f_$eq(new ClosureExample..anonfun.1()); public static void scala$App$_setter_$executionStart_$eq(long paramLong) public String[] scala$App$$_args() { return this.scala$App$$_args; } Predef..MODULE$.println(new StringContext(Predef..MODULE$.wrapRefArray((Object[])new String[] { "closure ", "" })).s(Predef..MODULE$.genericWrapArray(new Object[] { public void scala$App$$_args_$eq(String[] x$1) { this.scala$App$$_args = x$1;}))); { BoxesRunTime.boxToInteger(this.$outer.f().apply$mcII$sp(5)) } ClosureExample..MODULE$.scala$App$_setter_$executionStart_$eq(paramLong); public ListBuffer<Function0<BoxedUnit>> scala$App$$initCode() { return this.scala$App$$initCode; } } public void scala$App$_setter_$executionStart_$eq(long x$1) { this.executionStart = x$1; } return BoxedUnit.UNIT; public void scala$App$_setter_$scala$App$$initCode_$eq(ListBuffer x$1) { this.scala$App$$initCode = x$1; } } public static long executionStart() public String[] args() { return App.class.args(this); } { public void delayedInit(Function0<BoxedUnit> body) { App.class.delayedInit(this, body); } public ClosureExample$delayedInit$body(ClosureExample. $outer) return ClosureExample..MODULE$.executionStart(); public void main(String[] args) { App.class.main(this, args); } { } public Function1<Object, Object> f() { return this.f; } public void f_$eq(Function1 x$1) { this.f = x$1; } public static Function1<Object, Object> f() { return ClosureExample..MODULE$.f(); } } public static class delayedInit$body extends AbstractFunction0 { private final ClosureExample. $outer; public final Object apply() { this.$outer.f_$eq(new ClosureExample..anonfun.1()); Predef..MODULE$.println(new StringContext(Predef..MODULE$.wrapRefArray((Object[])new String[] { "closure ", "" })).s(Predef..MODULE$.genericWrapArray(new Object[] { BoxesRunTime.boxToInteger(this.$outer.f().apply $mcII$sp(5)) }))); return BoxedUnit.UNIT; } public delayedInit$body(ClosureExample. $outer) { } } } © Copyright Performize-IT LTD.
  • 34. @specialized Generics implemented by type erasure For primitive types this means : Boxing/Unboxing Performance hit Large memory footprint @specialized annotation enables specialized implementations © Copyright Performize-IT LTD.
  • 35. What about code cache? Code cache hold optimized assembly code Should be large enough to hold If you need more perm gen You may need more code cache -XX:CodeCacheSize= Monitor it via JMX Production © Copyright Performize-IT LTD.
  • 36. @specialized Nightmare class SpecializeNightmare { trait S1[@specialized A, @specialized B] { def f(p1:A): Unit } } Generates 165 classes Don’t try with 3,4,5 © Copyright Performize-IT LTD.
  • 37. OutOfMemory Perm Gen Space Congrats you have a perm gen OOM -XX:MaxPermSize=1024m (Or -J-XX:MaxPermSize=1024m if you use Scala command line) Production © Copyright Performize-IT LTD.
  • 38. Oh dear! Oh dear! I shall be too late! © Copyright Performize IT LTD.
  • 39. -optimise A scalac command line parameter Performs optimizations of bytecode Inlining boxing/unboxing elimination etc Improves performance Slower compilation Production © Copyright Performize-IT LTD.
  • 40. Inlining Scala uses information it has in compile time To know which methods can be inlined It can do better job than the JVM Automatic when you -optimise Production © Copyright Performize-IT LTD.
  • 41. Inlining Visibility On scala compiler level Add -Ylog:inline to see what inlined scalac -optimise -Ylog:inline -d ../bin /ClosureExampleInline.scala |& grep inlined com/performizeit/scalapeno/demos [log inliner] inlined ClosureExampleInline.<init> // 1 inlined: ClosureExampleInline.delayedInit [log inliner] inlined com.performizeit.scalapeno.demos.ClosureExampleInline$$anonfun$f $1.apply // 1 inlined: com.performizeit.scalapeno.demos.anonfun$f$1.apply$mcII$sp com.performizeit.scalapeno.demos. com.performizeit.scalapeno.demos. © Copyright Performize-IT LTD.
  • 42. Inlining Visibility JVM JIT Compiler compiler options Not recommended for production -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining ! Prod © Copyright Performize-IT LTD.
  • 43. @inline You may direct the compiler to inline a method Usually you will not need it the compiler will do it anyway. Or the JVM will do it anyway No real need to clutter the code.... @inline final def f = (x: Int) => x*x © Copyright Performize-IT LTD.
  • 44. Member accessors Get/Set getters to a val fields getters&setters to var fields Will you pay for this? Nope ! JVM inlines accessor methods (by default) If you insist on penalty -XX:-UseFastAccessorMethods © Copyright Performize-IT LTD.
  • 46. Parallel Collections Apply only when has a location is a hotspot Very easy to use behind the scenes ForkJoinFramework (Java 6) Dangerous when code : Only has side effects Non associative when proven to improve Easy to use val v = Vector(Range(0,10000000)).flatten v.par.map(_ + 1) © Copyright Performize-IT LTD.
  • 47. Profiler - JVisualVM Part of the JDK A profiler Use when Want to identify hotspot Analyze memory allocation bottlenecks Alternatives Yourkit (Commercial) JProbe(Commercial) JProfiler(Commercial) © Copyright Performize-IT LTD.
  • 48. Sampling vs Instrumentation Sampling - sample application threads and stack traces to get statistics Instrumentation - modify byte code to record times and invocation counts © Copyright Performize-IT LTD.
  • 49. Scala Stacks revisited while (true) { var a = List(Range(0,1000)).flatten // println(a) for (i <- 1 to 10 ) { a = a :+ i println(a.last) } } © Copyright Performize-IT LTD.
  • 50. Garbage Collection © Copyright Performize IT LTD.
  • 51. Immutability Immutability may cause more objects allocation Not necessary a performance hit Short lived objects GC handles them efficiently Escape analysis Parallelization!!! © Copyright Performize-IT LTD.
  • 52. VisualVM (allocation hotspots) Find locations large amounts of bytes are being allocated. large number of objects being allocation © Copyright Performize-IT LTD.
  • 53. Large (im)mutable state You have a huge graph which changes gradually Eventually end up in Old Generation A small change may cause huge impact on state That may screw up GC © Copyright Performize-IT LTD.
  • 54. GC Visibility GC can be visualized partially through JMX The best way to do get the whole picture is by GC logs -Xloggc:<log file name> -XX:+PrintGCDetails -XX:+PrintGCDateStamps Java 7 supports a “rolling appender” -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=<#files> -XX:GCLogFileSize=<number>M Prod © Copyright Performize-IT LTD.
  • 55. GCViewer Analysis GC logs Use when: Experience GC problems Is GC efficient ?(throughput ) Does GC stops application ( pause time) Alternatives Cesnum (Commercial) © Copyright Performize-IT LTD.
  • 56. And They Lived Happily Ever After © Copyright Performize IT LTD.
  • 57. slides /: (_ + _) Don’t be afraid of Scala You will be able to optimize large scale apps Optimize where needed You need to (Java =>) Scala Yourself ATM - Know Java to optimize Scala © Copyright Performize-IT LTD.
  • 59. The End © Copyright Performize IT LTD.