Detailed description of PHP7 and HHVM performance (graphic)

Source: Internet
Author: User
Tags hhvm integer numbers types of functions

Changes in the ranking of PHP languages

According to the "Tiobe programming language Rankings" (although the list is limited, but still a good reference), 2010 PHP was the highest in the World programming language ranked third. It can be seen that the PHP language in the PC Internet era of the web domain is masterful, Dynasky one column.


in PHP programmers, there was a joke:

A woman: You can make this forum people fight up, I will eat with you.
PHP Programmer: PHP is the best language in the world!
A forum fryer, all kinds of quarrel ...
A woman: Take you, let's go!
PHP Programmer: Not today, I have to convince them that PHP must be the best language.

Well, we got to the point where the language itself is not good or bad, but that it solves different problems in its own scenario. The era of the Internet wheel is fast, with the advent of mobile internet, in just over four years, mobile technology development sweeping the world. At the same time, a variety of languages, and the former brilliant PHP from the original programming language list, down to the sixth place (December 2014 list). As a result, the sound of a bad PHP.


However, Xinchen, one of the PHP language developers, has one data in the 2014 Qcon share, the world's top 1 million web site, 81.3% using the Web server-side scripting language is php,2013 year-over-period is 78.3%. In other words, PHP does not reduce the Web services, but in the wave of mobile internet, adding a lot of other language technology applications, and then diluted.

Recent performance comparisons with PHP7 and HHVM have become a hot topic of contention, and everyone is discussing and focusing on which is the future of PHP's performance improvement.

The origins of HHVM (HipHop Virtual machine)

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

HHVM originated from Facebook, and many of Facebook's early-morning code was developed using PHP, but with the rapid development of the business, the efficiency of PHP execution became more and more obvious. To optimize execution efficiency, Facebook started using hiphop in 2008, a PHP execution engine originally designed to turn Fackbook's massive PHP code into C + + to improve performance and conserve resources. PHP code that uses hiphop has several times the performance boost. Later, Facebook will open up the hiphop platform and evolve into the current HHVM.

1. Why is PHP slow?

The slowness of PHP is relative to the C + + level language, in fact, the original design of the PHP language is not used to solve computational-intensive scenarios. We can roughly understand that PHP has sacrificed execution efficiency in order to improve development efficiency.

We know that PHP is a big feature of the weak type feature, that is, I can arbitrarily define a variable, and then give it arbitrarily assigned to various types of data. Take an int integer number as an example, in the C language:

int num = 200;//is typically 4 bytes

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


This structure will occupy much more memory than the C variable, which is defined in PHP as follows:

$a = 200;//This variable will actually occupy many times the storage space compared to the C variable.

In fact, for PHP, no matter what type of data stored, are used in the above "kill" the structure of the implementation. In order to be compatible with the PHP Programmer's Variable type "random", PHP is a developer friendly, but the execution of the engine is very cruel. The memory consumption of a single variable may not be obvious, and once the array of PHP is used, the complexity index goes up (the implementation of the array is hashtable). Then, when the Zend engine executes, the PHP code is compiled into opcode (PHP's intermediate bytecode, which is a bit like a compilation), which is performed by the Zend engine line by row.

Whether it is a string connection operation, or an array of simple modifications, and so on, almost all are "PHP programmer a word, Zend engine run broken leg" rhythm. Therefore, the same operation, compared to C, PHP consumes more CPU and memory system resources. In addition, there are automatic memory recovery, variable type judgment and so on, will increase the consumption of system resources.

For example, I use a pure PHP implementation of the fast sorting function and the native sort function, sorting 10,000 integer numbers, to do a time-consuming comparison, the results are as follows:


Native sort time 3.44 ms, and our own PHP function sort is 68.79 Ms. We find that the efficiency gap between the two is huge. My test method is to calculate the time interval before and after the function is executed, not the entire PHP script from the start to the end of the time. PHP script startup and shutdown process, itself has a series of initialization and cleanup work, will also occupy a lot of time.



Typically, the ranking of PHP execution efficiency is:

    1. The quickest is the PHP language structure (isset, ECHO, etc.), part of the PHP language (they are not functions at all).

    2. Then faster is the PHP native and extension functions. PHP extension, based on the Zend API, with C implementation of the function, execution efficiency and C++/java belong to the same number of orders.

    3. The real slow is the code and functions we write ourselves through PHP. For example, if we use a relatively heavy pure PHP implementation of the framework, because the framework itself is a lot of modules, it will significantly drag the language level of execution efficiency, while occupying more memory. (Domestic YAF framework, implemented in an expanded way, so execution is much faster than a purely PHP-written framework)


In general, we do not recommend the use of PHP to implement logical complex computing types of functions, especially the web system traffic is relatively large scenario. Therefore, PHP programmers should have a broad understanding of PHP's various native functions and various types of development, in the specific functional implementation scenario, to seek a more native solution (native interface or extension), rather than write a bunch of complex PHP code to implement this type of functionality.

If you have enough PHP to expand your development capabilities, rewriting this type of business function as a PHP extension will also significantly increase the efficiency of your code execution. This is a very good way to also be widely used in PHP optimization. However, the shortcomings of the PHP business development that you write are also obvious:

    1. It takes a long time to expand and develop, and changes in requirements are complicated and poorly written may affect Web service stability. (for example, in the Apache worker mode, a multithreaded scenario hangs up and affects other normal sub-threads under the same process.) If you are multithreaded Web mode, writing extensions also requires thread-safe support.

    2. Expanding in the PHP version of the upgrade, you may need to do additional compatibility work.

    3. the cost of maintenance and takeover after the change of personnel is also higher.

In fact, in the Internet front-line enterprise, the more common solution is not to increase the PHP extension, and write a service server in C + +, and then PHP through the socket and service server communication to complete business processing, not the PHP itself and the business coupling.

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

2. HHVM ways to improve PHP execution performance

HHVM the way to improve the performance of PHP, by replacing the Zend engine to generate and execute PHP Intermediate bytecode (HHVM generate its own format of intermediate bytecode), execution through the JIT (Just in time, instant compilation is a software optimization technology, Refers to the machine code to be compiled at run time. The default practice of the Zend Engine is to compile the opcode first and then execute it one after the other, usually each instruction corresponds to a C-level function. If we produce a lot of duplicate opcode (pure PHP code and functions), the corresponding is Zend to execute the C code several times. The JIT does is to further, the large number of repeated execution of bytecode at run time to compile into machine code, to improve execution efficiency. Typically, the condition that triggers the JIT is that the code or function is repeatedly called repeatedly.


Normal PHP code, because the type of the variable cannot be fixed, need to add additional logic code to determine the type, so that PHP code is not conducive to CPU execution and optimization. Therefore, HHVM usually need to use the hack code (in order to be compatible with a particular feature and additional skills of the nature of the code) to "match," is to let the variable type fixed, convenient for virtual machine compilation execution. PHP seeks to accommodate all types in one form, while hack can mark the type of everything that is to be accommodated.

Examples of PHP code hack notation:


In the above example, the PHP code is mainly added to the variable type. Hack the overall direction of writing, is to change the previous "dynamic" to the "static" writing, to cooperate with the HHVM.

HHVM has attracted a lot of attention because of its high performance, and some first-line internet companies have started to follow suit. From pure language performance test results, HHVM lead the development of the PHP7 version of a lot.


However, from the specific business scenario, HHVM and PHP7 gap is not so big, to WordPress open Source Blog Home for the test scenario results, their current gap is not obvious.


However, PHP7 is currently in development, in terms of the available technical solutions, the current HHVM notch above. However, there are some problems with the deployment and application of HHVM:

    1. Service deployment is complex and has a certain maintenance cost.

    2. PHP native code is not fully supported and PHP extensions need to be properly compatible.

    3. HHVM is a new virtual machine that has a memory leak for a long time run. (it is said that the first-line internet companies in the application of this technology, through their own patch to solve the memory leak)

After all, HHVM is a relatively new open source project, and it still takes some time to grow to maturity.

PHP7 's performance innovation

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

1. Introduction of PHP7

Although the official version of PHP7 may not be released until October in 2015, a beta version should be visible next June, followed by a quality assurance of 3-4 months.

the project plan for the PHP community is as follows:


Because the project is still in development, the descriptions of the features that can be seen from the table are blurred. There must be more of the other features, just not yet announced. These are seen from the PHP community, because PHP7 is a project in development, and these are not necessarily accurate, but it does not prevent us from looking together.

    1. Phpng (php Next generation, Next generation PHP), performs various performance optimizations on the Zend execution engine itself, where JIT may be implemented in Zend Opcache components.

    2. The AST (abstract Syntax tree), which is designed to introduce a middleware in the PHP compilation process, instead of spitting out opcode directly from the interpreter. Decoupling the interpreter and compiler can reduce the amount of hack code, while making the implementation easier to understand and maintain.

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

    4. Supports integer semantics (integer semantics) such as Nan, Infinity, <<, >>, fixed list () consistency, and so on.

Among the above features, the most anticipated is Phpng performance optimization, the PHP community has released some performance of the speed data. From the data, the performance of Phpng is nearly 1 time times higher than the start of the project. This is a very good result, and the most important thing is that PHP7 's optimization plan is still a lot of unfinished. When all is done, I believe we can see a higher performance PHP7.

The velocity data is from the PHP community (wiki.php.net/phpng), which intercepts a subset of the data:


For its current PHP5.6 version, Phpng's October performance improvement has become apparent:


Under Simple translation:

    • Comprehensive test speed increased by 35%.

    • In the actual application scenario has 20%-70% speed (WordPress home page has 60% of the Ascension)

    • Less memory consumption

    • Support for most common Sapis

    • Support for most PHP extensions bound to resource allocation (69 complete, 6 to be migrated)

    • Provides execution speed comparable to HHVM3.3.0

2. PHP's weak type controversy

PHP is controversial in many ways, but with the release and refinement of language versions, criticism of functionality and features has begun to grow less. However, PHP's "weak type" characteristics, but obviously more controversial, from HHVM through the hack way directly "remove" the "weak type" feature can be seen, hhvm do not like the "weak type" feature. However, in the eyes of many of our PHP programmers, this is one of the important advantages of PHP. PHP variables are designed with the nature and elegance, the Sea of the rivers, all can be tolerated, not to make the language more simple?

In fact, some people think it is a serious problem, and the criticisms of "weak type" are as follows:

    1. In a "rigorous" language, the type of the good one variable is usually pre-defined, from start to finish, the type of the variable is fixed and the scope of use is fixed. and PHP variables, usually we can only see its name, the type of most of them can not be pre-defined, and can be arbitrarily changed. (Poor memory allocation management)

    2. To be compatible with weakly typed features, PHP needs to implement a lot of compatible code, including type judgments, type conversions, storage methods, and so on, increasing the complexity within the language. (Inefficient execution)

    3. The type of the variable is not controllable and there is a large number of "implicit type conversions" in the execution process, which can produce unpredictable results easily. (It really needs to be stressed here, PHP type conversion is a must-know point, the various types of mutual conversion may cause a lot of problems, especially beginners of PHP classmate ha)

These, they argue, are not in line with the simplicity of what is WYSIWYG, and the language is more efficient and easier to understand.

There are similar criticisms of languages such as JavaScript, because it behaves the same on this issue. However, a language will eventually be used in large-scale, there must be their truth. PHP has become the preferred scripting language for Web services development, and JavaScript is directly dominating the Web front-end, and it is unlikely that the developers will vote for them by foot. Programming language is the bridge between human and machine communication, and the ultimate pursuit is the grand goal of realizing "everyone can be programmed".

Throughout the history of language development, from 0 and 1 of machine code, to assembly language, then to C, then to the Dynamic scripting language PHP. The efficiency of execution decreased exponentially, but the learning threshold also decreased exponentially. The PHP language not only masks the complexity of C's memory management and pointers, but also shields the complexity of variable types further. Improve the efficiency of project development, reduce the threshold of learning, but at the same time sacrificing a certain degree of execution performance. Then, HHVM's hack gives us a feeling of "returning to the original" and re-introducing the complexity of the variable. Of course, different languages solve the problems in different situations, and can not generalize.


Summary

hhvm the performance of PHP to improve, let a person in front of the light, and sharpening PHP7 is extremely looking forward to. Both are extremely good open source projects, are constantly moving forward and development. For now, since the release from PHP7 is still a relatively long time, the choice of the current performance optimization scheme is, of course, HHVM. However, as far as I am concerned, I am more optimistic about PHP7, because it is more capable of backwards compatibility of PHP code. If the performance difference is not small, I will choose the simple one.

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.