DEV Community

Syed Ashar Ali
Syed Ashar Ali

Posted on

Why JIT was introduced?

Image description
As we know that there are two ways of translating in programming which is "Compiler" and "Interpreter". There are pros and cons in each of these ways of handling the translation. 

Interpreter as compared to Compiler, has quickly get up and running ability which makes it faster. But the JavaScript Interpreter trade-off was to interpret the code over and over again in case of looping.

To get the best of both worlds, browser mixed the compilers in and added a new part in JS engine named as "ðĶðĻ𝐧ðĒ𝐭ðĻðŦ" or "ðĐðŦðĻ𝐟ðĒðĨ𝐞ðŦ".
What monitor does is that it watches the code entirely as it runs. It keep tracks of things like how often the function has been executed. In case of repeated functions, that function is considered as "𝐰𝐚ðŦðĶ𝐎". That warmed function then get it off the "𝐛𝐚𝐎𝐞ðĨðĒ𝐧𝐞 𝐜ðĻðĶðĐðĒðĨ𝐞ðŦ" to create compiled version of it.

The baseline compiler will do it in chunks. Each operation in the function is compiled into one of more "𝐎𝐭ðŪ𝐛𝐛𝐎". Stubb will be specific to whatever types are being used to either sides of that operator and stored. In case of repeated operation with same operator and same type of either side of operator, the stored stubb will be used instead of reading the block of code again. This will save in translation time and help speed things up.

As one of the characteristics of compiler is that it takes time to think the best way to communicate with the machine which we referred as "optimization". The baseline compiler will make some optimizations but it does not wants to take up much of the time as the code is executing as the same time. If the code is very hot, it will take worthwhile in optimization.

In case of a very hot function, the monitor send that function to "ðĻðĐ𝐭ðĒðĶðĒzðĒ𝐧𝐠 𝐜ðĻðĶðĐðĒðĨ𝐞ðŦ" and this will create the faster version of that function. The optimizing compiler makes assumptions. For example: if the optimizing compiler assumes that all of the objects that are created by a particular constructor had a same shape, so the object has the same property names and they have been added in the same order. So the optimizing compiler uses the information that the monitor has been gathering to make such judgements and if something has been true in the past of code, it continues to consider it to be true.

The compiled code needs to be validated and if it's not true, the JIT assumed it as wrong assumption and dumped the optimized code. At this point, it goes back to the compiled version and do "ðŦ𝐞-ðĻðĐ𝐭ðĒðĶðĒðģðĒ𝐧𝐠".

There could be a possibility of running cycle between the optimizing and re-optimizing in case of wrong assumption, so JIT keep tracks of how many times they have optimized a function, and it's not working out then it will mark it to not to optimize the function and move on.

This is JIT in nutshell. Thankyou for reading it out till the end. This is my first blog and I'll be looking for more blogs in future.

Top comments (0)