javaone 2015: from java code to machine code
TRANSCRIPT
© 2015 IBM Corporation
Java Code to Machine CodeHow to write highly optimizeable Java code
© 2015 IBM Corporation
Important Disclaimers
THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.
WHILST EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
ALL PERFORMANCE DATA INCLUDED IN THIS PRESENTATION HAVE BEEN GATHERED IN A CONTROLLED ENVIRONMENT. YOUR OWN TEST RESULTS MAY VARY BASED ON HARDWARE, SOFTWARE OR INFRASTRUCTURE DIFFERENCES.
ALL DATA INCLUDED IN THIS PRESENTATION ARE MEANT TO BE USED ONLY AS A GUIDE.
IN ADDITION, THE INFORMATION CONTAINED IN THIS PRESENTATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM, WITHOUT NOTICE.
IBM AND ITS AFFILIATED COMPANIES SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION.
NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR SHALL HAVE THE EFFECT OF:
- CREATING ANY WARRANT OR REPRESENTATION FROM IBM, ITS AFFILIATED COMPANIES OR ITS OR THEIR SUPPLIERS AND/OR LICENSORS
2
© 2015 IBM Corporation3 3
Monitoring and Diagnostics Architect
@Chris__Bailey
@seabaylea
© 2015 IBM Corporation4
Goals of the talk
Look at how Java code is executed
Look at how Java code is optimized
Learn how to enable optimizations to occur
© 2015 IBM Corporation5
> javac
© 2015 IBM Corporation6
Anatomy of a Java Class Filestruct Class_File_Format { u4 magic_number; u2 minor_version; u2 major_version;
u2 constant_pool_count; cp_info constant_pool[constant_pool_count – 1];
u2 access_flags; u2 this_class; u2 super_class;
u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count];
u2 attributes_count; attribute_info attributes[attributes_count];}
© 2015 IBM Corporation7
Anatomy of a Java Class File
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation8
Anatomy of a Java Class Filepublic class MyObject() { public int myField = 48;
public MyObject() { }}
flags: ACC_PUBLIC, ACC_SUPERConstant pool: #1 = Class #2 #2 = Utf8 MyObject #3 = Class #4 #4 = Utf8 java/lang/Object #5 = Utf8 myField #6 = Utf8 I #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Methodref #4.#11 #11 = NameAndType #7:#8 #12 = Fieldref #1.#13 #13 = NameAndType #5:#6 #14 = Utf8 LineNumberTable #15 = Utf8 LocalVariableTable #16 = Utf8 this #17 = Utf8 LmyObject; #18 = Utf8 SourceFile #19 = Utf8 MyObject.java
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
© 2015 IBM Corporation9
Anatomy of a Java Class File
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation10
Anatomy of a Java Class File
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Constant Pool
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation11
Anatomy of a Java Class File
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Constant Pool
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation12
Anatomy of a Java Class File
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Operand Stack
Constant Pool
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation13
Anatomy of a Java Class File
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation14
Executing Bytecode
© 2015 IBM Corporation15
Executing Bytecodes
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
© 2015 IBM Corporation16
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation17
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
push
Executing Bytecodes
© 2015 IBM Corporation18
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation19
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation20
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject #4 java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation21
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation22
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code #3.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation23
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation24
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation25
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 #7.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation26
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 <init>.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation27
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 <init>.#8 #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation28
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation29
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.#11 <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation30
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation31
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #10 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation32
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
Executing Bytecodes
© 2015 IBM Corporation33
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
MyObject
Executing Bytecodes
© 2015 IBM Corporation34
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
Local Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation35
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation36
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
pushMyObject
Executing Bytecodes
© 2015 IBM Corporation37
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation38
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
pushMyObject
Executing Bytecodes
© 2015 IBM Corporation39
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation40
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation41
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation42
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
#2 MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation43
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation44
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V #1.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation45
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation46
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation47
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
#5.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation48
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
myField.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation49
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
myField.#6 LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation50
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation51
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.#13
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation52
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation53
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield #12 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation54
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield MyObject.myField.I 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
Executing Bytecodes
© 2015 IBM Corporation55
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield MyObject.myField.I 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
pop
Executing Bytecodes
© 2015 IBM Corporation56
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield MyObject.myField.I 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
>
MyObject
48pop
Executing Bytecodes
© 2015 IBM Corporation57
public int myField; flags: ACC_PUBLIC
public MyObject(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial Object.<init>.()V 4: aload_0 5: bipush 48 7: putfield MyObject.myField.I 10: return LineNumberTable: line 4: 0 line 2: 4 line 5: 10 LocalVariableTable: 0 11 0 this LmyObject;
thisLocal Variables
48
this
Operand Stack
Constant Pool
Java Heap
MyObject MyObject java/lang/Object java/lang/Object
myField I <init> ()V
Code Object.<init>.()V <init>.()V MyObject.myField.I
myField.I LineNumberTable LocalVariableTable this
LmyObject SourceFile MyObject.java
public class MyObject() { public int myField = 48;
public MyObject() { }}
> MyObject
48
Executing Bytecodes
© 2015 IBM Corporation58
Local Variables
Operand Stack
Constant Pool
Java Heap
MyObject
48
Executing Bytecodes
© 2015 IBM Corporation59
Machine Code
© 2015 IBM Corporation60
Machine Codepublic synchronized int sync1(){ return intArr[0];}
© 2015 IBM Corporation61
Machine Codepublic synchronized int sync1(){ return intArr[0];}
public int sync2(){ synchronized (this){ return intArr[0]; }}
© 2015 IBM Corporation62
public synchronized int sync1(){ return intArr[0];}
public synchronized int sync1(); descriptor: ()I flags: ACC_PUBLIC, ACC_SYNCHRONIZED Code: stack=2, locals=1, args_size=1 0: aload_0 1: getfield #12 4: iconst_0 5: iaload 6: ireturn
public int sync2(){ synchronized (this){ return intArr[0]; }}
Machine Code
© 2015 IBM Corporation63
public synchronized int sync1(){ return intArr[0];}
public synchronized int sync1(); descriptor: ()I flags: ACC_PUBLIC, ACC_SYNCHRONIZED Code: stack=2, locals=1, args_size=1 0: aload_0 1: getfield #12 4: iconst_0 5: iaload 6: ireturn
public int sync2(); descriptor: ()I flags: ACC_PUBLIC Code: stack=2, locals=2, args_size=1 0: aload_0 1: dup 2: astore_1 3: monitorenter 4: aload_0 5: getfield #12 // Field intArr:[I 8: iconst_0 9: iaload 10: aload_1 11: monitorexit 12: ireturn 13: aload_1 14: monitorexit 15: athrow Exception table: from to target type 4 12 13 any 13 15 13 any StackMapTable: number_of_entries = 1 frame_type = 255 /* full_frame */ offset_delta = 13 locals = [ class test/SyncClass, class test/SyncClass ] stack = [ class java/lang/Throwable ]
public int sync2(){ synchronized (this){ return intArr[0]; }}
Machine Code
© 2015 IBM Corporation64
JIT Optimizations
© 2015 IBM Corporation65
JIT Generated Code
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h ;*iload_00x000000000f12156c: imul edx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
© 2015 IBM Corporation66
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h0x000000000f12156c: imul edx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
JIT Generated Code
© 2015 IBM Corporation67
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h0x000000000f12156c: imul edx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
JIT Generated Code
© 2015 IBM Corporation68
JIT Generated Code
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h0x000000000f12156c: imul rdx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
© 2015 IBM Corporation69
JIT Generated Code
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h0x000000000f12156c: imul rdx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
© 2015 IBM Corporation70
JIT Generated Code
public static int multiply(int a, int b){ return a * b;}
public static int multiply(int, int); descriptor: (II)I flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=2 0: iload_0 1: iload_1 2: imul 3: ireturn LineNumberTable: line 14: 0 LocalVariableTable: Start Length Slot Name Signature 0 4 0 a I 0 4 1 b I
# {method} {0x000000000d4408e0} 'multiply' '(II)I'# parm0: rdx = int# parm1: r8 = int# [sp+0x40] (sp of caller)0x000000000f121560: mov dword ptr0x000000000f121567: push rbp0x000000000f121568: sub rsp,30h0x000000000f12156c: imul rdx,r8d0x000000000f121570: mov rax,rdx0x000000000f121573: add rsp,30h0x000000000f121577: pop rbp0x000000000f121578: test dword ptr0x000000000f12157e: ret
© 2015 IBM Corporation71
Inlining
© 2015 IBM Corporation72
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = multiply(factorial, i); } return factorial;}
public static int multiply(int a, int b){ return a * b;}
© 2015 IBM Corporation73
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = multiply(factorial, i); } return factorial;}
public static int multiply(int a, int b){ return a * b;}
36 instructions
© 2015 IBM Corporation74
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = multiply(factorial, i); } return factorial;}
public static int multiply(int a, int b){ return a * b;}
36 instructions
9 instructions
© 2015 IBM Corporation75
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = multiply(factorial, i); } return factorial;}
public static int multiply(int a, int b){ return a * b;}
36 instructions
9 instructions
© 2015 IBM Corporation76
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = } return factorial;}
public static int multiply(int a, int b){ return a * b;}
36 instructions
9 instructions
© 2015 IBM Corporation77
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = } return factorial;}
public static int multiply(int a, int b){ return a * b;}
inline
36 instructions
9 instructions
© 2015 IBM Corporation78
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = factorial * i; } return factorial;}
public static int multiply(int a, int b){ return a * b;}
inline
36 instructions
9 instructions
© 2015 IBM Corporation79
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = factorial * i; } return factorial;}
public static int multiply(int a, int b){ return a * b;}
9 instructions
36 instructions
© 2015 IBM Corporation80
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = factorial * i; } return factorial;}
public static int multiply(int a, int b){ return a * b;}
29 instructions
9 instructions
36 instructions
© 2015 IBM Corporation81
Inlining
public static int factorial(int num) { int factorial = 1; for (int i = 1; i <= num; i++) { factorial = factorial * i; } return factorial;}
public static int multiply(int a, int b){ return a * b;}
29 instructions
9 instructions
36 instructions
Inlining increases performance by:● Removing Prologue and Epilogue● Removing need to set up arguments
© 2015 IBM Corporation82
Enabling Inlining
Inlining increases performance by removing cost of calling conventions– Prologue, Eplilogue, Setting up parameters
Inlining benefits from certainty:– private, static and final methods are more easily inlined– public methods can be, depending on morphism
Inlining benefits from simplicity:– Must be small: thresholds at ≤35 bytecodes and ≤325 bytecodes– Simple is faster than complex
© 2015 IBM Corporation83
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { dataSet[i] = transformEntry(dataSet[i]); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { StringBuffer sb = new StringBuffer(); Date date = Calendar.getInstance().getTime(); sb.append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(date)); sb.append(" Error as value at MAX_VALUE "); data.setErrorMessage(sb.toString()); return data; } data.setValue(val + 1); return data;}
Too Large
© 2015 IBM Corporation84
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { dataSet[i] = transformEntry(dataSet[i]); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { StringBuffer sb = new StringBuffer(); Date date = Calendar.getInstance().getTime(); sb.append(new SimpleDateFormat("yyyyMMdd_HHmmss").format(date)); sb.append(" Error as value at MAX_VALUE "); data.setErrorMessage(sb.toString()); return data; } data.setValue(val + 1); return data;}
Too Large
Exceptional condition check
© 2015 IBM Corporation85
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { dataSet[i] = transformEntry(dataSet[i]); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { handleError(data); return data;
} data.setValue(val + 1); return data;}
Replace withmethod call
© 2015 IBM Corporation86
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { dataSet[i] = transformEntry(dataSet[i]); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { handleError(data); return data; } data.setValue(val + 1); return data;}
© 2015 IBM Corporation87
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { dataSet[i] = transformEntry(dataSet[i]); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { handleError(data); return data; } data.setValue(val + 1); return data;}
Inline
© 2015 IBM Corporation88
Example of Enabling Inliningpublic void transformData () { for (int i = 0; i < dataSet.length; i ++) { int val = dataSet[i].getValue(); if (val == Integer.MAX_VALUE) { handleError(dataSet[i]); } dataSet[i] .setValue(val + 1); }}
private int transformEntry(int data){ int val = data.getValue(); if (val == Integer.MAX_VALUE) { handleError(data); return data; } data.setValue(val + 1); return data;}
Not inlined
© 2015 IBM Corporation89
Loop Unrolling
© 2015 IBM Corporation90
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
for (String num : numbers) { result += parse(num); } return result;}
private static int parse(String num) { return Integer.parseInt(num);}
© 2015 IBM Corporation91
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
for (String num : numbers) { result += parse(num); } return result;}
private static int parse(String num) { return Integer.parseInt(num);}
inline
© 2015 IBM Corporation92
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
for (String num : numbers) { result += Integer.parseInt(num); } return result;}
private static int parse(String num) { return Integer.parseInt(num);}
inline
© 2015 IBM Corporation93
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
for (String num : numbers) { result += Integer.parseInt(num); } return result;}
private static int parse(String num) { return Integer.parseInt(num);}
© 2015 IBM Corporation94
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
for (String num : numbers) { result += Integer.parseInt(num); } return result;}
private static int parse(String num) { return Integer.parseInt(num);}
unroll
© 2015 IBM Corporation95
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
result += Integer.parseInt("0"); result += Integer.parseInt("1"); result += Integer.parseInt("2"); result += Integer.parseInt("3"); result += Integer.parseInt("4");
return result;}
private static int parse(String num) { return Integer.parseInt(num);}
© 2015 IBM Corporation96
Loop Unrolling
private static final String[] numbers = {"0", "1", "2", "3", "4"};
public static int total() { int result = 0;
result += Integer.parseInt("0"); result += Integer.parseInt("1"); result += Integer.parseInt("2"); result += Integer.parseInt("3"); result += Integer.parseInt("4");
return result;}
private static int parse(String num) { return Integer.parseInt(num);}
Possible because collection is final
© 2015 IBM Corporation97
Enabling LoopUnrolling
Loop unrolling increases performance by removing cost of:– Iterating around loop– Bounds checking
Loop unrolling benefits from certainty:– Know the size of the loop
Loop unrolling benefits from simplicity:– Loop must be inlinable
© 2015 IBM Corporation98
Synchronization
© 2015 IBM Corporation99
Synchronization approaches
public int[] intArr = {0, 1, 2};
public int sync2(){ synchronized (this){ return intArr[0]; }}
© 2015 IBM Corporation100
Synchronization approaches
public int[] intArr = {0, 1, 2};
public synchronized int sync1(){ return intArr[0];}
© 2015 IBM Corporation101
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0;
for (int i = 0; i < intArr.length; i++) { result += getEntry(i); }
return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
© 2015 IBM Corporation102
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0;
for (int i = 0; i < intArr.length; i++) { result += getEntry(i); }
return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
inline
© 2015 IBM Corporation103
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0;
for (int i = 0; i < intArr.length; i++) { result += intArr[entry]; }
return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
inline
© 2015 IBM Corporation104
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0;
for (int i = 0; i < intArr.length; i++) { result += intArr[entry]; }
return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
© 2015 IBM Corporation105
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0; for (int i = 0; i < intArr.length; i++) { result += intArr[entry]; } return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
© 2015 IBM Corporation106
Lock Coarsening
public int[] intArr = {0, 1, 2}; public int arrayCount() { int result = 0; synchronized { for (int i = 0; i < intArr.length; i++) { result += intArr[entry]; } } return result;}
public synchronized int getEntry(int entry){ return intArr[entry];}
Lock coarsen
© 2015 IBM Corporation107
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); synchronized (syncdList) { for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }}
© 2015 IBM Corporation108
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); synchronized (syncdList) { for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }}
© 2015 IBM Corporation109
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); synchronized (syncdList) { for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }}
© 2015 IBM Corporation110
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); synchronized (syncdList) { for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }}
© 2015 IBM Corporation111
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); synchronized (syncdList) { for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }}
© 2015 IBM Corporation112
Lock Elision
public String walkLockedList() { List syncdList = new ArrayList(); for (int i = 0; i < 10; i++) { syncdList.add(i); } return syncdList.toString(); }
Lock elision
© 2015 IBM Corporation113
Enabling Synchronization optimizations
Synchronization optimizations increases performance by removing cost of:– Unnecessary synchronization– Repeated synchronization
Synchronization optimizations from certainty:– Scope of the object being locked
Loop unrolling benefits from simplicity:– Inlining enables removal of locks
© 2015 IBM Corporation114
Field Optimizations
© 2015 IBM Corporation115
Accessing Fieldspublic static int test = 10;public static int getTest() { return test;}
private static int test = 10;public static int getTest() { return test;}
private final static int test = 10;public static int getTest() { return test;}
public static int getTest() { return 10;}
public int test = 10;public int getTest() { return test;}
private int test = 10;public int getTest() { return test;}
public final int test = 10;public int getTest() { return test;}
public int getTest() { return 10;}
© 2015 IBM Corporation116
Static Final fields
public static final int test = 10;
public int getTest() { return test;}
© 2015 IBM Corporation117
Static Final fields
public static final int test = 10;
public int getTest() { return test;}
© 2015 IBM Corporation118
Static Final fields
public static final int test = 10;
public int getTest() { return test;}
inline
© 2015 IBM Corporation119
Static Final fields
public static final int test = 10;
public int getTest() { return 10;}
inline
© 2015 IBM Corporation120
Static Final fields
public static final int test;
static { test = new Random().nextInt();}
public int getTest() { return test;}
© 2015 IBM Corporation121
Static Final fields
public static final int test;
static { test = 7;}
public int getTest() { return test;}
© 2015 IBM Corporation122
Static Final fields
public static final int test = 7;
static { test = 7;}
public int getTest() { return test;}
© 2015 IBM Corporation123
Static Final fields
public static final int test = 7;
public int getTest() { return test;}
© 2015 IBM Corporation124
Static Final fields
public static final int test = 7;
public int getTest() { return test;}
inline
© 2015 IBM Corporation125
Static Final fields
public static final int test = 7;
public int getTest() { return 7;}
inline
© 2015 IBM Corporation126
Instance Final fields
public final int test = 10;
public FieldTest() {
}
public int getTest() { return test;}
© 2015 IBM Corporation127
Instance Final fields
public final int test = 10;
public FieldTest() {
}
public int getTest() { return test;}
inline
© 2015 IBM Corporation128
Instance Final fields
public final int test = 10;
public FieldTest() {
}
public int getTest() { return 10;}
inline
© 2015 IBM Corporation129
Instance Final fields
public final int test;
public FieldTest(int val) { this.test = val;}
public int getTest() { return test;}
© 2015 IBM Corporation130
Instance Final fields
public final int test = val;
public FieldTest(int val) { this.test = val;}
public int getTest() { return test;}
© 2015 IBM Corporation131
Instance Final fields
public final int test = val;
public FieldTest(int val) { this.test = val;}
public int getTest() { return test;}
Not possible
© 2015 IBM Corporation132
Enabling Field optimizations
Field optimizations increases performance by:– Enabling other optimizations
Optimizations result from certainty:– Constants (final) are faster than variables– Locals (parameters and variables) are faster than fields–private can be faster than public
© 2015 IBM Corporation133
Coding Guidelines
© 2015 IBM Corporation134
Coding Guidelines
Follow good programming practices!– Fields that can be final, should be final– Fields that can be private, should be private– Data that can be passed as a parameter, should be passed as a parameter
Follow good object orientated design!– If function can be delegated to another method, it should be delegated
This results in short methods and well described data, allowing optimizations.
© 2015 IBM Corporation135
Defaulting to final and private
Default all fields/variables to final:– Compile time error:
Default all fields/variables to private:– Compile time error:
FieldTest2.java:14: error: cannot assign a value to final variable value this.value = val;
FieldLauncher.java:14: error: test3 has private access in FieldTest fieldTest.test3 = 10;
© 2015 IBM Corporation136
Analyzing the JIT
© 2015 IBM Corporation137
Digging into the generated assembler
HotSpot JIT provides options to look at JIT behaviour:– -XX:+UnlockDiagnosticVMOptions– -XX:+TraceClassLoading– -XX:+LogCompilation– -XX:+PrintAssembly
-XX:+PrintAssembly requires additional hdis library– Installed into jre/bin/server
Generates output to console and hotspot_pid<pid>.log file
© 2015 IBM Corporation138
Visualizing with JITWatch
Open source JITWatch tool provides visualization of data:
“Journal” tells you what optimizations have been applied, and which couldn't be
Also attempts to make optimization suggestions
© 2015 IBM Corporation139
Summary
© 2015 IBM Corporation140
Certainty
Locals are faster than globals– Fields and statics are slow; parameters and locals are fast
Constants are faster than variables– final is your friend, especially final static
private can be faster than public– protected and package are always just as slow as public
Small methods (≤100 bytecodes) are good
Simple is faster than complex
© 2015 IBM Corporation141
Questions?
© 2015 IBM Corporation142
IBM Developer Kits for Javahttp://www.ibm.com/java/jdk
IBM Developer Kits for Node.jshttp://ibm.co/ibmnodejs
WebShere Liberty Profilehttp://wasdev.net
IBM Bluemixhttp://www.ibm.com/bluemix
© 2015 IBM Corporation143
Copyright and Trademarks
© IBM Corporation 2015. All Rights Reserved.
IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corp., and registered in many jurisdictions worldwide.
Other product and service names might be trademarks of IBM or other companies.
A current list of IBM trademarks is available on the Web – see the IBM “Copyright and trademark information” page at URL: www.ibm.com/legal/copytrade.shtml