What does a just-in-time (JIT) compiler do?

后端 未结 18 1727
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:42

What does a JIT compiler specifically do as opposed to a non-JIT compiler? Can someone give a succinct and easy to understand description?

相关标签:
18条回答
  • 2020-11-22 12:57

    As other have mentioned

    JIT stands for Just-in-Time which means that code gets compiled when it is needed, not before runtime.

    Just to add a point to above discussion JVM maintains a count as of how many time a function is executed. If this count exceeds a predefined limit JIT compiles the code into machine language which can directly be executed by the processor (unlike the normal case in which javac compile the code into bytecode and then java - the interpreter interprets this bytecode line by line converts it into machine code and executes).

    Also next time this function is calculated same compiled code is executed again unlike normal interpretation in which the code is interpreted again line by line. This makes execution faster.

    0 讨论(0)
  • 2020-11-22 12:58

    JIT stands for Just-in-Time which means that code gets compiled when it is needed, not before runtime.

    This is beneficial because the compiler can generate code that is optimised for your particular machine. A static compiler, like your average C compiler, will compile all of the code on to executable code on the developer's machine. Hence the compiler will perform optimisations based on some assumptions. It can compile more slowly and do more optimisations because it is not slowing execution of the program for the user.

    0 讨论(0)
  • 2020-11-22 12:59

    A just in time compiler (JIT) is a piece of software which takes receives an non executable input and returns the appropriate machine code to be executed. For example:

    Intermediate representation    JIT    Native machine code for the current CPU architecture
    
         Java bytecode            --->        machine code
         Javascript (run with V8) --->        machine code
    

    The consequence of this is that for a certain CPU architecture the appropriate JIT compiler must be installed.

    Difference compiler, interpreter, and JIT

    Although there can be exceptions in general when we want to transform source code into machine code we can use:

    1. Compiler: Takes source code and returns a executable
    2. Interpreter: Executes the program instruction by instruction. It takes an executable segment of the source code and turns that segment into machine instructions. This process is repeated until all source code is transformed into machine instructions and executed.
    3. JIT: Many different implementations of a JIT are possible, however a JIT is usually a combination of a compliler and an interpreter. The JIT first turn intermediary data (e.g. Java bytecode) which it receives into machine language via interpretation. A JIT can often sense when a certain part of the code is executed often and the will compile this part for faster execution.
    0 讨论(0)
  • 2020-11-22 13:03

    JIT compiler only compiles the byte-code to equivalent native code at first execution. Upon every successive execution, the JVM merely uses the already compiled native code to optimize performance.

    Without JIT compiler, the JVM interpreter translates the byte-code line-by-line to make it appear as if a native application is being executed.

    Source

    0 讨论(0)
  • 2020-11-22 13:03

    Just In Time Compiler (JIT) :
    It compiles the java bytecodes into machine instructions of that specific CPU.

    For example, if we have a loop statement in our java code :

    while(i<10){
        // ...
        a=a+i;
        // ...
     }
    

    The above loop code runs for 10 times if the value of i is 0.

    It is not necessary to compile the bytecode for 10 times again and again as the same instruction is going to execute for 10 times. In that case, it is necessary to compile that code only once and the value can be changed for the required number of times. So, Just In Time (JIT) Compiler keeps track of such statements and methods (as said above before) and compiles such pieces of byte code into machine code for better performance.

    Another similar example , is that a search for a pattern using "Regular Expression" in a list of strings/sentences.

    JIT Compiler doesn't compile all the code to machine code. It compiles code that have a similar pattern at run time.

    See this Oracle documentation on Understand JIT to read more.

    0 讨论(0)
  • 2020-11-22 13:03

    You have code that is compliled into some IL (intermediate language). When you run your program, the computer doesn't understand this code. It only understands native code. So the JIT compiler compiles your IL into native code on the fly. It does this at the method level.

    0 讨论(0)
提交回复
热议问题