Home > Article > Backend Development > Understanding PHP 8's JIT
【Related recommendations: Understanding PHP 8s JIT8】
Understanding PHP 8s JIT 8's JIT (Just In Time) compiler will be integrated into Understanding PHP 8s JIT as an extension. The Opcache extension is used to convert certain opcodes directly into cpu instructions at runtime.
This means that after using JIT, Zend VM does not need to interpret certain opcodes, and these instructions will be executed directly as CPU-level instructions.
The impact of the Understanding PHP 8s JIT 8 Just In Time (JIT) compiler is unquestionable. But so far, I've found that very little is known about what JIT is supposed to do.
After much research and giving up, I decided to check the Understanding PHP 8s JIT source code myself. Combining some of my knowledge of the C language and all the scattered information I've gathered so far, I come up with this article, which I hope will help you understand Understanding PHP 8s JIT's JIT better.
To put it simply: When the JIT works as expected, your code is not executed through the Zend VM, but directly as a set of CPU-level instructions.
That's the whole idea.
But to understand it better we need to consider how php works internally. Not very complicated, but needs some introduction.
I wrote a blog post that gives a general overview of how php works. If you feel like this post is too much, just check another one and come back later. Things will become easier to understand.
As we all know, Understanding PHP 8s JIT is an interpreted language, but what does this sentence itself mean?
Every time Understanding PHP 8s JIT code (command line script or WEB application) is executed, it must go through the Understanding PHP 8s JIT interpreter. The most commonly used are the Understanding PHP 8s JIT-FPM and CLI interpreters.
The interpreter's job is simple: receive Understanding PHP 8s JIT code, interpret it, and return the result.
General interpreted languages follow this process. Some languages may eliminate a few steps, but the general idea is the same. In Understanding PHP 8s JIT, the process is as follows:
Reads the Understanding PHP 8s JIT code and interprets it as a set of keywords called Tokens. This process lets the interpreter know what code has been written in each program. This step is called Lexing or Tokenizing.
#After getting the Tokens collection, the Understanding PHP 8s JIT interpreter will try to parse them. An abstract syntax tree (AST) is generated through a process called Parsing. Here AST is a set of nodes representing what operations to perform. For example, "echo 1 1" actually means "print the result of 1 1" or more specifically "print an operation, this operation is 1 1".
With AST , it is easier to understand operations and priorities. Converting an abstract syntax tree into an operation that can be executed by the CPU requires a transition expression (IR), which in Understanding PHP 8s JIT we call Opcodes. The process of converting ASTs into Opcodes is called compilation .
With Opcodes, here comes the fun part: executing Code! Understanding PHP 8s JIT has an engine called Zend VM, which is able to receive a series of Opcodes and execute them. After all Opcodes are executed, Zend VM terminates the program.
This picture can make it clearer for you:
A simplified version of the Understanding PHP 8s JIT interpretation process overview.
As you can see. Here is a question: Even if the Understanding PHP 8s JIT code has not changed, will this process still be followed every time it is executed?
Let’s look back at Opcodes. correct! This is why the Opcache extension exists.
The Opcache extension comes with Understanding PHP 8s JIT and there is usually no need to disable it. When using Understanding PHP 8s JIT it is best to turn on Opcache.
Its function is to add a memory shared cache layer to Opcodes. Its job is to extract newly generated Opcodes from the AST and cache them so that the Lexing/Tokenizing and Parsing steps can be skipped during execution.
This is a process diagram that includes the Opcache extension:
Understanding PHP 8s JIT’s interpretation process using Opcache. If the file has already been parsed, Understanding PHP 8s JIT will get cached Opcodes for it instead of parsing it again.
Perfectly skipping the Lexing/Tokenizing, Parsing and Compiling steps
Related learning recommendations: php programming (video)
The above is the detailed content of Understanding PHP 8's JIT. For more information, please follow other related articles on the PHP Chinese website!