jdk 8: lambda performance study - oracle...jdk 8: lambda performance study sergey kuksenko...

74

Upload: others

Post on 25-May-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

JDK 8: Lambda Performance study

Sergey Kuksenko

[email protected], @kuksenk0

Page 2: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

The following is intended to outline our generalproduct direction. It is intended for informationpurposes only, and may not be incorporated into anycontract. It is not a commitment to deliver anymaterial, code, or functionality, and should not berelied upon in making purchasing decisions. Thedevelopment, release, and timing of any features orfunctionality described for Oracle’s products remainsat the sole discretion of Oracle.

Slide 2/55.

Page 3: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda

Slide 3/55.

Page 4: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda: performance

Lambda

linkage

capture

invocation

vs

Anonymous Class

class loading

instantiation

invocation

Slide 4/55.

Page 5: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda: performance

Lambda

linkage

capture

invocation

vs

Anonymous Class

class loading

instantiation

invocation

Slide 4/55.

Page 6: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda: performance

Lambda

linkage

capture

invocation

vs

Anonymous Class

class loading

instantiation

invocation

Slide 4/55.

Page 7: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda: performance

Lambda

linkage

capture

invocation

vs

Anonymous Class

class loading

instantiation

invocation

Slide 4/55.

Page 8: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda: SUT1

Intel R○ CoreTM i5-520M (Westmere) [2.0 GHz]1x2x2

Xubuntu 11.10 (64-bits)

HDD Hitachi 320Gb, 5400 rpm

1System Under TestSlide 5/55.

Page 9: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage

Slide 6/55.

Page 10: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: How?

@GenerateMicroBenchmark

@BenchmarkMode(Mode.SingleShotTime)

@OutputTimeUnit(TimeUnit.SECONDS)

@Fork(value = 5, warmups = 1)

public static Level link() {

...

};

Slide 7/55.

Page 11: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: How?

@GenerateMicroBenchmark

@BenchmarkMode(Mode.SingleShotTime)

@OutputTimeUnit(TimeUnit.SECONDS)

@Fork(value = 5, warmups = 1)

public static Level link() {

...

};

Slide 7/55.

Page 12: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: How?

@GenerateMicroBenchmark

@BenchmarkMode(Mode.SingleShotTime)

@OutputTimeUnit(TimeUnit.SECONDS)

@Fork(value = 5, warmups = 1)

public static Level link() {

...

};

Slide 7/55.

Page 13: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: How?

@GenerateMicroBenchmark

@BenchmarkMode(Mode.SingleShotTime)

@OutputTimeUnit(TimeUnit.SECONDS)

@Fork(value = 5, warmups = 1)

public static Level link() {

...

};

Slide 7/55.

Page 14: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: What?

Required:

lots of lambdas

e.g. ()->()->()->()->()->...->()->null

@FunctionalInterface

public interface Level {

Level up();

}

Slide 8/55.

Page 15: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: What?

Required:

lots of different lambdas

e.g. ()->()->()->()->()->...->()->null

@FunctionalInterface

public interface Level {

Level up();

}

Slide 8/55.

Page 16: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: What?

Required:

lots of different lambdas

e.g. ()->()->()->()->()->...->()->null

@FunctionalInterface

public interface Level {

Level up();

}

Slide 8/55.

Page 17: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: What?

Required:

lots of different lambdas

e.g. ()->()->()->()->()->...->()->null

@FunctionalInterface

public interface Level {

Level up();

}

Slide 8/55.

Page 18: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: lambda chain

...

public static Level get1023(String p) {

return () -> get1022(p);

}

public static Level get1024(String p) {

return () -> get1023(p);

}

...

Slide 9/55.

Page 19: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: anonymous chain

...

public static Level get1024(final String p){

return new Level () {

@Override

public Level up() {

return get1023(p);

}

};

}

...

Slide 10/55.

Page 20: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: benchmark

@GenerateMicroBenchmark

...

public static Level link() {

Level prev = null;

for(Level curr = Chain0.get1024("str");

curr != null;

curr = curr.up() ) {

prev = curr;

}

return prev;

}

Slide 11/55.

Page 21: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: results (hot)

-TieredCompilation +TieredCompilationanonymous lambda anonymous lambda

1K 0.47 0.80 0.35 0.624K 1.58 2.16 1.12 1.5816K 4.96 5.62 4.22 4.6764K 16.51 17.53 15.68 16.21

time, seconds

Slide 12/55.

Page 22: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: results (cold)

-TieredCompilation +TieredCompilationanonymous lambda anonymous lambda

1K 7.24 0.95 6.98 0.774K 16.64 2.46 16.16 1.8416K 22.44 5.92 21.25 4.9064K 34.52 18.20 33.34 16.33

time, seconds

Slide 13/55.

Page 23: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: results (cold)

-TieredCompilation +TieredCompilationanonymous lambda anonymous lambda

1K 1440% 19% 1894% 24%4K 953% 14% 1343% 16%16K 352% 5% 404% 5%64K 109% 4% 113% 1%

performance hit

Slide 14/55.

Page 24: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: results (hot)

Slide 15/55.

Page 25: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: results (cold)

Slide 16/55.

Page 26: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Linkage: Main contributors (lambda)

25% - resolve_indy13% - link_MH_constant

44% - LambdaMetaFactory20% - Unsafe.defineClass

Slide 17/55.

Page 27: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture

Slide 18/55.

Page 28: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Non-capture lambda: benchmarks

public static Supplier <String > lambda (){

return () -> "42";

}

public static Supplier <String > anonymous (){

return new Supplier <String >() {

@Override

public String get() {

return "42";

}

};

}

public static Supplier <String > baseline (){

return null;

}

Slide 19/55.

Page 29: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Non-capture lambda: benchmarks

public static Supplier <String > lambda (){

return () -> "42";

}

public static Supplier <String > anonymous (){

return new Supplier <String >() {

@Override

public String get() {

return "42";

}

};

}

public static Supplier <String > baseline (){

return null;

}

Slide 19/55.

Page 30: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Non-capture lambda: benchmarks

public static Supplier <String > lambda (){

return () -> "42";

}

public static Supplier <String > anonymous (){

return new Supplier <String >() {

@Override

public String get() {

return "42";

}

};

}

public static Supplier <String > baseline (){

return null;

}

Slide 19/55.

Page 31: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Non-capture lambda: results

single thread

max threads (4)

baseline 5.29± 0.02

5.92± 0.02

anonymous 6.02± 0.02

12.40± 0.09

cached anonymous 5.36± 0.01

5.97± 0.03

lambda 5.31± 0.02

5.93± 0.07

average time, nsecs/op

Slide 20/55.

Page 32: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Non-capture lambda: results

single thread max threads (4)baseline 5.29± 0.02 5.92± 0.02anonymous 6.02± 0.02 12.40± 0.09cached anonymous 5.36± 0.01 5.97± 0.03lambda 5.31± 0.02 5.93± 0.07

average time, nsecs/op

Slide 20/55.

Page 33: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: lambda

public Supplier <String > lambda () {

String localString = someString;

return () -> localString;

}

Instance size = 16 bytes 2

264-bits VM, -XX:+CompressedOopsSlide 21/55.

Page 34: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: anonymous (static context)

public static Supplier <String > anonymous () {

String localString = someString;

return new Supplier <String >() {

@Override

public String get() {

return localString;

}

};

}

Instance size = 16 bytes 3

364-bits VM, -XX:+CompressedOopsSlide 22/55.

Page 35: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: anonymous (non-static context)

public Supplier <String > anonymous () {

String localString = someString;

return new Supplier <String >() {

@Override

public String get() {

return localString;

}

};

}

Instance size = 24 bytes 3

364-bits VM, -XX:+CompressedOopsSlide 22/55.

Page 36: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: results

single thread max threadsanonymous(static) 6.94± 0.03 13.4± 0.33anonymous(non-static) 7.88± 0.09 18.7± 0.17lambda 8.29± 0.04 16.0± 0.28

average time, nsec/op

Slide 23/55.

Page 37: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: results

single thread max threadsanonymous(static) 6.94± 0.03 13.4± 0.33anonymous(non-static) 7.88± 0.09 18.7± 0.17lambda 8.29± 0.04 16.0± 0.28

average time, nsec/op

Slide 23/55.

Page 38: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: exploring asm

...mov 0x68(%r10),%ebp

; *getstatic someStringmov $0xefe53110,%r10d

; metadata(’Capture1$$Lambda$1’)movzbl 0x186(%r12,%r10,8),%r8dadd $0xfffffffffffffffc,%r8dtest %r8d,%r8djne allocation_slow_pathmov 0x60(%r15),%raxmov %rax ,%r11add $0x10 ,%r11cmp 0x70(%r15),%r11jae allocation_slow_pathmov %r11 ,0x60(%r15)prefetchnta 0xc0(%r11)mov 0xa8(%r12 ,%r10 ,8),%r10mov %r10 ,(% rax)movl $0xefe53110 ,0x8(%rax)

; {metadata(’Capture1$$Lambda$1 ’)}mov %ebp ,0xc(%rax)

;* invokevirtual allocateInstance...

← checkif class was initialized(Unsafe.allocateInstance

from jsr292 LF’s)

Slide 24/55.

Page 39: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: exploring asm

...mov 0x68(%r10),%ebp

; *getstatic someStringmov $0xefe53110,%r10d

; metadata(’Capture1$$Lambda$1’)movzbl 0x186(%r12,%r10,8),%r8dadd $0xfffffffffffffffc,%r8dtest %r8d,%r8djne allocation_slow_pathmov 0x60(%r15),%raxmov %rax ,%r11add $0x10 ,%r11cmp 0x70(%r15),%r11jae allocation_slow_pathmov %r11 ,0x60(%r15)prefetchnta 0xc0(%r11)mov 0xa8(%r12 ,%r10 ,8),%r10mov %r10 ,(% rax)movl $0xefe53110 ,0x8(%rax)

; {metadata(’Capture1$$Lambda$1 ’)}mov %ebp ,0xc(%rax)

;* invokevirtual allocateInstance...

← checkif class was initialized(Unsafe.allocateInstance

from jsr292 LF’s)

Slide 24/55.

Page 40: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: benchmark

Can we find a benchmark or/and JVM environmentwhere allocation size difference is significant?

Slide 25/55.

Page 41: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: benchmark

@GenerateMicroBenchmark

@BenchmarkMode(Mode.AverageTime)

@OutputTimeUnit(TimeUnit.NANOSECONDS)

@OperationsPerInvocation(SIZE)4

public Supplier <Supplier > chain_lambda () {

Supplier <Supplier > top = null;

for (int i = 0; i < SIZE; i++) {

Supplier <Supplier > current = top;

top = () -> current;

}

return top;

}

4SIZE==1048576Slide 26/55.

Page 42: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: chain results

out of the box

1 thread

4 threads

anonymous 8.4± 1.1

47± 16

lambda 6.7± 0.6

28± 10

-Xmx1g

anonymous 11± 1.2

84± 9

lambda 7.6± 0.4

47± 20

-Xmx1g -Xmn800m

anonymous 8.1± 0.9

123± 18

lambda 6.0± 0.7

i28± 14

average time, nsecs/opSlide 27/55.

Page 43: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: beware of microbenchmarks

out of the box

1 thread 4 threadsanonymous 8.4± 1.1 47± 16lambda 6.7± 0.6 28± 10

-Xmx1g

anonymous 11± 1.2 84± 9lambda 7.6± 0.4 47± 20

-Xmx1g -Xmn800m

anonymous 8.1± 0.9 123± 18lambda 6.0± 0.7

i

28± 14average time, nsecs/op

Slide 27/55.

Page 44: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture warmup(time-to-performance)

Slide 28/55.

Page 45: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

lots of different lambdas (e.g. linkagebenchmark)

throughput (-bm Throughput)

no warmup (-wi 0)

get throughput each second (-r 1)

large amount of iterations (-i 200)

Slide 29/55.

Page 46: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

4K chain; -XX:-TieredCompilation

Slide 30/55.

Page 47: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: lambda slow warmup

Main culprits:

jsr292 LF implementationlayer of LF’s generated methods

HotSpot (interpreter)calling a method is hard (even simple delegatingmethods)

Slide 31/55.

Page 48: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: LF’s inline tree

@ 1 oracle.micro.benchmarks.jsr335.lambda.chain.lamb.cap1.common.Chain3:get3161@ 1 java.lang.invoke.LambdaForm$MH /1362679684:: linkToCallSite

@ 1 java.lang.invoke.Invokers :: getCallSiteTarget@ 4 java.lang.invoke.ConstantCallSite :: getTarget

@ 10 java.lang.invoke.LambdaForm$MH /90234171:: convert@ 9 java.lang.invoke.LambdaForm$DMH /1041177261:: newInvokeSpecial_L_L

@ 1 java.lang.invoke.DirectMethodHandle :: allocateInstance@ 12 sun.misc.Unsafe :: allocateInstance (0 bytes) (intrinsic)

@ 6 java.lang.invoke.DirectMethodHandle :: constructorMethod@ 16 ... $$Lambda$936 ::<init >

@ 1 java.lang.invoke.MagicLambdaImpl ::<init > (5 bytes)@ 1 java.lang.Object::<init > (1 bytes)

Slide 32/55.

Page 49: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: lambda slow warmup

Main culprits:

jsr292 LF implementationlayer of LF’s generated methods

HotSpot (interpreter)calling a method is hard (even simple delegatingmethods)

Slide 33/55.

Page 50: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

extra invocations for anonymous

Slide 34/55.

Page 51: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: lambda slow warmup

Areas for improvement:

Lambda runtime representation?

jsr292 LF implementation?

Tiered Compilation?

HotSpot (interpreter)?

Slide 35/55.

Page 52: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

4K chain; -XX:-TieredCompilation

Slide 36/55.

Page 53: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

4K chain; -XX:+TieredCompilation

Slide 37/55.

Page 54: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Capture: time-to-performance

4K chain; -XX:+TieredCompilation

Slide 38/55.

Page 55: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Invocation

Slide 39/55.

Page 56: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Invocation: performance

Lambda invocation behaves

exactly as anonymous class invocation

Slide 40/55.

Page 57: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Invocation: performance

Lambda5 invocation behaves

exactly as anonymous class invocation

5current implementationSlide 40/55.

Page 58: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lambda and optimizations

Slide 41/55.

Page 59: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Inline: benchmark

public String id_lambda (){

String str = "string";

Function <String , String > id = s -> s;

return id.apply(str);

}

public String id_ideal (){

String str = "string";

return str;

}

Slide 42/55.

Page 60: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Inline: benchmark

public String id_lambda (){

String str = "string";

Function <String , String > id = s -> s;

return id.apply(str);

}

public String id_ideal (){

String str = "string";

return str;

}

Slide 42/55.

Page 61: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Inline: results

ideal 5.38± 0.03anonymous 5.40± 0.02cached anonymous 5.37± 0.03lambda 5.38± 0.02

average time, nsecs/op

Slide 43/55.

Page 62: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Inline: asm

ideal, anonymous, cached anonymous:...mov $0x7d75cd018 ,%rax ; {oop("string")}...

lambda:...mov $0x7d776c8b0 ,%r10 ; {oop(a ’TestOpt0$$Lambda$1 ’)}mov 0x8(%r10),%r11dcmp $0xefe56908 ,%r11d ; {metadata(’TestOpt0$$Lambda$1 ’)}jne <invokeinterface_slowpath >mov $0x7d75cd018 ,%rax ; {oop("string")}...

Slide 44/55.

Page 63: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Scalar replacement: benchmark

public String sup_lambda (){

String str = "string";

Supplier <String > sup = () -> str;

return sup.get();

}

public String sup_ideal (){

String str = "string";

return str;

}

Slide 45/55.

Page 64: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Scalar replacement: benchmark

public String sup_lambda (){

String str = "string";

Supplier <String > sup = () -> str;

return sup.get();

}

public String sup_ideal (){

String str = "string";

return str;

}

Slide 45/55.

Page 65: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Scalar replacement: results

ideal 5.49± 0.03anonymous 5.52± 0.02lambda 5.53± 0.02average time, nsecs/op

Slide 46/55.

Page 66: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Scalar replacement: asm

ideal, anonymous, lambda:...mov $0x7d75cd018 ,%rax ; {oop("string")}...

Slide 47/55.

Page 67: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Streams

Slide 48/55.

Page 68: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: benchmark

List <Integer > list = new ArrayList <>();

@GenerateMicroBenchmark

public int forEach_4filters () {

Counter c = new Counter ();

list.stream ()

.filter(i -> (i & 0xf) == 0)

.filter(i -> (i & 0xff) == 0)

.filter(i -> (i & 0xfff) == 0)

.filter(i -> (i & 0xffff) == 0)

.forEach(c::add);

return c.sum;

}

Slide 49/55.

Page 69: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: benchmark

List <Integer > list = new ArrayList <>();

@GenerateMicroBenchmark

public int forEach_3filters () {

Counter c = new Counter ();

list.stream ()

.filter(i -> (i & 0xf) == 0)

.filter(i -> (i & 0xff) == 0)

.filter(i -> (i & 0xfff) == 0)

.filter(i -> (i & 0xffff) == 0)

.forEach(c::add);

return c.sum;

}

Slide 50/55.

Page 70: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: benchmark

List <Integer > list = new ArrayList <>();

@GenerateMicroBenchmark

public int forEach_2filters () {

Counter c = new Counter ();

list.stream ()

.filter(i -> (i & 0xf) == 0)

.filter(i -> (i & 0xff) == 0)

.filter(i -> (i & 0xfff) == 0)

.filter(i -> (i & 0xffff) == 0)

.forEach(c::add);

return c.sum;

}

Slide 51/55.

Page 71: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: benchmark

@GenerateMicroBenchmark

public int iterator_4filters () {

Counter c = new Counter ();

Iterator <Integer > iterator = list

.stream ()

.filter(i -> (i & 0xf) == 0)

.filter(i -> (i & 0xff) == 0)

.filter(i -> (i & 0xfff) == 0)

.filter(i -> (i & 0xffff) == 0)

.iterator ();

while (iterator.hasNext ()) {

c.add(iterator.next ());

}

return c.sum;

}

Slide 52/55.

Page 72: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: benchmark

@GenerateMicroBenchmark

public int for_4filters () {

Counter c = new Counter ();

for(Integer i : list) {

if((i & 0xf) == 0 &&

(i & 0xff) == 0 &&

(i & 0xfff) == 0 &&

(i & 0xffff) == 0) {

c.add(i);

}

}

return c.sum;

}

Slide 53/55.

Page 73: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Lazy vs Eager: results

2 filters 3 filters 4 filtersforEach 3.0 1.8 1.7iterator 1.1 0.7 0.6for 2.4 2.4 2.3

throughput, ops/sec

Slide 54/55.

Page 74: JDK 8: Lambda Performance study - Oracle...JDK 8: Lambda Performance study Sergey Kuksenko sergey.kuksenko@oracle.com, @kuksenk0 The following is intended to outline our general product

Q&A?

Slide 55/55.