Update: Looking to try HPHP? Take a peek at my open-source HPHP UI.
What are we looking at?
The chart above shows Ubuntu in a 2 and 8 core configuration (the x2 and x8), along with a Server 2008 instance running a benchmark program to stress a PHP based webserver.
What does it mean? The good folks over at FaceBook recently released a custom tool called HopHop that rewrites PHP into native C++ code. The upshot to this, as you can see, is bare-bones PHP code, that is, code which relies on math and loops and such, is much, much faster. Of course other code should be faster as well, though I have yet to benchmark more complex scripts.
The downside or, what’s the catch?
To be honest these aren’t really downsides considering how new, cool, and free it is, but as of right now getting a HipHop accelerated script running on your own system means having 64-bit Linux, knowledge of the command line (though not too much), and scripts that play nice with HipHop.
By play nice we mean that HipHip, when doing its thing, not only rewrites your script into native C++, it can also wrap the entire thing into a single executable. That means with a single binary file you get an entire PHP 5.2.x instance, your script, and a basic web server to serve the script(s).
In fact, you can try it out right here: hiphop-demo
If you download this file, when you expand it you’ll get a folder called hphp_2ZEavp. This folder contains a binary file called program, and so long as you have Ubuntu 9.10, you can execute the program with:
sudo service apache2 stop
cd into: hphp_2ZEavp
sudo program -m server
Again, you need to use Linux (and only tested on Ubuntu 9.10), but if you do, so long as nothing else is bound to port 80 (the first line will stop your apache2 server if running), you should get a few lines about how the static content is loaded, and how all servers have been started.
You can now load the script in your web browser with:
http://localhost/bench.php
Again, the single executable has the entire PHP interpreter and many extensions, your script, and a web server all wrapped into one element. This is why it’s quite large considering the PHP script is only a few kb. (It also includes the build files and raw C++ source code so you can see what HipHop does to a PHP file at: hphp_2ZEavp/php/bench.cpp)
Things to consider
In order to work its magic, HipHop needs to:
- Ditch some rarely used features such as eval()
- Requires a 64-bit OS, so for example, you need to download 64-bit Ubuntu.
- May exacerbate some common PHP quirks such as a NULL being returned as opposed to false for a read file operation (this generally isn’t HopHop’s fault, but PHP’s documentation)
- Doesn’t implement all PHP extensions, most notably PDO
- Is still rather new, so their are incompatibilities if your code is not super ‘tight’. For example, bashing function and file names, assigning a constant a value and then concatenating one constant with another, and so on.
- Requires all code files to be present and complete before a compile. This means, for example, that the configuration script for WordPress must be filled out before your compile the code though HipHop.
- Supports 5.2.x at the moment, with plans for 5.3 still in the works.
- It’s hard to tell if the generated code is multi-threaded, but probably not. The x2 and x8 difference in the benchmarks can be attributed to the x8 server being a faster processor with better RAM speeds.
- As extensions need to be custom written for HipHop, this can mean upstream features take longer to make it into a production environment.
Thoughts on the future of HipHop and what it means for PHP:
First, this technology has the potential to open new doors for PHP. Specifically, raw number crunching is vastly improved under HipHop (as you can see my own testing shows a roughly 5x improvement), which means taking on more CPU expensive tasks such as image manipulation and complex security code is made more feasible.
If nothing else, the large frameworks like Zend Framework will benefit greatly when and if they are ported, as anyone who’s done code coverage on a simple ZFW page call can attest too, those 18 thousand lines being called for each request (byte-code cache not withstanding) loves to eat them some CPU time.
Second, it will force a bit more compliance when it comes to coding standards. Don’t get me wrong, PHP is made better because of its loosely types roots and flexible coding style, but the kinds of issue HipHop expose will make PHP better by highlighting problems and encouraging more rapid solutions. It will also encourage, for the big boys at least, a more standardized form of PHP coding–In short, you’ll learn to start coding for HipHop.
Third, one thing that’s always been a pain with PHP deployment is relying on nonstandard and unsecured text files to store things like database information. Having such files with HipHop is simply not possible, as everything needs to be finaliazed before the compile step. Seeing as this is now open source, it’s not hard to imagine someone developing a set of tools which standardize this process, complete with OS installers and the like to safely and reliable deliver you PHP applications. This is a will for users, and a win for developers.
More importantly, it’s not hard at all to see how this tech could be integrated into a larger pipeline with just a few tweaks. If you’re in, say, Visual Studio building a .NET app you already have to ‘build a solution’, so what’s the big deal having to do the same thing in PHP when you publish your final application? That said, their is also an executable called hphpi that apparently interprets in real time, but I have yet to try it.
Overall though, this is a very promising start, and one that finally puts PHP in the same league as the compiled languages such as .NET and Java in terms of speed and performance. It will be interesting to see if Microsoft, who has been courting the PHP community as of late, will do anything with this. For those who wonder why I would speculate in such a way, Microsoft has pleasantly surprised me with this.
The Bottom line
Without a doubt HipHop is a transformative piece of technology, a real game-changer for larger PHP shops. Their are plenty of things yet to do, but if 90% of FaceBook’s code-base already runs on this tech, so can yours should you take the time to built it right–and the performance will certainly be worth it.
The importance of private help with open-source
This does bring up one other point though, which is as much as I love open source, free software advocates like myself must realize how important private and corporate influence is on our tools of choice. HipHip may not be a necessity for most of us, but if PHP wants to continue its dominance on the web for larger entities, its performance shortcomings needed to be addressed. HipHop does so, but in this case, only because of private money and ingenuity.
A Quick PS–Here’s the raw bench script the HipHop example is based on.
