Table of Contents
Inspired by HHVM, we decided to build PHP with both performance and functionality
For PHP, which is not a strongly typed language, it is even more difficult to import JIT
Home Backend Development PHP7 The key to doubling PHP7 performance revealed

The key to doubling PHP7 performance revealed

Jun 24, 2020 pm 05:31 PM
php7 performance

The key to doubling PHP7 performance revealed

It is not easy for a mature language that has gone through many revisions and countless optimizations in the past 20 years to have a breakthrough that doubles its performance. Rasmus Lerdorf admits that unlike ordinary new languages, It is often easy to find a lot of room for improvement in projects. The new version of PHP does not achieve such results by modifying some programs.

The 20-year-old veteran web programming language PHP will release a new version of PHP 7 as soon as the end of October. This is the first major revision in ten years. The biggest feature is a major breakthrough in performance, which is better than the previous version. PHP 5 is twice as fast. Rasmus Lerdorf, the father of PHP, said that it can even perform faster than the PHP program under the HHVM virtual machine.


A mature language that has gone through many revisions and countless optimizations over the past 20 years can still have a breakthrough that doubles its performance. It is not an easy task, Rasmus Lerdorf said frankly. Unlike other new projects where it is easy to find a lot of room for improvement, the new version of PHP does not achieve such results by modifying some programs. On the contrary, after a lot of detailed optimization and performance accumulation, PHP 7 has an execution performance that is not inferior to HHVM.

Rasmus Lerdorf and the PHP core contribution team have spent a lot of effort to reduce the number of memory bits moved when the program is running, thereby speeding up execution performance. For example, the data structure zval for storing variables in PHP is reduced from 24 bits to 16 bits, and Hashtable is reduced from 72 bits to 56 bits. The functions in PHP are reviewed to consider whether there is any room for improvement in performance.

In addition to reducing memory usage, Rasmus Lerdorf also examined the operating principle of the CPU's Cache line to understand how the program code interacts with the CPU, how the compiler compiles the program code under the new CPU architecture, and other details to ensure The program code of PHP 7 conforms to the architecture of modern CPUs. Although the optimization of each project contributes less than 0.5% to performance, because there are many optimization projects, or a certain improved function is repeatedly called by the application, the overall effect of the correction can make such a big progress.

Inspired by HHVM, we decided to build PHP with both performance and functionality

In order to optimize PHP operation, Facebook created the virtual machine HHVM with JIT compilation. Although HHVM has fast execution performance, its design optimized for specific purposes can only satisfy a small number of developers. On the contrary, in addition to improving the performance of PHP, Rasmus Lerdorf also wants to meet the needs of high-end users and amateur users at the same time, making PHP 7 a programming language with both performance and general functions.

However, it is not difficult to develop a programming language that meets the needs of a small number of people in the market. However, the PHP project aims at many targets and must meet the needs of amateur users and professional developers at the same time. It is difficult to develop everything because There will always be some groups whose needs cannot be met. "It's like spraying a large area with a water pipe. Everyone's clothes will be a little wet, but no one's clothes will be completely soaked." Rasmus Lerdorf said.

The computing performance of PHP that does not use external frameworks is very good. However, due to the influence of external frameworks, PHP, which could originally process thousands of web page requests in a few seconds, has a significant performance drop and can only process dozens of web pages. Require. Rasmus Lerdorf said that before the emergence of HHVM, users were more concerned about whether PHP could reduce the difficulty of web development than the performance requirements of PHP, and these frameworks can make developers' work easier. However, after Facebook launched HHVM, it attracted many users who valued PHP performance, making Rasmus Lerdorf realize that many users have performance needs. He began to think about how to integrate HHVM's JIT architecture with PHP.

But Rasmus Lerdorf said that PHP and HHVM are quite different in architectural design. For example, HHVM's multi-threaded architecture is not very stable. In addition, the portability of HHVM is not good, and it is still a long way from being able to run on the Windows platform. PHP has many developers developing in the Windows environment, and HHVM cannot take care of those users.

Rasmus Lerdorf said that he could not give up the main architecture of PHP. Although they had considered integrating the two, HHVM had many limitations in its use. Although HHVM is a very good tool for Facebook and many developers, for PHP projects, the scope of use of HHVM is not broad enough and can only meet the needs of specific projects such as Facebook or Wikipedia.

For PHP, which is not a strongly typed language, it is even more difficult to import JIT

However, adding JIT compilation to PHP is very difficult. Rasmus Lerdorf said that JIT must learn to identify the operating patterns (Patterns) of the program, such as understanding which parts are important program codes, and predicting when the program will be called or which parts of the program will be called before the program is run.

Rasmus Lerdorf said that in many cars, JIT must be able to predict which part of the car will turn right, which part of the car will turn left, or which cars of certain colors will go straight. "And JIT must predict all of them." Correct, otherwise the performance will be greatly reduced." However, if the prediction is correct, the program execution performance will be greatly improved.

It is not easy to add JIT to the compilation of general programming languages. Rasmus Lerdorf said that it is even more difficult to add JIT due to the dynamic nature of PHP. For example, he said that the developer declared the value of parameter $a to be 1, but it does not mean that the value of all $a in the program is 1, because the parameter value can be easily redefined in PHP. In C language, when the developer declares parameter a to be an integer, then a will always be an integer. If anywhere in the program declares that a is a type other than integer, even compilation will not be possible. And because C language is a strongly typed programming language, "JIT can predict that variable a is an integer, but in PHP, we don't have this luxury." He explained that HHVM's approach is that when JIT learns that a is an integer type After that, it is assumed that a is always an integer.

In order to use JIT compilation, HHVM has limited the development of PHP to some extent. Users of HHVM must clearly declare the nature of variables, but developers using PHP can first declare a category (Class) without nature, and then specify the variable attributes of the class. "Without any restrictions, adding JIT to PHP is what we have to do." He said that PHP must take into account developers of WordPress, Drupal and other frameworks, and cannot arbitrarily stop supporting these frameworks. Therefore, compared with HHVM, PHP has more restrictions on creating JIT.

However, "This does not mean that we cannot do JIT. In addition, we must also control the development direction of PHP." Rasmus Lerdorf said.

Currently, Dmitry Stogov, one of the core contributors to PHP, is developing a prototype JIT and using some experimental applications to test operation. Rasmus Lerdorf said that if this JIT is used to perform certain repetitive operations or loop programs, the performance of PHP 7 can be made 10 times faster.

However, he also admitted that when this experimental JIT was used in WordPress, it did not achieve any acceleration effect. "The JIT we want to create is not something learned in college textbooks, but something that can be learned in college textbooks." JIT working in the real world," he said. Because PHP has always held such ideals, trying to solve problems in people's lives and operating in real-world online environments, not just theories in textbooks.

Rasmus Lerdorf said that when PHP first came out, he spent at least 16 hours a day developing PHP. But now he has gradually reduced his investment in development, and instead devoted his energy to publicity and speeches around the world. He jokingly said: "Instead of developing it myself, it is better to inspire people who are far smarter than me and willing to spend eighteen hours a day to write programs to develop PHP." He said that his ultimate goal is that there is not a single line in PHP written by him. Programming code, "Any growing project should not be led by one developer. Old program code should be replaced with new program code." And he believes that other developers should aim for this.

Recommended tutorial: "php video tutorial"

The above is the detailed content of The key to doubling PHP7 performance revealed. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

How performant are PHP functions? How performant are PHP functions? Apr 18, 2024 pm 06:45 PM

The performance of different PHP functions is crucial to application efficiency. Functions with better performance include echo and print, while functions such as str_replace, array_merge, and file_get_contents have slower performance. For example, the str_replace function is used to replace strings and has moderate performance, while the sprintf function is used to format strings. Performance analysis shows that it only takes 0.05 milliseconds to execute one example, proving that the function performs well. Therefore, using functions wisely can lead to faster and more efficient applications.

What is the performance impact of converting PHP arrays to objects? What is the performance impact of converting PHP arrays to objects? Apr 30, 2024 am 08:39 AM

In PHP, the conversion of arrays to objects will have an impact on performance, mainly affected by factors such as array size, complexity, object class, etc. To optimize performance, consider using custom iterators, avoiding unnecessary conversions, batch converting arrays, and other techniques.

How to use benchmarks to evaluate the performance of Java functions? How to use benchmarks to evaluate the performance of Java functions? Apr 19, 2024 pm 10:18 PM

A way to benchmark the performance of Java functions is to use the Java Microbenchmark Suite (JMH). Specific steps include: Adding JMH dependencies to the project. Create a new Java class and annotate it with @State to represent the benchmark method. Write the benchmark method in the class and annotate it with @Benchmark. Run the benchmark using the JMH command line tool.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

See all articles