Show you the key to doubling PHP 7 Performance

Source: Internet
Author: User
Tags hhvm drupal
This article mainly introduces the key information about doubling the performance of PHP 7. For more information, see

This article mainly introduces the key information about doubling the performance of PHP 7. For more information, see

The 20-year-old web application language PHP will be released at the end of October. This is the first major version in the past decade. Its biggest characteristic is its performance breakthrough, it is twice faster than the previous version of PHP 5. Rasmus Lerdorf, the father of PHP, says it can even provide faster performance than PHP programs under HHVM.

HHVM is a PHP optimization mechanism tailored by Facebook for its own website features, and is not necessarily 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 open-source e-commerce system Opencart, to have the performance without losing the HHVM technology. On the eve of the release of the new version, he also shared the key to a major breakthrough in PHP 7 performance when coming to the PHPConf Taiwan annual meeting.

A mature language that has undergone many revisions and optimizations over the past 20 years, and a breakthrough that doubles its performance is not easy, Rasmus Lerdorf said frankly, unlike general new projects, it is easy to find a lot of room for improvement. The new version of PHP has achieved such results without modifying some programs. On the contrary, PHP 7 can achieve the execution performance without losing HHVM after a lot of detail optimization and performance accumulation.

Rasmus Lerdorf and the PHP core contribution team spent a lot of effort to reduce the number of memory locations that were moved during program operation, thus accelerating execution performance. For example, the data architecture zval for storing variables in PHP is reduced from 24 bits to 16 bits, Hashtable is reduced from 72 bits to 56 bits, and PHP's function is viewed, think about whether there is any space to improve performance.

In addition to reducing memory usage, Rasmus Lerdorf further examines the operating principles of the CPU Cache line, understand how the program code interacts with the CPU, how to compile the program code under the new CPU architecture, and other details to ensure that the PHP 7 program code conforms to the modern CPU architecture. Although the optimization of each project contributes less than 0.5% to performance, many optimization projects or an improved function will be repeatedly called by the application, the overall improvement of the overall rectification results can be achieved.

Stimulated by HHVM, PHP with both performance and functionality is decided

In order to optimize PHP operations, Facebook combined with JIT compilation and translation to create a virtual machine HHVM. While HHVM has fast execution performance, it is designed for specific purposes and can only meet the needs of a small number of developers. On the contrary, Rasmus Lerdorf not only wants to improve the performance of PHP, but also needs to meet the needs of high-end users and amateur users, so that PHP 7 can be a programming language with both performance and general functions.

However, it is not difficult to develop a program language that meets the needs of a few people on the market, but the PHP project targets many objects and must meet the needs of both amateur users and professional developers, development is hard to cover, because the needs of some ethnic groups are always unable to be met. "This is like spraying water pipes in a wide range, and everyone's clothes will be wet, but no one's clothes are completely wet. 」 Rasmus Lerdorf analogy.

PHP without a plug-in framework has excellent computing performance. However, due to the impact of the external framework, PHP that can handle thousands of web pages in a few seconds has greatly reduced its performance, it can only process dozens of requirements. Rasmus Lerdorf said that before HHVM appeared, users were concerned about whether PHP could reduce the difficulty of web development compared to the requirements on PHP performance, and these frameworks made it easier for developers to work. However, after Facebook launched HHVM, many users focusing on PHP performance were introduced, making Rasmus Lerdorf aware of the performance requirements of many users. 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 architecture design. For example, the multi-threaded architecture of HHVM is not very stable. In addition, the portability of HHVM is not good, and there is still a long way to operate on the Windows platform, while many PHP developers develop in the Windows environment, and HHVM cannot take care of those users.

Rasmus Lerdorf says he cannot abandon the main PHP architecture. Although they have considered integrating the two, there are many restrictions on the use of HHVM. Although HHVM is a good tool for Facebook and many developers, HHVM is not widely used for PHP projects and can only meet the needs of specific projects such as Facebook or Wikipedia.

For non-strong language PHP, it is difficult to import JIT

However, it is very difficult to add JIT compilation in PHP. Rasmus Lerdorf indicates that JIT must learn to identify the Patterns of a program, for example, to understand which parts are important program code and to predict the time when the program is called before it operates, or what part of the program will call.

Rasmus Lerdorf: 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 some colors the car will go straight, "JIT must make all predictions correctly, otherwise the performance will be greatly reduced. 」 However, if the prediction is correct, the program execution performance will be greatly improved.

Adding JIT to compilation in general programming languages is not easy. Rasmus Lerdorf indicates that it is difficult to add JIT due to the dynamic attributes of PHP (dynamic. For example, the developer declares that the value of $ a is 1, but it does not mean that all $ a values in the program are 1, because the parameter values in PHP can be easily redefined. In C, when the developer declares that parameter a is an integer, a is always an integer. If a is of a type other than an integer, compilation cannot be performed. Because the C language is a strong programming language, "JIT can predict that variable a is an integer, but we do not have this luxury in PHP .」 He explained that the practice of HHVM is that when JIT learns that a is an integer type, it is assumed that a is always an integer.

In order to use JIT compilation, HHVM restricts the development of PHP to some extent. HHVM users must clearly declare the nature of the variables. However, PHP developers can first declare non-Nature classes, and then specify the variable attributes of the classes. 「 Without any restrictions, adding JIT to PHP is what we need to do .」 He said that PHP must take into account the developers of Wordpress, Drupal and other frameworks and cannot stop supporting these frameworks at will. Therefore, PHP imposes more restrictions on JIT creation than HHVM.

However, "This does not mean we cannot do JIT. In addition, we also need to control the development trend of PHP .」 Rasmus Lerdorf.

Currently, Dmitry Stogov, one of the core contributors to PHP, develops a prototype JIT and uses some experimental applications to test its operation. Rasmus Lerdorf indicates that if the JIT is used to execute some repetitive operations or loop programs, PHP 7 performance will be faster than 10 times.

However, he admitted that this experimental JIT was not accelerated when it was used for Wordpress. "The JIT we want to build is not what we want to learn in college textbooks, it is the JIT that can operate in the real world. 」 He said. Because PHP has always held such an ideal, trying to solve problems in people's lives, and operating in real-world online environments, not just theories in textbooks.

Rasmus Lerdorf said he spent at least 16 hours developing PHP every day when PHP was just introduced. However, he has gradually reduced his investment in development and switched to focusing on Publicizing and giving speeches around the world. He joked: "It is better to motivate people who are far more intelligent than me and willing to spend 18 hours a day writing programs to develop PHP .」 He said that his final goal is that PHP does not have any line of sequential code written by him. "No growing project should be dominated by a developer, old program code should be replaced by new program code. 」 He believes that other developers should take this as their goal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.