JIT Compiler defines Just in Time Compiler which has been recently introduced in PHP version 8. As you know PHP is a interpreted language, which means our code does get get compiled before run but the PHP engine read the code and run.

Actually PHP execution happens in 4 stages:

  1. Lexing/Tokenizing
  2. Parsing
  3. Compilation
  4. Interpretation
PHP Execution
PHP Execution

Lexing/Tokenizing

In first stage the interpreter reads our code and generate a sequence of tokens. In programming, Lexical analysis is the process of converting a sequence of code into sequence of tokens.

Parsing

Its check for syntax rules and uses the previously generated tokens to build an Abstract Syntax Tree (A hierarchical representation of structure of source code).

Compilation

Here AST nodes are read and translated by PHP into opcode. These opcode is the language which Zend VM (PHP’s Virtual Machine) understands.

Interpretation

These opcode is interpreted and run in the Zend VM. 

So till now, php compile first and then move to interpreter step and for this it uses a concept called OPCache (Opcode Cache). This will store the results of the compile step, so it does not require to re-compile on next time.

How JIT Works?

JIT compiler compiles opcode into machine code and run that code instead of giving it  to Zend VM to run. It is implemented as an independent part of OPCache. This also ensure that we do not require a interpreter any more and the code will run much faster than that of before.

Facts

The code is converted to machine code and run directly by the CPU instead of the interpreter, the speed will definitely increase for computational code and also improve CPU usage. But most of our programme depends upon the database queries, API, http requests etc, even our web application remains idle just waiting for a api response or db operation. So for  such scenario it might not be much beneficial in terms of speed or performance.

PHP 8 performance diagram

Thanks for reading! For any queries feel free to add in comments.

Leave a Reply

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