Home  >  Article  >  Backend Development  >  The key to doubling PHP7 performance revealed

The key to doubling PHP7 performance revealed

coldplay.xixi
coldplay.xixiforward
2020-06-24 17:31:582481browse

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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete