Table of Contents
JIT of PHP 8
How is PHP code executed?
Opcache Extension
What is the effect of Just In Time compilation?
So how is Just In Time compilation done?
So your performance gains probably won't be huge
Home Backend Development PHP8 An in-depth look at JIT in PHP 8

An in-depth look at JIT in PHP 8

Apr 25, 2022 pm 08:46 PM
php php8

This article will take you to understand the JIT in PHP 8, and talk about how JIT participates in the interpretation process. I hope it will be helpful to everyone!

An in-depth look at JIT in PHP 8

PHP 8's JIT (Just In Time) compiler will be integrated into PHP 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.

JIT of PHP 8

The impact of the PHP 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 PHP 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 PHP'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.

How is PHP code executed?

As we all know, PHP is an interpreted language, but what does this sentence itself mean?

Every time PHP code (command line script or WEB application) is executed, it must go through the PHP interpreter. The most commonly used are the PHP-FPM and CLI interpreters.

The interpreter's job is simple: receive PHP 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 PHP, the process is as follows:

  • Reads the PHP 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 PHP 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 PHP we call Opcodes. The process of converting ASTs into Opcodes is called compilation .

  • With Opcodes, here comes the fun part: executing Code! PHP 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:

An in-depth look at JIT in PHP 8

A simplified version of the PHP interpretation process overview.

As you can see. Here is a question: Even if the PHP 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.

Opcache Extension

The Opcache extension comes with PHP and there is usually no need to disable it. When using PHP 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:

An in-depth look at JIT in PHP 8

PHP’s interpretation process using Opcache. If the file has already been parsed, PHP will get cached Opcodes for it instead of parsing it again.

Perfectly skipping the Lexing/Tokenizing, Parsing and Compiling steps?.

Side Note: This is the awesome PHP 7.4 Preloading Features RFC! Allows you to tell PHP FPM to parse the code base, convert it into Opcodes and cache it before executing it.

Do you want to know how JIT participates in this interpretation process? This article will explain.

What is the effect of Just In Time compilation?

After listening to Zeev's PHP and JIT broadcast on PHP Internals News, I figured out what JIT actually does.

If the Opcache extension can get Opcodes faster and transfer them directly to the Zend VM, the JIT allows them to run without using the Zend VM at all.

Zend VM is a program written in C that acts as a layer between Opcodes and the CPU. JIT generates compiled code directly at runtime, so PHP can skip the Zend VM and be executed directly by the CPU. In theory, the performance will be better.

This sounds strange because a specific implementation needs to be written for each type of structure before it can be compiled into machine code. But in fact this is reasonable.

PHP’s JIT uses a library called DynaASM (Dynamic Assembler), which maps a set of CPU instructions in a specific format into assembly code for many different CPU types. Therefore, the compiler only needs to use DynASM to convert Opcodes into machine code for a specific structure.

However, there is a problem that has troubled me for a long time.

If preloading can parse PHP code into Opcodes before execution, and DynASM can compile Opcodes into machine code (Just In Time compilation), why don't we use Ahead of Time compilation) What about compiling PHP immediately?

One of the reasons I found out by listening to Zeev's broadcast is that PHP is a weakly typed language, which means that PHP often doesn't know the type of a variable until the Zend VM tries to execute an opcode.

You can check the Zend_value union type to learn that many pointers point to variables of different types. Whenever Zend VM tries to get a value from a Zend_value, it uses macros like ZSTR_VAL to get a pointer to a string in the union type.

For example, this Zend VM handler handles "less than or equal to" (

Using machine code to perform type inference logic is not feasible and may become slower.

Evaluating first and then compiling is also not a good choice, because compiling to machine code is a CPU-intensive task. So compiling everything at runtime is also not good.

So how is Just In Time compilation done?

Now we know that types cannot be well inferred to compile ahead of time. We also know that compilation at runtime is computationally expensive. So what are the benefits of JIT for PHP?

In order to seek balance, PHP's JIT tries to compile only valuable Opcodes. To do this, the JIT analyzes the Opcodes that the Zend VM is going to execute and checks for possible compilations. (According to the configuration file)

When an Opcode is compiled, it will hand execution to the compiled code instead of to Zend VM. It looks like this:

An in-depth look at JIT in PHP 8

#JIT interpretation process for PHP. If compiled, Opcodes are not executed by Zend VM.

Therefore, in the Opcache extension, there are two detection instructions to determine whether to compile Opcode. If so, the compiler will use DynASM to convert this Opcode to machine code and execute this machine code.

Interestingly, since the code compiled in the current interface has a MB limit (also configurable), code execution must be able to switch seamlessly between JIT and interpreted code.

By the way, this talk by Benoit Jacquemont on JIT in php helped me understand this whole thing.

I'm still not sure when the compilation part was effectively done, but I guess right now I really don't want to know.

So your performance gains probably won't be huge

I hope it's now clear to everyone why most php applications don't get big performance gains from using a just-in-time compiler. This is why Zeev recommends that profiling and experimenting with different JIT configurations for your application is the best approach.

If you're using PHP FPM, you'll typically share compiled opcodes across multiple requests, but that's still not a game changer.

This is because JIT optimizes computationally intensive operations and most php applications today are more I/O bound than anything else. If you are accessing disk or network anyway, then handle It doesn't matter whether the operation is compiled or not. The timing will be very similar.

Unless...

You are doing something that is not I/O bound, like image processing or machine learning. Anything that doesn't touch I/O will benefit from a JIT compiler.

This is why people now say that we prefer to write native functions in PHP instead of writing in C. If this function were to be compiled anyway, the overhead would be unexpressive.

Fun times becoming a PHP programmer...


I hope this article will be helpful to you and enable you to better understand the JIT of PHP8.


Original address: https://thephp.website/en/issue/php-8-jit/

Recommended: "PHP Video Tutorial

The above is the detailed content of An in-depth look at JIT in PHP 8. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles