1
THE	
PERFORMANCE	
ENGINEER’S	
GUIDE	TO	
HOTSPOT	
JIT	COMPILATION
Monica	Beckwith	
CODE	KARAM	LLC
2
About Me …
Java/JVM/GC performance person
Current Life:
Consultant who solves Managed Runtime performance issues
Past Life:
Worked with Oracle, Sun, AMD…
- Worked with HotSpot VM: JVM heuristics, JIT compiler, GCs:
Parallel(Old) GC, G1 GC, CMS GC
www.codekaram.com
https://www.linkedin.com/in/monicabeckwith
Tweet @mon_beck
©2016 CodeKaram
Establishing the Characters
and The Plot
• HotSpot Virtual Machine
• Runtime Goal
• Common Compilation Techniques
• C1, C2 & Tiered Compilations
• Adaptive Optimization - The Plot
3
©2016 CodeKaram
The Storyline
• Dynamic Deoptimization
• Inlining
• Intrinsics
4
©2016 CodeKaram
The StoryLine
• Compressed Oops + Compressed Class
Pointers
• Escape Analysis
• Vectorization (Auto and otherwise)
5
6
HotSpot Virtual Machine
©2016 CodeKaram
Let’s Peek Under The Hood
7
Java Application
Java
APIs Execution Engine
Java VM
OS + Hardware
Runtime
©2016 CodeKaram8
Java Application
Java
API
Java VM
OS + HardwareJRE
Execution Engine
Runtime
Java Runtime Environment
©2016 CodeKaram
The Helpers
9
Java VM
Execution Engine
Runtime
©2016 CodeKaram
HotSpot VM
10
Execution Engine
Heap
Management
- Garbage
Collection
JIT Compilation -
Tiered (C1 + C2)
©2016 CodeKaram
HotSpot VM
11
Runtime
VM Class
loading
Interpreter
Bytecode
Verification,
Exception
Handling,
Synchronization
, Thread
Management, ….http://openjdk.java.net/groups/hotspot/
docs/RuntimeOverview.html
©2016 CodeKaram
Runtime Goal
12
Bytecode
Native code
©2016 CodeKaram
HotSpot VM - Template
Interpreter
13
Bytecode 1
Bytecode 2
Template Native Code 1
Template Native Code 2
TemplateTable
©2016 CodeKaram
Common Compilation
Techniques
14
Compilation
Adaptive
Optimization
Ahead
Of
Time
Profile-
guided
Static
©2016 CodeKaram
HotSpot VM
15
Compilation
Adaptive
Optimization
Ahead
Of
Time
Profile-
guided
Static
©2016 CodeKaram
• CompileThreshold
• Identify root of compilation
• Method Compilation or On-stack
replacement (Loop)?
16
Identifying Performance
Critical Methods
17
JIT Optimization - Client
Compiler (C1)
©2016 CodeKaram
• Start in interpreter
• CompileThreshold = 1500
• Compile with client compiler
• Few optimizations
18
Client Compiler
19
JIT Optimization - Server
Compiler (C2)
©2016 CodeKaram
• Start in interpreter
• CompileThreshold = 10000
• Compile with optimized server compiler
• High performance optimizations
20
Server Compiler
21
JIT Optimization - Tiered
Compilation
22
©2016 CodeKaram
• Start in interpreter
• Tiered optimization with client compiler
• Code profiled information
• Enable server compiler
23
Tiered Compilation
©2016 CodeKaram24
Tiered Compilation - Effect
on Code Cache
Tiered
Compilation
Code
Cache
Compiled
Native Code
©2016 CodeKaram
• C1 compilation threshold for tiered is about a 100
invocations
• Tiered Compilation has a lot more profiled information
for C1 compiled methods
• CodeCache needs to be 5x larger than non-tiered
• Default on JDK8 when tiered is enabled (240MB vs
48MB)
• Need more? Use -XX:ReservedCodeCacheSize
25
Tiered Compilation - Effect
on Code Cache
26
Advanced Optimizations -
Adaptive Optimization
©2016 CodeKaram27
The Plot
Startup
(Adaptive) JIT
Optimizations
Profiling of
critical hot-
spots
Interpreter
©2016 CodeKaram28
The Plot
Startup
(Adaptive) JIT
Optimizations
Profiling of
critical hot-
spots
Interpreter
29
Adaptive Optimization:
Dynamic Deoptimization
30
Dynamic Deoptimization
©2016 CodeKaram
• Oopsie!
• dependencies invalidation
• classes unloading and redefinition
• uncommon path in compiled code
• misguided profiled information
31
Dynamic Deoptimization
32
Understanding Adaptive
Optimization -
PrintCompilation
©2016 CodeKaram
PrintCompilation
33
timestamp compilation-id flags tiered-
compilation-level Method <@ osr_bci> code-
size <deoptimization>
©2016 CodeKaram
PrintCompilation
34
Flags:
%: is_osr_method
s: is_synchronized
!: has_exception_handler
b: is_blocking
n: is_native
©2016 CodeKaram
PrintCompilation
35
567 693 % ! 3
org.h2.command.dml.Insert::insertRows @ 76 (513 bytes)
656 797 n 0
java.lang.Object::clone (native)
779 835 s 4
java.lang.StringBuffer::append (13 bytes)
©2016 CodeKaram36
573 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
7963 2223 4
org.h2.table.Table::fireAfterRow (17 bytes)
7964 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made not entrant
33547 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made zombie
Dynamic Deoptimization
©2016 CodeKaram37
573 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
7963 2223 4
org.h2.table.Table::fireAfterRow (17 bytes)
7964 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made not entrant
33547 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made zombie
Dynamic Deoptimization
©2016 CodeKaram38
573 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
7963 2223 4
org.h2.table.Table::fireAfterRow (17 bytes)
7964 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made not entrant
33547 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made zombie
Dynamic Deoptimization
©2016 CodeKaram39
573 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
7963 2223 4
org.h2.table.Table::fireAfterRow (17 bytes)
7964 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made not entrant
33547 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made zombie
Dynamic Deoptimization
©2016 CodeKaram40
573 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
7963 2223 4
org.h2.table.Table::fireAfterRow (17 bytes)
7964 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made not entrant
33547 704 2
org.h2.table.Table::fireAfterRow (17 bytes)
made zombie
Dynamic Deoptimization
41
JIT Optimization - Inlining
©2016 CodeKaram
HotSpot has studied Inlining to it’s max potential!
42
Inlining
©2016 CodeKaram
MinInliningThreshold, MaxFreqInlineSize,
InlineSmallCode, MaxInlineSize, MaxInlineLevel,
DesiredMethodLimit …
43
Inlining
44
Understanding Inlining
Optimization -
PrintInlining
©2016 CodeKaram
PrintInling*
45
@ 76 java.util.zip.Inflater::setInput (74 bytes)
too big
@ 80 java.io.BufferedInputStream::getBufIfOpen (21
bytes) inline (hot)
@ 91 java.lang.System::arraycopy (0 bytes)
(intrinsic)
@ 2 java.lang.ClassLoader::checkName (43 bytes)
callee is too large
* needs -XX:+UnlockDiagnosticVMOptions
©2016 CodeKaram
PrintInling*
46
@ 76 java.util.zip.Inflater::setInput (74 bytes)
too big
@ 80 java.io.BufferedInputStream::getBufIfOpen (21
bytes) inline (hot)
@ 91 java.lang.System::arraycopy (0 bytes)
(intrinsic)
@ 2 java.lang.ClassLoader::checkName (43 bytes)
callee is too large
* needs -XX:+UnlockDiagnosticVMOptions
47
JIT Optimization - Intrinsics
©2016 CodeKaram48
Without Intrinsics
Java Method
JIT Compilation
Execute Generated Code
©2016 CodeKaram49
Intrinsics
Java Method
JIT Compilation
Execute Optimized Code
Call Hand-
Optimized
Assembly Code
©2016 CodeKaram
PrintInling*
50
@ 76 java.util.zip.Inflater::setInput (74 bytes)
too big
@ 80 java.io.BufferedInputStream::getBufIfOpen (21
bytes) inline (hot)
@ 91 java.lang.System::arraycopy (0 bytes)
(intrinsic)
@ 2 java.lang.ClassLoader::checkName (43 bytes)
callee is too large
* needs -XX:+UnlockDiagnosticVMOptions
51
Advanced Optimizations -
Compressed Oops +
Compressed Class Pointers
52
A Java Object
©2016 CodeKaram53
Header Body
©2016 CodeKaram
Objects, Fields & Alignment
• Objects are 8 byte aligned (default).
• Fields:
• are aligned by their type.
• can fill a gap that maybe required for
alignment.
• are accessed using offset from the start of the
object
54
55
Java Object Layout in Memory
©2016 CodeKaram56
-Tool to analyze Java object layout :)
Java Object Layout (JOL)
©2016 CodeKaram
JOL Command Line Options
$ java -jar jol-cli.jar
Usage: jol-cli.jar <mode> [optional arguments]*
Available modes:
estimates: Simulate the class layout in different VM modes.
externals: Show the object externals: the objects reachable
from a given instance.
footprint: Estimate the footprint of all objects reachable
from a given instance
heapdump: Consume the heap dump and estimate the savings
in different layout strategies.
heapdumpstats: Consume the heap dump and print the most
frequent instances.
idealpack: Compute the object footprint under different
field layout strategies.
internals: Show the object internals: field layout and
default contents, object header
shapes: Dump the object shapes present in JAR files or
heap dumps.
string-compress: Consume the heap dumps and figures out the
savings attainable with compressed strings.
57
©2016 CodeKaram
JOL Command Line Options
$ java -jar jol-cli.jar
Usage: jol-cli.jar <mode> [optional arguments]*
Available modes:
estimates: Simulate the class layout in different VM modes.
externals: Show the object externals: the objects reachable from
a given instance.
footprint: Estimate the footprint of all objects reachable from
a given instance
heapdump: Consume the heap dump and estimate the savings in
different layout strategies.
heapdumpstats: Consume the heap dump and print the most frequent
instances.
idealpack: Compute the object footprint under different field
layout strategies.
internals: Show the object internals:
field layout and default contents, object header
shapes: Dump the object shapes present in JAR files or heap
dumps.
string-compress: Consume the heap dumps and figures out the savings
attainable with compressed strings.
58
©2016 CodeKaram
Compressed OOPs and
Compressed Class Pointers
59
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit
shift.
©2016 CodeKaram
Compressed OOPs and
Compressed Class Pointers
60
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit
shift.
61
A Java Object Header
©2016 CodeKaram
A Java Object Header
62
Header
Klass
Mark
Word
Array
Length
63
Java Object Layout Samples -
Uncompressed
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07
c0 34 (10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00
00 00 (00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4
bytes total
64
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07
c0 34 (10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00
00 00 (00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4
bytes total
65
Mark Word
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07
c0 34 (10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00
00 00 (00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4
bytes total
66
Klass
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07
c0 34 (10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00
00 00 (00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4
bytes total
67
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07
c0 34 (10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00
00 00 (00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4
bytes total
68
Array Length
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00
(00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00
(00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07 c0 34
(10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00 00 00
(00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00 00 00
(00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap)
N/A
24 8 byte [B.<elements> N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes
total
69
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00
(00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00
(00000000 00000000 00000000 00000000) (0)
8 4 (object header) a8 07 c0 34
(10101000 00000111 11000000 00110100) (885000104)
12 4 (object header) 02 00 00 00
(00000010 00000000 00000000 00000000) (2)
16 4 (object header) 08 00 00 00
(00001000 00000000 00000000 00000000) (8)
20 4 (alignment/padding gap) N/A
24 8 byte [B.<elements>
N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes
total
70
71
Java Object Layout Samples -
Compressed
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
72
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
73
Mark Word
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
74
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
75
Klass
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
76
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION
VALUE
0 4 (object header) 01
00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00
00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5
00 00 f8 (11110101 00000000 00000000 11111000)
(-134217483)
12 4 (object header) 08
00 00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements> N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
77
Array Length
©2016 CodeKaram
JOL: 8 Byte Array
[B object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00
00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00
00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) f5 00
00 f8 (11110101 00000000 00000000 11111000) (-134217483)
12 4 (object header) 08 00
00 00 (00001000 00000000 00000000 00000000) (8)
16 8 byte [B.<elements>
N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0
bytes total
78
79
Java Object Layout Header
Comparison
©2016 CodeKaram
Compressed vs
Uncompressed
Compressed:
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes
external = 0 bytes total
vs
Uncompressed:
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes
external = 4 bytes total
80
©2016 CodeKaram
Compressed vs
Uncompressed
Compressed:
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes
external = 0 bytes total
vs
Uncompressed:
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes
external = 4 bytes total
81
82
A Java Object
©2016 CodeKaram83
Header Body
©2016 CodeKaram
Compressed OOPs and
Compressed Class Pointers
84
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit
shift.
85
Java Object Layout Samples -
UnCompressed
©2016 CodeKaram
JOL: Class
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header) N/A
16 8 Constructor Class.cachedConstructor N/A
24 8 Class Class.newInstanceCallerCache N/A
32 8 String Class.name N/A
40 8 Module Class.module N/A
48 8 (alignment/padding gap) N/A
56 8 String Class.packageName N/A
64 8 Class Class.componentType N/A
72 8 SoftReference Class.reflectionData N/A
80 8 ClassRepository Class.genericInfo N/A
88 8 Object[] Class.enumConstants N/A
96 8 Map Class.enumConstantDirectory N/A
104 8 AnnotationData Class.annotationData N/A
112 8 AnnotationType Class.annotationType N/A
120 8 ClassValueMap Class.classValueMap N/A
128 32 (alignment/padding gap) N/A
160 4 int Class.classRedefinedCount N/A
164 4 (loss due to the next object alignment)
Instance size: 168 bytes
Space losses: 40 bytes internal + 4 bytes external = 44 bytes total
86
©2016 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header)
N/A
16 8 Constructor Class.cachedConstructor N/A
24 8 Class Class.newInstanceCallerCache N/A
32 8 String Class.name N/A
40 8 Module Class.module N/A
48 8 (alignment/padding gap) N/A
56 8 String Class.packageName N/A
64 8 Class Class.componentType N/A
72 8 SoftReference Class.reflectionData N/A
80 8 ClassRepository Class.genericInfo N/A
88 8 Object[] Class.enumConstants N/A
96 8 Map Class.enumConstantDirectory N/A
104 8 AnnotationData Class.annotationData N/A
112 8 AnnotationType Class.annotationType N/A
120 8 ClassValueMap Class.classValueMap N/A
128 32 (alignment/padding gap) N/A
160 4 int Class.classRedefinedCount N/A
164 4 (loss due to the next object alignment)
Instance size: 168 bytes
Space losses: 40 bytes internal + 4 bytes external = 44 bytes total
87
JOL: Class
©2016 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 16 (object header) N/A
16 8 Constructor Class.cachedConstructor N/A
24 8 Class Class.newInstanceCallerCache N/A
32 8 String Class.name N/A
40 8 Module Class.module N/A
48 8 (alignment/padding gap) N/A
56 8 String Class.packageName N/A
64 8 Class Class.componentType N/A
72 8 SoftReference Class.reflectionData N/A
80 8 ClassRepository Class.genericInfo N/A
88 8 Object[] Class.enumConstants N/A
96 8 Map Class.enumConstantDirectory N/A
104 8 AnnotationData Class.annotationData N/A
112 8 AnnotationType Class.annotationType N/A
120 8 ClassValueMap Class.classValueMap N/A
128 32 (alignment/padding gap) N/A
160 4 int Class.classRedefinedCount N/A
164 4 (loss due to the next object alignment)
Instance size: 168 bytes
Space losses: 40 bytes internal + 4 bytes external = 44 bytes total
88
JOL: Class
89
Java Object Layout Samples -
Compressed
©2016 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 Constructor Class.cachedConstructor N/A
16 4 Class Class.newInstanceCallerCache N/A
20 4 String Class.name N/A
24 4 Module Class.module N/A
28 4 (alignment/padding gap) N/A
32 4 String Class.packageName N/A
36 4 Class Class.componentType N/A
40 4 SoftReference Class.reflectionData N/A
44 4 ClassRepository Class.genericInfo N/A
48 4 Object[] Class.enumConstants N/A
52 4 Map Class.enumConstantDirectory N/A
56 4 AnnotationData Class.annotationData N/A
60 4 AnnotationType Class.annotationType N/A
64 4 ClassValueMap Class.classValueMap N/A
68 28 (alignment/padding gap) N/A
96 4 int Class.classRedefinedCount N/A
100 4 (loss due to the next object alignment)
90
JOL: Class
©2016 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header)
N/A
12 4 Constructor Class.cachedConstructor N/A
16 4 Class Class.newInstanceCallerCache N/A
20 4 String Class.name N/A
24 4 Module Class.module N/A
28 4 (alignment/padding gap) N/A
32 4 String Class.packageName N/A
36 4 Class Class.componentType N/A
40 4 SoftReference Class.reflectionData N/A
44 4 ClassRepository Class.genericInfo N/A
48 4 Object[] Class.enumConstants N/A
52 4 Map Class.enumConstantDirectory N/A
56 4 AnnotationData Class.annotationData N/A
60 4 AnnotationType Class.annotationType N/A
64 4 ClassValueMap Class.classValueMap N/A
68 28 (alignment/padding gap) N/A
96 4 int Class.classRedefinedCount N/A
100 4 (loss due to the next object alignment)
91
JOL: Class
©2016 CodeKaram
java.lang.Class object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 Constructor Class.cachedConstructor N/A
16 4 Class Class.newInstanceCallerCache N/A
20 4 String Class.name N/A
24 4 Module Class.module N/A
28 4 (alignment/padding gap) N/A
32 4 String Class.packageName N/A
36 4 Class Class.componentType N/A
40 4 SoftReference Class.reflectionData N/A
44 4 ClassRepository Class.genericInfo N/A
48 4 Object[] Class.enumConstants N/A
52 4 Map Class.enumConstantDirectory N/A
56 4 AnnotationData Class.annotationData N/A
60 4 AnnotationType Class.annotationType N/A
64 4 ClassValueMap Class.classValueMap N/A
68 28 (alignment/padding gap) N/A
96 4 int Class.classRedefinedCount N/A
100 4 (loss due to the next object alignment)
92
JOL: Class
©2016 CodeKaram
Compressed OOPs and
Compressed Class Pointers
93
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit
shift.
P
P
94
… but what is the 3-bit shift?
©2016 CodeKaram
Compressed OOPs
95
<wide-oop> = <narrow-oop-base> +
(<narrow-oop> << 3) + <field-offset>
©2016 CodeKaram
Compressed OOPs
96
Heap Size?
<4 GB
(no encoding/
decoding needed)
>4GB; <28GB
(zero-based)
<wide-oop> = <narrow-oop> <narrow-oop> << 3
©2016 CodeKaram
Compressed OOPs
97
Heap Size?
>28 GB; <32 GB
(regular)
>32 GB; <64 GB *
(change alignment)
<wide-oop> =
<narrow-oop-base>
+ (<narrow-oop>
<< 3) + <field-
offset>
<narrow-oop-base>
+ (<narrow-oop>
<< 4) + <field-
offset>
©2016 CodeKaram
Compressed OOPs
98
Heap Size? <4 GB
>4GB;
<28GB
<32GB <64GB
Object
Alignment?
8 bytes 8 bytes 8 bytes 16 bytes
Offset
Required?
No No Yes Yes
Shift by? No shift 3 3 4
99
Other Advanced Optimizations
©2016 CodeKaram
• Fast dynamic type tests for type safety
• Range check elimination
• Loop unrolling
• Escape Analysis
• Vectorization!
100
Other Optimizations
©2016 CodeKaram
• Fast dynamic type tests for type safety
• Range check elimination
• Loop unrolling
• Escape Analysis
• Vectorization!
101
Other Optimizations
102
Advanced Optimization -
Escape Analysis
©2016 CodeKaram
• Entire IR graph
• escaping allocations?
• not stored to a static field or non-static field of
an external object,
• not returned from method,
• not passed as parameter to another method
where it escapes.
103
Escape Analysis
©2016 CodeKaram104
Escape Analysis
allocated object doesn’t
escape the compiled method
allocated object not
passed as a parameter
+
remove allocation and keep field
values in registers
=
©2016 CodeKaram105
Escape Analysis
allocated object is
passed as a parameter
+
remove locks associated with object
and use optimized compare instructions
=
allocated object doesn’t
escape the compiled method
©2016 CodeKaram
• Fast dynamic type tests for type safety
• Range check elimination
• Loop unrolling
• Escape Analysis
• Vectorization!
106
Other JIT Compiler
Optimizations
107
Advanced Optimization -
Vectorization
108
Vectorization in HotSpot
VM??
©2016 CodeKaram
Vectorization
109
• Utilize SIMD (Single Instruction Multiple Data)
instructions offered by the processor.
• Generate assembly stubs
• Get benefits of operating on cache line size data
chunks
110
Advanced Optimization -
Auto-Vectorization
111
Auto-Vectorization in
HotSpot VM??
112
Look Ma, no stubs!!
113
©2016 CodeKaram
SuperWord Level Parallelism
114
http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
©2016 CodeKaram
SuperWord Level Parallelism
115
• Loop Unrolling
• Alignment Analysis
• Pre-Optimization
• Identifying Adjacent Memory References
• Extending the “PackSet”
• Combination and SIMD operation
http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
©2016 CodeKaram
Q & A
116
117
Appendix
©2016 CodeKaram
Compressed Class Pointers
118
• JDK 8 —> Perm Gen Removal —> Class Data outside of
heap
• Compressed class pointer space
• contains class metadata
• is a part of Metaspace
©2016 CodeKaram
Compressed Class Pointers
119
PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013
©2016 CodeKaram120
Mark Word - 32 bit vs 64 bit
Klass - 32 bit vs 64 bit
Array Length - 32 bit on both
boolean, byte, char, float, int, short - 32 bit on both
double, long - 64 bit on both
ILP32 vs. LP64 Field Sizes

The Performance Engineer's Guide To HotSpot Just-in-Time Compilation

  • 1.
  • 2.
    2 About Me … Java/JVM/GCperformance person Current Life: Consultant who solves Managed Runtime performance issues Past Life: Worked with Oracle, Sun, AMD… - Worked with HotSpot VM: JVM heuristics, JIT compiler, GCs: Parallel(Old) GC, G1 GC, CMS GC www.codekaram.com https://www.linkedin.com/in/monicabeckwith Tweet @mon_beck
  • 3.
    ©2016 CodeKaram Establishing theCharacters and The Plot • HotSpot Virtual Machine • Runtime Goal • Common Compilation Techniques • C1, C2 & Tiered Compilations • Adaptive Optimization - The Plot 3
  • 4.
    ©2016 CodeKaram The Storyline •Dynamic Deoptimization • Inlining • Intrinsics 4
  • 5.
    ©2016 CodeKaram The StoryLine •Compressed Oops + Compressed Class Pointers • Escape Analysis • Vectorization (Auto and otherwise) 5
  • 6.
  • 7.
    ©2016 CodeKaram Let’s PeekUnder The Hood 7 Java Application Java APIs Execution Engine Java VM OS + Hardware Runtime
  • 8.
    ©2016 CodeKaram8 Java Application Java API JavaVM OS + HardwareJRE Execution Engine Runtime Java Runtime Environment
  • 9.
    ©2016 CodeKaram The Helpers 9 JavaVM Execution Engine Runtime
  • 10.
    ©2016 CodeKaram HotSpot VM 10 ExecutionEngine Heap Management - Garbage Collection JIT Compilation - Tiered (C1 + C2)
  • 11.
    ©2016 CodeKaram HotSpot VM 11 Runtime VMClass loading Interpreter Bytecode Verification, Exception Handling, Synchronization , Thread Management, ….http://openjdk.java.net/groups/hotspot/ docs/RuntimeOverview.html
  • 12.
  • 13.
    ©2016 CodeKaram HotSpot VM- Template Interpreter 13 Bytecode 1 Bytecode 2 Template Native Code 1 Template Native Code 2 TemplateTable
  • 14.
  • 15.
  • 16.
    ©2016 CodeKaram • CompileThreshold •Identify root of compilation • Method Compilation or On-stack replacement (Loop)? 16 Identifying Performance Critical Methods
  • 17.
    17 JIT Optimization -Client Compiler (C1)
  • 18.
    ©2016 CodeKaram • Startin interpreter • CompileThreshold = 1500 • Compile with client compiler • Few optimizations 18 Client Compiler
  • 19.
    19 JIT Optimization -Server Compiler (C2)
  • 20.
    ©2016 CodeKaram • Startin interpreter • CompileThreshold = 10000 • Compile with optimized server compiler • High performance optimizations 20 Server Compiler
  • 21.
    21 JIT Optimization -Tiered Compilation
  • 22.
  • 23.
    ©2016 CodeKaram • Startin interpreter • Tiered optimization with client compiler • Code profiled information • Enable server compiler 23 Tiered Compilation
  • 24.
    ©2016 CodeKaram24 Tiered Compilation- Effect on Code Cache Tiered Compilation Code Cache Compiled Native Code
  • 25.
    ©2016 CodeKaram • C1compilation threshold for tiered is about a 100 invocations • Tiered Compilation has a lot more profiled information for C1 compiled methods • CodeCache needs to be 5x larger than non-tiered • Default on JDK8 when tiered is enabled (240MB vs 48MB) • Need more? Use -XX:ReservedCodeCacheSize 25 Tiered Compilation - Effect on Code Cache
  • 26.
  • 27.
    ©2016 CodeKaram27 The Plot Startup (Adaptive)JIT Optimizations Profiling of critical hot- spots Interpreter
  • 28.
    ©2016 CodeKaram28 The Plot Startup (Adaptive)JIT Optimizations Profiling of critical hot- spots Interpreter
  • 29.
  • 30.
  • 31.
    ©2016 CodeKaram • Oopsie! •dependencies invalidation • classes unloading and redefinition • uncommon path in compiled code • misguided profiled information 31 Dynamic Deoptimization
  • 32.
  • 33.
    ©2016 CodeKaram PrintCompilation 33 timestamp compilation-idflags tiered- compilation-level Method <@ osr_bci> code- size <deoptimization>
  • 34.
    ©2016 CodeKaram PrintCompilation 34 Flags: %: is_osr_method s:is_synchronized !: has_exception_handler b: is_blocking n: is_native
  • 35.
    ©2016 CodeKaram PrintCompilation 35 567 693% ! 3 org.h2.command.dml.Insert::insertRows @ 76 (513 bytes) 656 797 n 0 java.lang.Object::clone (native) 779 835 s 4 java.lang.StringBuffer::append (13 bytes)
  • 36.
    ©2016 CodeKaram36 573 7042 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  • 37.
    ©2016 CodeKaram37 573 7042 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  • 38.
    ©2016 CodeKaram38 573 7042 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  • 39.
    ©2016 CodeKaram39 573 7042 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  • 40.
    ©2016 CodeKaram40 573 7042 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  • 41.
  • 42.
    ©2016 CodeKaram HotSpot hasstudied Inlining to it’s max potential! 42 Inlining
  • 43.
    ©2016 CodeKaram MinInliningThreshold, MaxFreqInlineSize, InlineSmallCode,MaxInlineSize, MaxInlineLevel, DesiredMethodLimit … 43 Inlining
  • 44.
  • 45.
    ©2016 CodeKaram PrintInling* 45 @ 76java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  • 46.
    ©2016 CodeKaram PrintInling* 46 @ 76java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  • 47.
  • 48.
    ©2016 CodeKaram48 Without Intrinsics JavaMethod JIT Compilation Execute Generated Code
  • 49.
    ©2016 CodeKaram49 Intrinsics Java Method JITCompilation Execute Optimized Code Call Hand- Optimized Assembly Code
  • 50.
    ©2016 CodeKaram PrintInling* 50 @ 76java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  • 51.
    51 Advanced Optimizations - CompressedOops + Compressed Class Pointers
  • 52.
  • 53.
  • 54.
    ©2016 CodeKaram Objects, Fields& Alignment • Objects are 8 byte aligned (default). • Fields: • are aligned by their type. • can fill a gap that maybe required for alignment. • are accessed using offset from the start of the object 54
  • 55.
  • 56.
    ©2016 CodeKaram56 -Tool toanalyze Java object layout :) Java Object Layout (JOL)
  • 57.
    ©2016 CodeKaram JOL CommandLine Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 57
  • 58.
    ©2016 CodeKaram JOL CommandLine Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 58
  • 59.
    ©2016 CodeKaram Compressed OOPsand Compressed Class Pointers 59 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  • 60.
    ©2016 CodeKaram Compressed OOPsand Compressed Class Pointers 60 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  • 61.
  • 62.
    ©2016 CodeKaram A JavaObject Header 62 Header Klass Mark Word Array Length
  • 63.
    63 Java Object LayoutSamples - Uncompressed
  • 64.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 64
  • 65.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 65 Mark Word
  • 66.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 66 Klass
  • 67.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 67
  • 68.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 68 Array Length
  • 69.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 69
  • 70.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 70
  • 71.
    71 Java Object LayoutSamples - Compressed
  • 72.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 72
  • 73.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 73 Mark Word
  • 74.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 74
  • 75.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 75 Klass
  • 76.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 76
  • 77.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 77 Array Length
  • 78.
    ©2016 CodeKaram JOL: 8Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 78
  • 79.
    79 Java Object LayoutHeader Comparison
  • 80.
    ©2016 CodeKaram Compressed vs Uncompressed Compressed: Instancesize: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 80
  • 81.
    ©2016 CodeKaram Compressed vs Uncompressed Compressed: Instancesize: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 81
  • 82.
  • 83.
  • 84.
    ©2016 CodeKaram Compressed OOPsand Compressed Class Pointers 84 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  • 85.
    85 Java Object LayoutSamples - UnCompressed
  • 86.
    ©2016 CodeKaram JOL: Class java.lang.Classobject internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 86
  • 87.
    ©2016 CodeKaram java.lang.Class objectinternals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 87 JOL: Class
  • 88.
    ©2016 CodeKaram java.lang.Class objectinternals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 88 JOL: Class
  • 89.
    89 Java Object LayoutSamples - Compressed
  • 90.
    ©2016 CodeKaram java.lang.Class objectinternals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 90 JOL: Class
  • 91.
    ©2016 CodeKaram java.lang.Class objectinternals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 91 JOL: Class
  • 92.
    ©2016 CodeKaram java.lang.Class objectinternals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 92 JOL: Class
  • 93.
    ©2016 CodeKaram Compressed OOPsand Compressed Class Pointers 93 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift. P P
  • 94.
    94 … but whatis the 3-bit shift?
  • 95.
    ©2016 CodeKaram Compressed OOPs 95 <wide-oop>= <narrow-oop-base> + (<narrow-oop> << 3) + <field-offset>
  • 96.
    ©2016 CodeKaram Compressed OOPs 96 HeapSize? <4 GB (no encoding/ decoding needed) >4GB; <28GB (zero-based) <wide-oop> = <narrow-oop> <narrow-oop> << 3
  • 97.
    ©2016 CodeKaram Compressed OOPs 97 HeapSize? >28 GB; <32 GB (regular) >32 GB; <64 GB * (change alignment) <wide-oop> = <narrow-oop-base> + (<narrow-oop> << 3) + <field- offset> <narrow-oop-base> + (<narrow-oop> << 4) + <field- offset>
  • 98.
    ©2016 CodeKaram Compressed OOPs 98 HeapSize? <4 GB >4GB; <28GB <32GB <64GB Object Alignment? 8 bytes 8 bytes 8 bytes 16 bytes Offset Required? No No Yes Yes Shift by? No shift 3 3 4
  • 99.
  • 100.
    ©2016 CodeKaram • Fastdynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 100 Other Optimizations
  • 101.
    ©2016 CodeKaram • Fastdynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 101 Other Optimizations
  • 102.
  • 103.
    ©2016 CodeKaram • EntireIR graph • escaping allocations? • not stored to a static field or non-static field of an external object, • not returned from method, • not passed as parameter to another method where it escapes. 103 Escape Analysis
  • 104.
    ©2016 CodeKaram104 Escape Analysis allocatedobject doesn’t escape the compiled method allocated object not passed as a parameter + remove allocation and keep field values in registers =
  • 105.
    ©2016 CodeKaram105 Escape Analysis allocatedobject is passed as a parameter + remove locks associated with object and use optimized compare instructions = allocated object doesn’t escape the compiled method
  • 106.
    ©2016 CodeKaram • Fastdynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 106 Other JIT Compiler Optimizations
  • 107.
  • 108.
  • 109.
    ©2016 CodeKaram Vectorization 109 • UtilizeSIMD (Single Instruction Multiple Data) instructions offered by the processor. • Generate assembly stubs • Get benefits of operating on cache line size data chunks
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
    ©2016 CodeKaram SuperWord LevelParallelism 114 http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  • 115.
    ©2016 CodeKaram SuperWord LevelParallelism 115 • Loop Unrolling • Alignment Analysis • Pre-Optimization • Identifying Adjacent Memory References • Extending the “PackSet” • Combination and SIMD operation http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  • 116.
  • 117.
  • 118.
    ©2016 CodeKaram Compressed ClassPointers 118 • JDK 8 —> Perm Gen Removal —> Class Data outside of heap • Compressed class pointer space • contains class metadata • is a part of Metaspace
  • 119.
    ©2016 CodeKaram Compressed ClassPointers 119 PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013
  • 120.
    ©2016 CodeKaram120 Mark Word- 32 bit vs 64 bit Klass - 32 bit vs 64 bit Array Length - 32 bit on both boolean, byte, char, float, int, short - 32 bit on both double, long - 64 bit on both ILP32 vs. LP64 Field Sizes