Home  >  Article  >  Backend Development  >  Detailed introduction to the performance of PHP7 and HHVM (picture and text)

Detailed introduction to the performance of PHP7 and HHVM (picture and text)

黄舟
黄舟Original
2017-03-24 10:07:524439browse

Changes in the ranking of PHP language

According to the "TIOBEProgramming Language Ranking" (Although the list has statistical methods limitations, but it is still a good reference). In 2010, PHP ranked third among the world's programming languages. It can be seen that the PHP language is the most powerful language in the Web field in the PC Internet era.        

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者

##There was a joke among PHP programmers:

A certain woman: Can you make this forum possible? If everyone starts making noise, I will have dinner with you. ​ ​ PHP Programmer: PHP is the best language in the world!
, all kinds of quarrels ...
: :: Take you, let's go!
PHP Programmer: Not today, I must convince them that PHP must be the best language.

Okay, let’s get down to business. The language itself is not good or bad, it just solves different problems in the scenarios in which it is used. The Internet era is moving very fast. With the arrival of mobile Internet, in just a short time In more than four years, mobile technology development has swept the world. At the same time, various languages ​​​​are emerging, and the once glorious PHP has dropped to sixth place from the original programming language list (December 2014 list one). As a result, voices that bad-mouthed PHP emerged one after another.

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者
       

However, Brother Niao (Hui Xinchen, one of the PHP language developers) shared a data at Qcon in 2014. Among the top 1 million websites in the world, 81.3% make The web server scripting language used is PHP, which was 78.3% in the same period of 2013. In other words, PHP has not decreased in terms of Web services, but has increased a lot in the mobile Internet wave. The application of other language technologies is thus diluted.

Recently, the performance comparison between PHP7 and HHVM has become a hot and controversial topic. Everyone is discussing and paying attention to which one is the future of PHP performance improvement.

The origin of HHVM (HipHop Virtual Machine)

HHVM is an open source PHP virtual machine that uses JIT compilation and other technologies. Let the execution performance of PHP code be greatly improved. It is rumored that the execution performance of the current version of native PHP code can be improved by 5-10 times.

HHVM originated from Facebook. Many of Facebook’s early codes were developed using PHP. However, with the rapid development of business, PHP implemented Operational efficiency has become an increasingly obvious issue. In order to optimize execution efficiency, Facebook began to use HipHop in 2008, which is a PHP execution engine. It was originally designed to Convert a large amount of Facebook PHP code into                             C++ to improve performance and save resources. The performance of PHP code using HipHop is improved several times. Later, Facebook open sourced the HipHop platform and gradually developed it into the current HHVM.

1. Why is PHP slow?        

PHP is slower than C/C++-level languages. In fact, the PHP language was not originally designed to solve computing-intensive application scenarios. We can roughly understand that PHP sacrifices execution efficiency in order to improve development efficiency.

We know that a big feature of PHP is the weak type feature. In other words, I can define a variable at will, and then assign it to various values ​​at will. types of data. Taking an int integer number as an example, in C language:

int num = 200;//通常是4字节

However, if PHP defines the same variable, the actual corresponding storage structure Then it is:


##This structure will occupy far more than C variables Much memory is defined in PHP as follows:

$a = 200;//这变量将实际占用对比C变量很多倍的存储空间。

In fact, for PHP, no matter what type of data is stored, it is implemented using the above-mentioned "kill" structure. In order to be compatible with PHP programmers' variable type "intrusion", PHP does It's friendly to developers, but cruel to the execution engine. The memory consumption of a single variable may not be obvious yet. Once PHP's array, etc. are used, the complexity increases exponentially (the implementation of the array is HashTable). Then, when the Zend engine executes, these PHP codes are compiled into opcode (PHP's intermediate bytecode, the format is somewhat similar to assembly), and the Zend engine compiles it line by line. Explanation and execution.

Whether it is the connection operation of string or the simple modification of the array, etc., almost all of them are "a word from a PHP programmer, and the Zend engine will break down" Rhythm. Therefore, for the same operation, PHP consumes more system resources such as CPU and memory than C. In addition, there are automatic memory recycling, variable type judgment, etc., which will increase the consumption of system resources.

For example, I used the quick sort function and the native sort function implemented in pure PHP to sort 10,000 integer numbers to make a time-consuming comparison. The results are as follows:

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者

The native sort takes 3.44 ms, while our own implemented PHP functionsort takes 68.79 ms. We found that there is a huge gap in execution efficiency between the two. My way of testing is to calculate the time interval before and after the function is executed, not the time from start to end of the entire PHP script. PHP script to start and shut down too The process itself has a series of initialization and cleanup work, which also takes up a lot of time.          
       

     

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者
       

      


#        

Normally, the ranking of PHP execution efficiency is:

                   

  1. The fastest are the PHP language structures (isset, echo, etc.), which are part of the PHP language (they are not functions at all).

  2. #Then the faster ones are PHP’s native and extended functions. PHP extension, based on Zend API, implements functions in C, and its execution efficiency is of the same order of magnitude as C++/Java.

  3. What is really slow is the code and functions we write ourselves through PHP. For example, if we use a relatively heavy framework implemented in pure PHP, because the framework itself has many modules, it will obviously drag down the execution efficiency at the language level and occupy more memory. (The domestic Yaf framework is implemented in an expanded manner, so the execution efficiency is much faster than the framework written in pure PHP)

                         

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者
       

​​

Under normal circumstances, we do not recommend using PHP to implement complex logical calculation functions, especially in scenarios where the Web system traffic is relatively large. Therefore, PHP programmers should Have a relatively broad understanding of PHP's various native functions and various extensions. In specific function implementation scenarios, seek more native solutions (native interface or extensions) instead of writing one yourself. Heaps of complex PHP code to implement this type of functionality.

If you have enough PHP extension development capabilities, rewriting this type of business function as a PHP extension will also greatly improve the execution efficiency of the code. This is a very good method and is also widely used in PHP optimization. However, the shortcomings of self-written PHP business development are also obvious:

  1. The expansion development takes a long time, and it is complicated to modify when the requirements change, and it is not easy to write. It may affect the stability of web services. (For example, in Apache's worker mode, if it hangs in a multi-threaded scenario, it will affect other normal sub-threads in the same process. If it is a multi-threaded Web mode, writing extensions also needs to support threadsSafety )

  2. #Extensions may require additional compatibility work when the PHP version is upgraded.

  3. #The maintenance and takeover costs after personnel changes are also relatively high.

In fact, among first-tier Internet companies, the more common solution is not to add PHP expansion, but to use C/C++ independently Write a service server, and then PHP communicates with the service server through socket to complete business processing, without coupling PHP itself with the business.

However, most of the performance bottlenecks of Web services are in the time consuming of network transmission and other service servers (such as MySQL, etc.), the time consuming of PHP execution The overall time-consuming proportion is very small, so from a business perspective, the impact may not be obvious.

2. How HHVM improves PHP execution performance

The way HHVM improves PHP performance is Replaces the Zend engine to generate and execute PHP's intermediate bytecode (HHVM generates its own format of intermediate bytecode code), executed through JIT (Just In Time, Just-in-time compilation is a software optimization technology, which means that the bytecode is compiled into machine code at runtime) and converted into machine code for execution. The default method of the Zend engine is to compile it into opcode first, and then compile it one by one. Execution, usually each instruction corresponds to a C language level function. If we generate a large number of repeated opcodes (codes and functions written in pure PHP), Zend will execute these C codes one by one multiple times. code. What JIT does is to go one step further and compile a large number of repeatedly executed bytecodes into machine code at runtime to improve execution efficiency. Usually, the condition that triggers JIT is that the code or function is repeated calls.

     

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者
         

         

Ordinary PHP code, because the type of the variable cannot be fixed, additional logic code to determine the type needs to be added. This PHP code is not conducive to CPU execution and optimization. of. Therefore, HHVM usually needs to use PHP code with Hack writing method (additional technical code added to be compatible with certain features) to "cooperate", in order to make the variable type fixed and facilitate virtualization. Machine compiled and executed. PHP pursues to accommodate all types in one form, while Hack can mark everything accommodated with a certain type.

Example of Hack writing method of PHP code:          

           

PHP7和HHVM的性能之争介绍 - 徐汉彬Hansion - 技术行者
         

​ ​ ​

In the above example, the PHP code is mainly added with variable types. The general direction of Hack writing is to change the previous "dynamic" writing method into "static" writing method to cooperate with HHVM.

HHVM has attracted a lot of attention because of its high performance, and some first-tier Internet companies have also begun to follow suit. Judging from the pure language execution performance test results, HHVM is much ahead of the PHP7 version under development.


However, from the perspective of specific business scenarios , the gap between HHVM and PHP7 is not that big. In the results of the test scenario using the WordPress open source blog homepage, their current gap is not obvious.                                                                                                     

​ ​ ​


However, PHP7 is still under development. Judging from the available technical solutions, the current HHVM is slightly better. However, there are some problems in the deployment and application of HHVM:

Service deployment is relatively complex and has certain maintenance costs.

  1. # does not fully support PHP native code, and PHP extensions also need to be properly compatible.

  2. #HHVM is a new virtual machine and may cause memory leaks when running for a long time. (It is said that when first-tier Internet companies apply this technology, they solve memory leaks by patching themselves)

  3. HHVM is, after all, For a relatively new open source project, it still takes some time to mature.

    Performance Innovation of PHP7

The performance issues that PHP has long been criticized for will be greatly improved in this version. There is no PHP6 in the middle of the version. It is said that this version has a project, and later most of the functions were implemented in the 5.x version. In order to avoid confusion, the next major version will be PHP7 directly. (A few years ago, I also read books about PHP6.)

1. Introduction to PHP7

         

Although the official version of PHP7 may not be released until October 2015, a test version should be available in June next year, followed by 3-4 months of quality assurance. The project plan of the PHP community is as follows:

               


##            

Because the project is still under development, from the table, the feature descriptions that can be seen are relatively Vague. There are definitely more other features, they just haven't been announced yet. The following are from the PHP community. Because PHP7 is a project under development, the following may not be accurate, but it does not prevent us from taking a look.

  1. PHPNG (PHP next generation, next generation PHP), various performance optimizations for the Zend execution engine itself, including JIT, may be implemented in Zend                                                           In the Opcache component.

  2. AST (Abstract Syntax Tree) aims to introduce a middleware into the PHP compilation process to replace the way of spitting out opcode directly from the interpreter. Decoupling the interpreter and compiler can reduce a lot of Hack code, and at the same time, make the implementation easier to understand and maintain.

  3. uniform variable syntax introduces an internally consistent and complete variable syntax, allowing PHP's parser to more fully support various types Variables. The usage of some variables needs to be adjusted, such as variable $$a, etc.

  4. Support integer semantics (integer semantics), such as NaN, Infinity, 071af19a55f4da1989e8c02b755ba052>, correct the consistency of list(), etc. wait.

# Among the above features, the most anticipated is the performance optimization of PHPng. The PHP community has released some performance speed test data. From the data point of view, PHPng The execution performance has been nearly doubled compared to the beginning of the project. This result is already very good. Moreover, the most important thing is that there are still many optimization plans for PHP7 that have not yet been completed. Wait until everything is completed, I believe we can see a higher performance PHP7.

This speed test data comes from the PHP community (wiki.php.net/phpng), and part of the data is intercepted:


                                                                                                                                                                                                                                                                                                  

         
     

Simple translation:

                            ​ ​ ​

The comprehensive test speed is increased by 35%.

    In actual application scenarios, there is a 20%-70% speed increase (the WordPress homepage has a 60% improvement)
  • Less memory consumption
  • Supports most commonly used SAPIs
  • Support most PHP extensions bound to resource allocation (69 completed, 6 to be migrated)
  • Provides comparable to HHVM3.3.0 Execution speed
  •                                        
  • #2. PHP’s Weak Type Controversy
         

         

PHP has many controversial features, but with the release and improvement of the language version, criticism of functions and features has begun to diminish. However, PHP's "weak type" feature has obviously been more controversial. From the fact that HHVM directly "removed" the "weak type" feature through Hack, it can be seen that HHVM does not like the "weak type" feature. However, in many of our In the eyes of PHP programmers, this is one of the important advantages of PHP. Variables in PHP are designed to be casual and elegant, embracing everything. Doesn't it make the language simpler?

​ ​ ​

In fact, some people think it is a serious problem. The criticism of "weak typing" is roughly as follows:

                            ​ ​ ​

  1. In a "rigorous" language, the type of a variable is usually predefined. From beginning to end, the type of the variable is fixed, and the scope of use is also fixed. As for PHP variables, usually we can only see its name, and most types cannot be predefined, and can be changed at will. (Memory allocation is not easy to manage)

  2. In order to be compatible with weak type features, PHP needs to implement a large amount of compatible code, including type judgment and type conversion , storage methods, etc., increase the internal complexity of the language. (Low execution efficiency)

  3. The type of the variable is uncontrollable. There are a large number of "implicit type conversions" during the execution process, which can easily lead to unpredictable result. (It does need to be emphasized here that PHP type conversion is a point that must be mastered. Converting various types to each other may cause many problems, especially for students who are new to PHP)

                            ​ ​ ​

They believe that these are not in line with the simplicity of "what you see is what you get", and that languages ​​with strict grammar are more efficient and easier to "understand".

​ ​ ​

Javascript and other languages ​​have also been similarly criticized, because their performance on this issue is the same. However, for a language to eventually be used on a large scale, it must There are reasons for them. PHP has become the preferred scripting language for Web service development, and Javascript has directly dominated the Web front-end field. It cannot be an accident that it has reached this point. Developers use The feet voted for them. Programming language is a bridge between humans and machines, and the ultimate pursuit is to achieve the grand goal of “everyone can program”.

​ ​ ​

Looking at the history of language development, starting from the machine code of 0 and 1, to assembly language, then to C language, and then to the dynamic scripting language PHP. The execution efficiency decreases exponentially, but the learning threshold also decreases exponentially. The PHP language not only shields the complexity of C's memory management and pointers, but also further shields the complexity of variable types. It improves the efficiency of project development and lowers the threshold for learning, but at the same time sacrifices a certain amount of execution performance. Then, HHVM's Hack gives us a "return to primitive" feeling, reintroducing the complexity of variables. Of course, different languages ​​solve problems in different scenarios and cannot be generalized.

           


         

​ ​ ​

##Summary

HHVM’s performance improvements to PHP are impressive, and the hard-working PHP7 is highly anticipated. Both are excellent open source projects and both are constantly improving Moving forward and developing. For now, because it is still a long time before the official version of PHP7 is released, the current choice of performance optimization solution is of course HHVM. However, personally, I prefer I am optimistic about PHP7 because it is more backward compatible with PHP code. If there is not much difference in performance between the two, I will choose the simpler one.

The above is the detailed content of Detailed introduction to the performance of PHP7 and HHVM (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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