java performance boost
TRANSCRIPT
©2015 GlobalLogic Inc.
2
Java Performance Boost
By Andrii Antilikatorov
3
4
Optimize or not optimize?
5
Rule
6
7
8
Avoid usage byte[] to String constructor (Java 6)
9
Avoid usage byte[] to String constructor (Java 6)
• Update to Java 7 or later• Minimize usage of constructor
10
Understanding of “switch on String”
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
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
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
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…
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
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)
17
Avoid heavy logic in static constructors
18
Avoid heavy logic in static constructors
19
Avoid heavy logic in static constructors
20
Problem with inner classes
21
Problem with inner classes
22
Cost of method calls
invokestatic
invokevirtual
invokeinterface
invokespecial
23
Cost of method calls
invokestatic
invokevirtual
invokeinterface
invokespecial
Dependency Injection
24
Maps & Sets - Hints
Set:
Map:
25
Maps & Sets - Hints
• emptyList/emptyMap/emptySet - for empty collections
• singletonList/singletonMap/singletonSet - for collections containing a single element/pair
26
Performance of Date/Time entities
java.util.Date java.util.GregorianCalendar
27
Performance of Date/Time entities
long
Int[]
InternationalizationArithmetic
operation
support
24 bytes 448 bytes
java.util.Date java.util.GregorianCalendar
28
Performance of Date/Time entities
long
Int[]
InternationalizationArithmetic
operation
support
24 bytes 448 bytes
java.util.Date java.util.GregorianCalendar
29
Synchronized block or synchronized method?
30
Synchronized block or synchronized method?
0: aload_0 1: getfield 2: nop 3: iconst_m1 4: ireturn
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
32
Thread-safe variants of List and Set
Manual synchronization of operations with collections
Use CopyOnWrite* collections
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
34
Synchronized or volatile variables?
MESI Protocol• Modified• Exclusive• Shared• Invalid
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
©2015 GlobalLogic Inc.