3. Agenda
•
Java Bytecode Basics
•
Be familiar with Bytecode tools
•
Bytecode evaluation
•
Language Feature vs Bytecode
•
Dive deeper
•
Changes happened in JDK7 and onwards
•
QA
3
4. Java Bytecode Basics
•
Why to know Java Bytecode:-
– It will help you understand your code better.
– Knowing the kitchen from where food is coming
– Clear the myths
4
5. Java Bytecode Tools
●
javap -help
●
javap -c
●
javap -p
●
javap -verbose
●
javap -c -p -verbose → Its a good option
5
6. Bytecode Evaluation
●
Write Once, Run Anywhere (WORA)
●
Provides more safety and security – Managed
env.
●
Optimizations can be done at runtime
●
Its hard to deal with CPU instrs.
6
7. JVM Principals for Bytecode
●
All operations are stack based.
●
Parameters, locals – All uses stack
●
Push/Pop/Dup/Dup2
●
Stack slot – 32 bit (how to handle
long/double ?)
●
Classes - fully qualified name
7
8. Class file format
8
ClassFile {
u4 magic;
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];
}
9. Invoke* method calls
●
invokestatic – invokes class (static) method
●
invokespecial - Invoke instance method;
special handling for superclass, private, and
instance initialization method invocations
●
invokeinterface – invoke interface method
●
invokevirutal - Invoke instance method;
dispatch based on class
9
JDK 7 onwards - InvokeDynmaic
10. Get rid of Myths
●
Java don't have GOTO statements.
– Why we need GOTO at first place ?
●
Use StringBuilders if your string is changing.
– What string concat do in java.
●
How inner classes work
– Do it mock on encapsulation.
●
Many mores …
10
11. Execution Process
11
.java .class
javac
Just In Time
Compiler
Just In Time
CompilerNative CodeNative OS
Java
Interpreter
Java
Interpreter
Java Virtual
Machine
Java Virtual
Machine
12. Flavors of Just In Time (JIT) Compiler
•
JIT comes in two flavors
•
C1 (Client compiler)
– -client option
•
C2 (Server compiler)
– -server option
•
-XX:+TieredCompilation
– Better decision of compilers.
•
What happens if: -client -XX:+TieredCompilation
12
13. JIT optimizations – Expression Optimization
•
Eliminate dead codes.
•
Expression optimization.
13
int someCalculation(int x1, int x2, int x3) {
int res1 = x1+x2;
int res2 = x1-x2;
int res3 = x1+x3;
return (res1+res2)/2; }
int someCalculation(int x1, int x2, int x3) {
return x1; }
14. JIT Optimization – Inline Method
•
Inline method
– Substitute body of the method (<35 bytes of JVM bytecode)
– This provides the best optimization by JIT
– A better inline that C++
– Reduction of function calling overhead. Something else ?
14
int compute(int var) {
int result;
if(var > 5) {
result = computeFurther(var);
}
else {
result = 100;
}
return result;
}
myVal = compute(3);
myVal = 100;
15. JIT Optimization – Caching Technique
•
Caching read calls
– Store it for future request.
– major performance impact in loops
15
Point findMid(Point p1, Point p2) {
Point p;
p.x = (p1.x + p2.x)/2;
p.y = (p1.y + p2.y)/2;
return p;
...
...
x – p1.x;
y – p1.y;
while(!monitor.flag)
….....
….....
boolean flag = monitor.flag
16. JIT Optimization – Uncommon Trap
•
De-optimization
– UncommonTrap
– Scrap the optimization.
16
int compute(int var) {
int result;
if(var > 5) {
result = computeFurther(var);
}
else {
result = 100;
}
return result;
}
int compute(int var) {
if(var > 5)
uncommonTrap();
return 100;
}
17. JIT Optimization – Monomorphic dispatch
•
Class Hierarchy Analysis
– Optimization decision based on assumptions.
– Re-evaluate assumption when new class loaded.
17
public class Animal {
private String color;
public String getColor() {
return color;
} }
myColor = animal.getColor();
public class Animal {
String color;
}
myColor = animal.color;
18. JIT Optimization – Null Checks
•
Implicit Null Check
– Virtual machine believe code will not throw null
pointer.
– What if VM got a null pointer ?
18
x = point.x;
y = point.y;
Equivalents to:
if(point==null) throw new NullPointerException();
else {
x = point.x;
y = point.y;
}
For each reference in your code, VM
checks null pointer. If VM assumes that
Your code will not throw NPE, it will
remove the check. If happened, de-optimize.
Handle the SEGV.
19. JIT Optimization – Multi-threading Env.
•
Eliminate locks if monitor is not reachable from other
threads
•
Join adjacent synchronized blocks on the same object
•
Do check options:
– XX:+UseBiasedLocking
19
20. JIT Optimization – Loop Optimization
•
Combining loops – Two loops can be combined if
taking equivalent time.
•
Inversion loops – Change while into do-while.
•
Tiling loops – Re-organize loop so that it will fix in
cache.
20
22. Benchmarking Difficulties
•
Garbage collect can distort benchmarking.
•
Benchmarking can be distorted by unintentionally
dead code
•
Improper warm-up can distort benchmarking.
•
Dynamic De-optimziation can distort benchmarking.
•
Momomorphic call can distort benchmarking.
•
Inlining of code can distort benchmarking.
22