PHP, a 20-year-old veteran web programming language, 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 faster than the previous version of PHP 5. Double the time, Rasmus Lerdorf, the father of PHP, said that it can even perform faster than the PHP program under the HHVM virtual machine.
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.
For a mature language that has gone through many revisions and countless optimizations in the past 20 years, it is not easy to have a breakthrough that doubles the performance. Rasmus Lerdorf said frankly that unlike ordinary new projects, it is often 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 that PHP 7 The program code 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 be such a big improvement.
Inspired by HHVM, I decided to build PHP with both performance and functionality
In order to optimize the operation of PHP, Facebook created a 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. Development is difficult to cover everything because there will always be The needs of some groups 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, 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 cannot give up the main architecture of PHP. Although they have considered integrating the two, HHVM has many limitations in 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.
PHP, which is not a strongly typed language, is even more difficult to import into 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 correctly, otherwise Performance will be greatly reduced. "However, if the prediction is correct, 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 restricted 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 the 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 used in the real world." Operational JIT," 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.