PHP 7 and JavaScript Optimizations using PGO

Despite the proliferation of NodeJS, PHP is still very widely used on the Internet, and recent changes in the lastest version (PHP 7) have improved performance, and indirectly sustainability.

However, they require that the engine be recompiled for the particular server where it is going to be used. This is Performance-Guided Optimization, or PGO. The  description on how to re-create a PGO PHP binary from a C++ recompile may be found on the following site:

The original Intel article:

Quoted values are up to 14% performance increases for WordPress.

What Needs to Be Done

To benefit from PGO you need to change the procedure slightly.

  1. In the first time you need to replace the first make command by make prof-gen
  2. Run your target application that you want to optimize
  3. Repeat the make command using make prof-use
  4. Run make install

This results in an optimized version of PHP for the application. It implies that most of your use will be for a specific app like WordPress or other CMS systems.

Useful, but not in Use?

Trouble is, most hosts and Linux distributions don’t have this optimized version. Unless a web host recompiles PHP this way, they lose several percent in efficiency. The ability to do this optimization has been around for several years…but how many hosts have done this optimization? According to the Intel article, Mozilla uses PGO when compiling Firefox. But we don’t know about the thousands of hosts running WordPress for clients on their servers.

My guess is that many have not.

Now, if you have a virtual server, you may have the ability to re-compile PHP just for your account. But this kind of hosting account is more expensive than the ones usually used.

The application-specific nature – you have to optimize for a specific application – means that hosting companies would have to split their generic web servers into a group of application servers – in other words, a “cloud”. So, a PHP/Apache combo running WordPress, with PGO applied during the compile, could be efficient with WordPress but inefficient with another CMS.

What About JavaScript?

Now, modern web services use things like NodeJS or Ruby on Rails, which replace a generic web server with an application-specific one. If NodeJS supported PGO, it is likely it could be highly efficient. However, I couldn’t find anything about how the NodeJS binary is compiled for various systems. All I found was a suggestion:

My guess is that PGO has not been a priority for Node. This is a shame, because Node is typically used to create very specific kinds of applications, in contrast to the generic Apache/PHP hosts. Among other things, it would require that web devs recompile their Node binaries after they’ve built their apps, and distribute the binaries into their “cloud” servers.

While JavaScript is not compiled, it is possible to do manual PGO within your code.

Here, manual profiling of code running in an application allows significant speed bumps – by finding the most common paths through the code during actual operation, and making “shortcuts” for these common paths.

And, the fact that JavaScript interpreters like V8 run repeatedly (instead of being compiled once) allows for performance-guided optimization within the JavaScript interpreter. This is dynamic PGO, rather than the static version described above for compiled programs.

In fact, this article notes that V8 running JavaScript can actually beat GCC-compiled C++ in some application-specific situations:

Writing JavaScript in an “assembly-like way” (e.g. Asm.js) it is possible to transpile other languages like C++ to JavaScript. When converted with a tool like Enscripten, the C++ => JS program retains some of its former compiled speed when interpreted by V8 and other engines, while getting some of the benefits of interpreted languages.

What this all shows is that there is plenty of optimization to be done in the hosting space. We already know that “traditional” web hosts vie to be the “greenest” but to date nobody in the NodeJS world seems to be thinking this way. Would NodeJS style “cloud” apps benefit from PGO, or using ASM.js standards become common in server-side JavaScript? Will traditional web hosts adopt PGO for their environments. It makes sense if they advertise as green.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s