Home > Backend Development > PHP Tutorial > Detailed explanation of the key to doubling the performance of PHP7

Detailed explanation of the key to doubling the performance of PHP7

*文
Release: 2023-03-19 09:56:01
Original
1781 people have browsed it

This article mainly introduces relevant information to help you understand the key to doubling the performance of PHP7. Friends in need can refer to it. I hope to be helpful.

PHP 7 is the first major revision in ten years. The biggest feature is a major breakthrough in performance. It can be twice as fast as the previous version of PHP 5. Rasmus Lerdorf, the father of PHP, said that it is even faster than HHVM virtualization. The performance of PHP programs under the computer is faster.

HHVM is a PHP optimization mechanism customized by Facebook for the characteristics of its own website, and may not be applicable to any website. But Rasmus Lerdorf said that one of the goals of the new version is to enable any website developer, even when using the development framework Drupal and the open source e-commerce system Opencart, to have the same performance as using HHVM technology. On the eve of the release of the new version, he also took the opportunity to come to Taiwan to attend the PHPConf Taiwan annual conference to share the key to the performance breakthrough of PHP 7.

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 it is not as easy to find as most new projects. There is 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 without the use of external frameworks is very good. However, due to the influence of external frameworks, the performance of PHP, which could originally process thousands of web page requests in a few seconds, has dropped significantly and it can only process Dozens of requests. 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.

Related recommendations:

Detailed explanation of how PHP implements operable verification codes

Detailed explanation of how PHP implements file search

Detailed explanation of how PHP jumps to the corresponding web page according to the user language

The above is the detailed content of Detailed explanation of the key to doubling the performance of PHP7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template