Optimization of the JIT mechanism in Java for running speed

Source: Internet
Author: User

The origin of the JIT

In the previous blog, Javac compilation process, briefly described the Java compiler (JAVAC), you can see JAVAC and C compiler is not the same, not directly to the Java source code compiled into a processor instructions. Instead, it produces a uniform specification, unrelated to the binary format of the machine bytecode. In the execution period, the JVM will explain the execution of the bytecode, which is the main reason why Java is so successful across platforms, and you can write and build a copy of it on one platform and execute it on top of other platforms.

But this also leads to serious problems, and interpret is usually slower than direct compile into a platform-qualified native binary code. Sun was aware of this severity in the late 90, when they asked Dr. Cliff Click to provide a solution.

Hot Spot

They introduced the JIT compiler (instant compiler) in the virtual machine, and when the virtual machine discovers that a method or block of code is running particularly frequently, it will identify the code as "Hot Spot Code", in order to improve the efficiency of hot spot code execution, at run time, The virtual machine will compile this code into the machine code associated with the local platform and optimize it at all levels, and it is the JIT compiler that accomplishes this task. In some cases, a well-tuned JVM might be more efficient than manual C + + or C.

Now almost all of the mainstream commercial virtual machines (such as Sun HotSpot, IBM J9) contain both interpreters and compilers. When the program needs to start and execute quickly, the interpreter can play a role first, save the compile time, immediately execute, when the program runs, over time, the compiler will gradually return to function, the more and more code to compile the cost of code, you can get higher execution efficiency. Interpreting execution can save memory, and compiling execution can improve efficiency.

There are two types of hot Spot Code that will be compiled by the instant compiler during the run:

?   被多次调用的方法。?   被多次调用的循环体。

In both cases, the compiler takes the entire method as the compilation object, and this compilation is also the standard way to compile the virtual machine. To know if a piece of code or method is not hot code, it is necessary to trigger the immediate compilation, need to do hot spot Detection (hotspot detection). At present, the main hot spot determination methods have the following two kinds:

    • Sampling-based hotspot detection: the virtual opportunity of this method periodically checks the top of each thread's stack, and if some methods are found to be present at the top of the stack, then this method code is "hot Spot code". The advantage of this detection method is that it is simple and efficient, and it is easy to get the method call relationship, the disadvantage is that it is difficult to accurately confirm the heat of a method, it is easy to disturb the hotspot detection because of the thread blocking or other external factors.

    • Hot spot detection based on counter: the virtual opportunity of this method is to set up counter for each method, even code block, the number of execution times of statistic method, if the number of executions exceeds certain threshold, it is considered as "hot method". This kind of statistical method is more complicated, it needs to establish and maintain counter for each method, and can't get the call relation of method directly, but its statistic result is more precise and rigorous.

Specific examples

Here's an example of how JIT can improve program performance

 class Calculator {     Wrapper wrapper;     publicvoidcalculate() {         y = wrapper.get();         z = wrapper.get();         sum = y + z;     } } class Wrapper {     intvalue;     intget() {         returnvalue;     } }

This is a piece of code written by a developer, assuming this code is hot Spot

class Calculator {     Wrapper wrapper;     publicvoid calculate() {         y = wrapper.value;         sum = y + y;     class Wrapper {     finalint value;     finalint get() {         return value;     } }

This is the equivalent of the hotspot VM after hot Spot Detection to optimize the code, of course, JIT is the bytecode compiled cost of machine code, shown here is optimized after the equivalent of the source code.

Look at the compilation optimizations above, first of all

    1. Inline method (Methods inline)
      Use B.value instead of wrapper.get () to reduce latency by not directly accessing Wrapper.value through function calls.
 class Calculator {     Wrapper wrapper;     publicvoidcalculate() {         y = wrapper.value;         z = wrapper.value;         sum = y + z;     } }

2. Remove the extra load: replace z = Wrapper.value with z = y, so only the zone variable is accessed instead of the wrapper.value to reduce the delay.

 class Calculator {     Wrapper wrapper;     publicvoidcalculate() {         y = wrapper.value;         z = y;         sum = y + z;     } }

3.copy propagation (Replication propagation): replace Z = y with y = y, there is no need to use a variable z, because Z and y will be equal.

 class Calculator {     Wrapper wrapper;     publicvoidcalculate() {         y = wrapper.value;         y = y;         sum = y + y;     } }

4 Eliminate unused source code: y = y is unnecessary and can be wiped out.

 class Calculator {     Wrapper wrapper;     publicvoidcalculate() {         y = wrapper.value;         sum = y + y;     } }

The above is the optimization of a single class, the following is the optimization of the inheritance relationship

 Public  interface Animal {     Public void Eat();} Public  class Cat implements Animal{     Public void Eat() {System.out.println ("Cat eat Fish"); }} Public  class Test{     Public void MethodA(Animal Animal)    {animal.eat (); }}

First, the entire "type inheritance" of Animal is analyzed, and only one implementation class cat is found, then the code in MethodA (Animal Animal) can be optimized as follows.

   publicvoidmethodA(Animal animal){        System.out.println("cat eat fish");    }

However, if later in the run process, "type inheritance relationship" found animal another implementation of the class dog, then not in the execution before the optimization of the compiled machine code, but to perform interpretation, that is, the following "inverse optimization."

Inverse optimization:

When the execution of the compiled machine code no longer meets the optimization criteria, the corresponding part of the machine code is returned to the interpretation execution.

All of the above is C1 optimization, there are mainly for the service-side program Optimization C2 optimization, here is no longer introduced.

Optimization of the JIT mechanism in Java for running speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.