Does JIT and Interpreter run in parallel?

It is redundant. A bytecode that is compiled with the JIT to native code will no longer need the interpreter. Code that is interpreted does not go through the JIT.

Think of the interpreter as a big switch statement that receives the bytecodes for a function and does what the bytecode tells it to do.

void interpret(function) { 
    foreach (instruction in function) {
        switch (instruction) {
            case ADD:
                // add the parameters
                break;
            case MULT:
                // multiply the parameters
                break;
            ...
        }
    } 
}

So why have both at the same time? Good question, the reason is performance.

Suppose you need to run a java function once. How long would it take? Lets assume it takes 50ms to JIT compile a function, 1 ms to execute the JIT compiled function and 7 ms to interpret it:

Option 1, we JIT compile the function:

TotalTime=TimeToJIT+TimeToExecute=50+1=51msTotalTime=TimeToJIT+TimeToExecute=50+1=51ms

Option 2, we Interpret the function:

TotalTime=TimetoInterpret=7msTotalTime=TimetoInterpret=7ms

Even though executing a JIT compiled function is faster than interpreting it, the time it takes to JIT compile means it’s not worth it. Thus if we are running the function once, it might be faster to interpret it.

Suppose you need to run the same function 1000 times (for example it is called in a loop), How long would it take?

Option 1, we JIT compile the function:

TotalTime=TimeToJIT+TimeToExecute∗1000=50+1∗1000=1050msTotalTime=TimeToJIT+TimeToExecute∗1000=50+1∗1000=1050ms

Option 2, we interpret the function:

TotalTime=TimetoInterpret∗1000=7∗1000=7000msTotalTime=TimetoInterpret∗1000=7∗1000=7000ms

Notice that we only need to JIT compile the function once and execute the compiled function 1000 times. In this case the time it takes to JIT compile the function dwarfs in comparison to the time it takes to execute it. The fastest way would be to JIT compile the function.

There are memory implications too. It takes more memory to JIT compile a function.

The VM is always playing a game of heuristics to determine when it is appropriate to interpret and when it is appropriate to JIT compile a function. It is able to execute the program faster over all by having both mechanisms available and choosing function by function whether to JIT or interpret it.

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *