Vaibhav's Blog Space

  • Java
    July 13, 2015

A fast overview of Just-In-Time(JIT) Compiler

JIT comes in these flavors:
 C1 (Client compiler) -client option
C2 (Server compiler)-server option
-XX:+TieredCompilation - Better decision of compilers.

Common Optimizations done by Just-In-Time (JIT) Compiler do:

 1. Eliminate dead codes and Expression optimization.

 int someCalculation(int x1, int x2, int x3) {
  int res1 = x1+x2;
int res2 = x1-x2;
  int res3 = x1+x3;
return (res1+res2)/2;

will be converted to

int someCalculation(int x1, int x2, int x3) {

return x1;

 2. Inline Method

- Substitute body of the method (<35 bytes of JVM bytecode)
- This provides the best optimization by JIT
- A better inline that C++ 

For Example: 

int compute(int var) {
int result;
if(var > 5) {
result = computeFurther(var);
else {
result = 100;
return result;

If you call myVal = compute(3); it will get converted into myVal = 100;

3. Caching Technique:

Point findMid(Point p1, Point p2) {
Point p;
p.x = (p1.x + p2.x)/2;
p.y = (p1.y + p2.y)/2;
return p;

p1.x, p2.x -> It can convert into temp1, temp2 and can be cached.

4. Monomorphic dispatch:

public class Birds {
private String color;
public String getColor() {
return color;
} }
myColor = birds.getColor(); 

If there is no other override of this method, it will convert into

public class Birds {
String color;

mycolor = birds.color; 

5. Null Checks Removal:

x = point.x;
y = point.y;
At JVM it is equivalents to:
if(point==null) throw new NullPointerException();
else {
x = point.x;
y = point.y;

But if the code will not throw NullPointer for more than threshold reference, it will remove the if check.

6. Threading Optimizations:

- Eliminate locks if monitor is not reachable from other threads
- Join adjacent synchronized blocks on the same object

7. Loop Optimizations: 

- Combining loops – Two loops can be combined if taking equivalent time.
- Inversion loops – Change while into do-while. (why, just give a javap -c)
- Tiling loops – Re-organize loop so that it will fix in cache. 

VM Args:

Xint – Interpreter mode
Xcomp – Compiled mode
Xmixed – Interpreter + Compiler
-server → C2 compiler
-client → C1 compiler
-XX:+TieredCompilation → C1 + C2 (used by 32/64 bit mode) 

Logging Options:

-XX:LogFile=<path to file>

(For more see my last blog)

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.