java performance boost

36
©2015 GlobalLogic Inc.

Upload: globallogic-ukraine

Post on 08-Apr-2017

1.540 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Java Performance Boost

©2015 GlobalLogic Inc.

Page 2: Java Performance Boost

2

Java Performance Boost

By Andrii Antilikatorov

Page 3: Java Performance Boost

3

Page 4: Java Performance Boost

4

Optimize or not optimize?

Page 5: Java Performance Boost

5

Rule

Page 6: Java Performance Boost

6

Page 7: Java Performance Boost

7

Page 8: Java Performance Boost

8

Avoid usage byte[] to String constructor (Java 6)

Page 9: Java Performance Boost

9

Avoid usage byte[] to String constructor (Java 6)

• Update to Java 7 or later• Minimize usage of constructor

Page 10: Java Performance Boost

10

Understanding of “switch on String”

Page 11: Java Performance Boost

11

String Internation

String

String

StringString

String

String

String

String

String

String

StringString

Do not use String.intern() on Java 6

Internation is efficient on Java 7 and above

Works great in multithreaded code

JVM string pool is NOT thread localString

Page 12: Java Performance Boost

12

String Deduplication

Available from Java 8 update 20

Requires G1 collector to be enabled. Optional phase of G1.

Unlikely to run on the loaded systems

Processes only strings that survived after few garbage collections

Doesn’t mean you shouldn’t use internation

String

String

StringString

String

String

String

String

String

String

StringString

String

Page 13: Java Performance Boost

13

String Deduplication

Available from Java 8 update 20

Requires G1 collector to be enabled. Optional phase of G1.

Unlikely to run on the loaded systems

Processes only strings that survived after few garbage collections

Doesn’t mean you shouldn’t use internation

String

String

StringString

String

String

String

String

String

String

StringString

String

String before Java 7 update 6

• char [] symbols;• Offset• Count• Hash code

Page 14: Java Performance Boost

14

String Deduplication

Available from Java 8 update 20

Requires G1 collector to be enabled. Optional phase of G1.

Unlikely to run on the loaded systems

Processes only strings that survived after few garbage collections

Doesn’t mean you shouldn’t use internation

String

String

StringString

String

String

String

String

String

String

StringString

String

String before Java 7 update 6

• char [] symbols;• Offset• Count• Hash code

This is very very long string which consumes a lot of memory…

This is very very long string which consumes a lot of memory…

Page 15: Java Performance Boost

15

String Deduplication

Available from Java 8 update 20

Requires G1 collector to be enabled. Optional phase of G1.

Unlikely to run on the loaded systems

Processes only strings that survived after few garbage collections

Doesn’t mean you shouldn’t use internation

String

String

StringString

String

String

String

String

String

String

StringString

String

String after Java 7 update 6

• char [] symbols;• Hash code

Page 16: Java Performance Boost

16

String Deduplication

Available from Java 8 update 20

Requires G1 collector to be enabled. Optional phase of G1.

Unlikely to run on the loaded systems

Processes only strings that survived after few garbage collections

Doesn’t mean you shouldn’t use internation

String

String

StringString

String

String

String

String

String

String

StringString

String• Serial• Parallel • Concurrent Mark Sweep (CMS) • Garbage-First (G1)

Page 17: Java Performance Boost

17

Avoid heavy logic in static constructors

Page 18: Java Performance Boost

18

Avoid heavy logic in static constructors

Page 19: Java Performance Boost

19

Avoid heavy logic in static constructors

Page 20: Java Performance Boost

20

Problem with inner classes

Page 21: Java Performance Boost

21

Problem with inner classes

Page 22: Java Performance Boost

22

Cost of method calls

invokestatic

invokevirtual

invokeinterface

invokespecial

Page 23: Java Performance Boost

23

Cost of method calls

invokestatic

invokevirtual

invokeinterface

invokespecial

Dependency Injection

Page 24: Java Performance Boost

24

Maps & Sets - Hints

Set:

Map:

Page 25: Java Performance Boost

25

Maps & Sets - Hints

• emptyList/emptyMap/emptySet - for empty collections

• singletonList/singletonMap/singletonSet - for collections containing a single element/pair

Page 26: Java Performance Boost

26

Performance of Date/Time entities

java.util.Date java.util.GregorianCalendar

Page 27: Java Performance Boost

27

Performance of Date/Time entities

long

Int[]

InternationalizationArithmetic

operation

support

24 bytes 448 bytes

java.util.Date java.util.GregorianCalendar

Page 28: Java Performance Boost

28

Performance of Date/Time entities

long

Int[]

InternationalizationArithmetic

operation

support

24 bytes 448 bytes

java.util.Date java.util.GregorianCalendar

Page 29: Java Performance Boost

29

Synchronized block or synchronized method?

Page 30: Java Performance Boost

30

Synchronized block or synchronized method?

0: aload_0 1: getfield 2: nop 3: iconst_m1 4: ireturn

Page 31: Java Performance Boost

31

Synchronized block or synchronized method?

0: aload_0 1: dup 2: astore_1 3: monitorenter 4: aload_0 5: getfield 6: nop 7: iconst_m1 8: aload_1 9: monitorexit 10: ireturn 11: astore_2 12: aload_1 13: monitorexit 14: aload_2 15: athrow

Page 32: Java Performance Boost

32

Thread-safe variants of List and Set

Manual synchronization of operations with collections

Use CopyOnWrite* collections 

Page 33: Java Performance Boost

33

Synchronized or volatile variables?

Volatile variables are less expensive than synchronized

Volatile are thread-safe for atomic operations

Volatile acquires lock on variable, therefore not thread- safe for non-atomic operations

Synchronized blocks have no performance impact if no synchronization required

Page 34: Java Performance Boost

34

Synchronized or volatile variables?

MESI Protocol• Modified• Exclusive• Shared• Invalid

Page 35: Java Performance Boost

35

Summary

Architecture first

Remember rule 80/20

Don’t try to write ideal code from the beginning

Don’t create bicycles

Don’t optimize for particular platform

Page 36: Java Performance Boost

©2015 GlobalLogic Inc.