JavaScript - JIT Compilation

JavaScript - JIT Compilation

History of JS:

  • JS was invented in 1995, for Netscape(one of the classic web browsers in 90’s).
  • JS is a scripting language, you can do many dynamic things and control the events on the web page.

JavaScript


What's called JS?

  • JS is a high-level, interpreted, multiparadigm, dynamically typed language.
  • It’s human-readable language. so the machine can't understand this.
  • So it needs the translator, to translate the high-level language to machine-level language. we need an interpreter or compiler. Because the Machine needs the machine language.


Compiler:?

The compiler collects and converts all the code into machine code. And execute in a single shot. Before implementing it’ll generate an Executable file. It’s used in languages like C, C++, and JAVA.

Pros:

  • Time consumption is very low because it does not disturb the machine for every line.
  • Portability, once you build the file, you can run it on any machine.
  • Optimized output, because during compilation code goes through various optimization processes.

Cons:

  • Compilation time, if the project size increases, compilation time also will increase.


Interpreter:

The interpreter is fast, it translates the source code line by line and executes it immediately line by line.

Pros:

  • It’ll detect the error instantly, don't need to wait for the whole compilation.

Cons:

  • Slower than a compiler, because it executes line-by-line
  • Optimization is less than the compiler because of limited code optimization opportunities.

Entry of Super Hero:

JS is an interpreted language, it uses an interpreter in the browser for language conversion. However, interpreters have some performance inefficiency. interpreters don't have any optimization logic. If we are running one loop in JS, every loop goes via an interpreter.?

To overcome this, we need one add-on. I.e JIT.

JIT:

It’s a Hybrid Execution Model. This is a technique used in dynamic programming languages like JavaScript, Python, and Java.

JIT compilation is the combination of both Interpreter and compiler to improve the optimization and runtime performance. This hybrid compilation system balances prompt code execution and performance in code execution patterns.

Code execution of JS using JIT

Parsing and Lexing:

  • Parsing helps to convert the code into AST- Abstract Syntax Tree. It’s the representation of code in the tree structure.?
  • Check more about AST here -> https://astexplorer.net/

AST

  • Lexing: Lexical Analysis is called Lexing. It breaks the sequence of code into tokens before parsing.


?Profiling:

Before the code execution, the JS engine collects all data about the code such as usage of code, frequently called functions, and code which affects the performance. This is called Profiling. And also it helps the compiler and interpreter to understand the run time behavior of code.


Monitor:

This system tracks the code execution, memory usage, CPU consumption, and benchmarks. And helps to identify the performance bottlenecks.


Profiling and Optimization:

?? The code has been sectioned based on profiling and monitoring, like

  1. Cold code -The code is executed for the first time during compilation.
  2. Hot code - The code is executed frequently in execution.
  3. Warm code - The code is executed less frequently than warm code.


  • Based on the code level, the engine will decide the flow.
  • If it is cold code, no need to compile, it directly executes via the interpreter.
  • If it is warm or hot code, the code will be processed via the compiler.


Optimization decision:

  • Based on the output of profiling and monitoring, the Engine will decide on the best optimization techniques and which part of the code has to be optimized.?
  • This decision is based on the frequency of execution, code complexity, and performance metrics.

Optimization- JIT Compilation:

  • After the decision, if the hot code path is identified, the engine will trigger the JIT compilation.
  • JIT compilation converts the Javascript code into machine-level code by harnesses hardware resources.


Store and Execution:

  • Once the code is compiled, Optimized High-level code is stored in memory and directly executed by the processor.
  • So subsequent code executions of hot code bypass the process of interpreter, parsing, and AST. The Optimized machine code will execute directly.
  • It results in faster and more efficient code execution of performance-critical code sections.


Dynamic Adaptation:

Based on run time code checks, Dynamic Adaptations will continuously monitor the optimization strategies to improve efficiency.

Runtime Feedback:

?Runtime feedback provides insights into how the program executes during the final execution. Such as code paths, and resource usage patterns.

Reoptimization and Deoptimization:

  • If any changes in run-time conditions, the compiler will trigger the Reoptimization or Deoptimization.
  • Reoptimization is the process which recompiling the code with different optimization techniques based on new profiling data or based on run time feedback.
  • If runtime feedback is not valid or JIT compilation is invalid, like when the code is changed, then the engine has to deoptimize the code, the code will revert to the interpreter or recompilation will start with different optimization methods. This process is called Deoptimization.

So the JS engine orchestrates the code execution decision with the help of runtime feedback data, static analysis,? continuous monitoring, and profiling.


?I’m wrapping up. Thanks, everyone for joining me on this journey through the world of? JS JIT Engines. And if any mistakes were made, please feel free to correct me- It’s all part of learning.

Let's keep Exploring Together.

要查看或添加评论,请登录

Nanthakumar D的更多文章