performance comparisons of java and groovy -...

32
Performance comparisons of Java and Groovy Jochen Theodorou Groovy Project Tech Lead SpringSource Germany

Upload: vuongque

Post on 13-May-2018

284 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Performance comparisons of Java and Groovy

Jochen TheodorouGroovy Project Tech LeadSpringSource Germany

Page 2: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

Groovy is a strong and dynamic typed language with static

elements

Page 3: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

● Dynamic language● Has an MOP (add/remove/update methods)● Instance based multimethods● Multi threaded (uses java threads)● Runtime class generation or compilation to file● Joint compilation of Groovy and Java (or Scala)● Compiles to normal classes with all signatures

visible

Page 4: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

● Tight integration with Java (Groovy extends Java extends Groovy)

● Support for generic signature● Support for annotations● In Groovy 1.7: Inner classes● Overloaded Methods● Support for closures● Duck typing

Page 5: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

● Dynamic typing● Static typing possible, but with a different

concept● Supports java security model● Native Java Bean property support

Page 6: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

● Array init syntax is not supported● Semis are optional● No generics testing in expressions● Parents are partially optional● Native lists and maps● Additional loop constructs● Additional methods on standard classes

Differences to Java:

Page 7: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

Important Projects:

Grails for Web Applications

Griffon for Swing Applications

Gradle for Buildsystems

Gparalizer for Grid Computing

Page 8: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

● Class stores an CallSite[]● Callsite becomes invalid on meta class

operations● meta class might be changed from a different

thread● Execution method might be precreated, use

reflection or runtime generated

Groovy 1.6 Callsite Caching:

Page 9: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

MetaClassCallsite

Groovy Intro

Callsite caching

fib(n) select method

create callable

Invoke

cache

Created at RuntimeCreated at Runtime

Page 10: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Groovy Intro

Multi threaded changes to meta classes require a volatile or synchronized checke at the call site

Problem:

Page 11: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

Loops are often optimized by loop unrolling

int x = 0;for(int i=0; i<3; i++) x++;

x=3;int x = 0;x++;x++;x++;

Page 12: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

My Example:

int c = Integer.parseInt(args[0]); int x = 0; while (x<c) x++;

This loop can be removed at runtime!

Page 13: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

Proof:

6 7 8 90.040

0.050

0.060

0.070

0.080

0.090

0.100

java_x

number of loops in 10 x̂

time

Page 14: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

Situation in Groovy:

6 7 8 90.000

5.000

10.000

15.000

20.000

25.000

30.000

35.000

groovy_x

number of loops in 10 x̂

time

Page 15: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

● Loop unrolling might be possible● Removing the code is not● This makes code blocks larger than needed● Does allow less optimizations

Page 16: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

java_V

private volatile int t = 0; public void loop(int n) { int x = 0; while (x<n) { if (t==0) x++ } }

6 7 8 90.000

0.500

1.000

1.500

2.000

2.500

java_xgroovy_ojava_V

number of loops (log)tim

e

Page 17: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Loops

The usage of volataileprevents the code being

optimized away

No solution to this!?

Page 18: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

java_int

public int fib(int n) { if(n<2) return n; return fib(n-1) + fib(n-2); }

10 20 30 400.000

5.000

10.000

15.000

20.000

25.000

groovy_xjava_int

ntim

e

Page 19: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

groovy_x

def fib(n) { if(n<2) return n return fib(n-1) + fib(n-2) }

10 20 30 400.000

5.000

10.000

15.000

20.000

25.000

groovy_xjava_int

ntim

e

Page 20: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

Is Groovy slow?

Are the programs equal?

No.

10 20 30 4010

15

20

25

30

35

groovy slow down

nfa

ctor

Page 21: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

To perform n<2 in Groovy we actually do:

n.compareTo(2)<0

• n is Integer• compareTo will be called directly• Still the bytecode version with primitives is faster

Page 22: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

To perform x+y in Groovy we actually call:

DGM#plus(int x,int y) {return x+y;

}

• x and y exist as Integer on the stack• to do x+y, we have to unbox x and y• the result needs to be boxed again• dynamic method call to this method• x and y are stored in Object[]

Page 23: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

Even if the Java program is changed to use Integer, the

performance is about the same.

Boxing does cost, but not as much to explain the low speed

Page 24: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

Are method calls responsible?

Page 25: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

Java using BigInteger

compared with Groovy using

BigInteger

In the end only 57% slower

10 20 30 400

5

10

15

20

25

30

35

slow down

n

fact

or

Page 26: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Fibonacci

● Hotspot needs much longer for Groovy

● Groovy has an addional startup penalty

● Method calls are about 50% slower

Page 27: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

FPC

● "private" allows a direct method call

● optional typing allows usage of primitive values

● meta programing still possible

fast path compiler:

10 20 30 400%

50%

100%

150%

200%

250%

groovy slow down

ntim

e

Page 28: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

FPC

groovy_o

def fib(n) { return fib_p(n)}private int fib_p(int x) { if (n<2) return n return fib_p(n-1) + fib_p(n-2)}

by private enableddirect method call

optional types

Page 29: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

FPC

● more clean stack trace● less bytecode generation at runtime● less class loading problems● lower initial costs compared to generating

Page 30: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Instrumentation based Hotspot

● Agent cannot attach itself ot its own VM● Continously rewriting methods seems to cause

problems

This causes Problems if groovy is used:● In a restricted environment● As library

GSoc 2008 Chanwit Kaewkasihttp://code.google.com/p/gjit/

Page 31: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Runtime generated Callables

Replacing the method content with a callable is not enough

● Stack trace will be even more problematic to read (line number and file can be retained, class name not)

● Requires runtime byte code generation with its class loading and permgen problems (annok?)

● Tricking with sun.reflect package

Page 32: Performance comparisons of Java and Groovy - …wiki.jvmlangsummit.com/images/8/8c/Theodorou_Groovy.pdf · Performance comparisons of Java and Groovy ... Groovy Intro Dynamic language

Conclusion

● Microbenchmarks are EVIL!● What do we need that speed for?● If you are trying to be as fast as Java, you have

to fight smallest problems● Possible good solutions for us, are not always

good for hotspot engineer minds