PHP Classes
elePHPant
Icontem

PHP 7 Performance Improvements May Increase if You Optimize it to Your Application using to PGO - PHP Classes blog

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 7 Performance Imp...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  

Author: Manuel Lemos

Posted on:

Categories: PHP Tutorials, PHP Performance

One of the greatest improvements of PHP 7 is the increase performance of the code in general aspects.

However, a little known fact is that PHP 7 performance can be increased further by optimize itself for specific applications using Profile-Guided Optimization (PGO) support.

In a recent article Intel researchers announced that PHP 7 built with PGO support may run WordPress about 7% or 8% faster when compared to PHP 7 built the using default way.

Read this article to learn more about PGO and how you can benefit from this optimization to make PHP 7 run even faster than it already runs.




Contents

Introduction to PGO

Intel Announces PGO based Performance Improvements for PHP

How to Benefit from PGO Performance Improvements?

Intel CPU Specific Optimization?

PHP for Windows Already Supported PGO Since PHP 5.4

Conclusion


Introduction to PGO

PGO stands for Profile-Guided Optimization. It is compiler optimization technique that uses profiling to improve the performance of a program.

In our case the "program" we are talking about, it is PHP. I do not mean the code of your PHP scripts, but rather the C or C++ code on which it is written the PHP Engine: Zend Engine and PHP extensions.

Most of us PHP developers write PHP code and run it on a Web server. The PHP Engine is already compiled and integrated with the Web server somehow.

However, before that can happen, the PHP engine code needs to be compiled from C or C++ into an executable binary made of machine code native to the CPU of your server machine.

Usually that task of compiling the PHP engine is done by developers responsible by the distribution of PHP that you use. For instance if you use Ubuntu or some other Linux distribution, the compilation is organized by the maintainers of that Linux distribution. The same goes of Windows, Mac OS or any other operating system PHP can run on.

They use C/C++ compilers available on those platforms. For instance, on Linux or Mac OS it is common to use GCC (GNU). On Windows they usually use Microsoft Visual C++ compiler.

When compiling a program it is possible to pass options to the compiler to tell it to perform optimizations to the resulting program is better somehow. It could run faster, or take less memory, or even both.

PGO is one of those optimizations that modern compilers can perform. In simple terms it analyzes the compiled program to determine the most likely paths the executed code will take in order to build a profile of the program. Then it optimizes the compilation of the program so it executes faster when running those paths.

This means that the PHP engine has to be compiled twice. The first time it compiles PHP with special instrumentation code so it can create a profile of the PHP engine code. Then you run PHP with an a PHP application that you want to optimize. It will produce profiling information that will be used in a second time it compiles PHP, this time to make your application run faster.

This means that you can have different versions of PHP optimized for different applications: one for Wordpress, one for Joomla, one for Drupal, etc.. A version optimized for one application does not necessarily work faster running another application. It depends on the kind of code that each application uses.

The good part is that you do not need to change your application PHP source code to benefit from PGO performance improvements.

Although you cannot expect that your general hosting company offers a PHP version optimized with PGO specifically for your application, it is possible that some hosting companies allow customers to choose a PGO optimized version for specific PHP applications. For instance companies specialized in WordPress hosting may offer PHP 7 optimized for WordPress with PGO.

PGO for PHP

Intel CPU

Just these days Intel announced they tested PGO support for PHP among other Open Source projects that show a measurable difference between using and not using this type of optimization.

Basically they claim they observed a difference of performance between 7% and 8% when running WordPress using PHP 7.

This participation of Intel in the efforts to improve PHP performance is not really news. As a matter of fact we commented about it in the Lately in PHP podcast a couple of months ago. We just did not know exactly what Intel would be doing to help improving PHP performance.

In reality the changes to benefit from optimizations like PGO were introduced in May this year. Intel has just been doing some tests with Open Source projects like PHP and Python. They plan to demo these improvements in the upcoming edition of Zendcon.

How to Benefit from PGO Performance Improvements?

The most important aspect to benefit from PGO performance improvements is that you or somebody else needs to compile it manually to optimize for the application you want to squeeze more performance.

This means that the generic compiled PHP binaries supplied by your Linux or another OS distribution may not perform as well as if when PHP is compiled profiling your application.

Very few PHP developers ever compiled PHP manually. Usually they take PHP for granted from their hosting company and that is it.

So to maximize the benefits of PGO, you need to download PHP source code and go through the usual sequence of steps using the commands: configure; make; make install.

However to benefit from PGO you need to change the procedure slightly. As mentioned above, you will need to compile PHP twice. In the first time you need to replace the first make command by make prof-gen. Then you run your target application that you want to optimize, and repeat the make command this time using make prof-use.

The resulting executable binaries from the last make install command is the optimized version of PHP for your application.

PHP for Windows Already Supported PGO Since PHP 5.4

PGO is not really a new thing. Several compilers support it since many years ago.

As a matter of fact PHP for Windows has been built with Microsoft Visual C++ with PGO support since PHP 5.4, even though the tests have started with PHP 5.3.

PGO support is even advertised in the PHP for Windows site, as you may notice on the left side bar.

Early tests in 2010 have shown gains of performance with WordPress of 17.4% in throughput and 14.74% and response time reduction.

Conclusion

Despite the support for PGO is not a widely known fact by the PHP community at large, it is certainly a welcomed feature that will allow PHP 7 to run even faster that we know already.

I believe the PHP community should be thankful to Intel and also to Microsoft for their help in giving this additional push to the PHP performance, which ultimately will reflect on PHP reputation and popularity among the Web developers.

If you liked this article or have questions about the PGO matters, post a comment here.


You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

3. php & pgo - Per Funke (2015-11-16 19:00)
shift in how to serve pages... - 1 reply
Read the whole comment and replies

2. back in the day, we routinely compiled our own! - David Mintz (2015-10-20 06:31)
and it wasn't that bad... - 1 reply
Read the whole comment and replies

1. Auto deployment apps - Michal Makaruk (2015-10-20 05:25)
Auto deployment apps... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 7 Performance Imp...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)