Some experiences using Facebook’s HPHP / Hiphop
Hiphop is Facebook’s open source C++ compiler for PHP. Hiphop (also known as HPHP) will compile your PHP code to C++ code, which you can then compile (with g++) into a big binary that will run your web site. The binary includes a web server, which will then behave (almost) exactly the same as your Apache + mod_php or Nginx with fastcgi PHP. If you want to know more about Hiphop and how it was created, you can check out the Facebook Engineering Blog article or the Github project.
After its introduction many bloggers have written background articles regarding Hiphop but few seem to have actually used it in a production environment. I have and here are some of my thoughts on working with Hiphop in such an environment.
Speed
Hiphop includes both and interpreter and a compiled version. This is something that is not frequently mentioned in blog posts about Hiphop. The interpreter (called HPHPi) can be used for developing, thus saving you the hassle and the delays associated with compiling every time you change a single file. (Compiling a simple 10 line script takes about one minute. This is probably because the web server has to be compiled too and this component is a few orders of magnitude more complex than my PHP script). In general is has the same behavior as the compiled version of Hiphop. It is a bit slower than using PHP with an opcode cache, but it is not that bad and with a fast laptop it is still workable.
The compiled version is really fast, at least a few times faster than using PHP with fastcgi. Also, Hiphop has become already three times as fast since it has been released in Februari 2010. So the speed is really, really good.
Behavioral differences
The HPHP interpreter, the compiled version and the official PHP interpreter will all give you ever so slightly different behavior. So make sure that your unit tests are ran against all three versions automatically or you will drive yourself and your team members insane, especially in a mixed PHP / HPHP environment.
Some PHP 5.3 stuff is still not completely supported in Hiphop, such as namespaces, of which at the moment nearly every feature is broken. Support for anonymous functions have only recently been added. Extensions such as PHP’s SOAP extension have been ported but some behavior varies between the variants. Small stuff mostly. For example, if you get a SoapFault exception somewhere, in PHP the exception will contain the message from the SOAP response, but in HPHP the exception will contain the entire SOAP response.
If you want to write scripts that are PHP and HPHP compatible, you will need to write some if statements here and there. Definitely test all your code against the compiled, interpreted and official PHP binary.
Features
Hiphop has some really cool features. There is a call_user_func_async() function (it does exactly what it says), and you can have two versions of you web site running at the same time, so that you can deploy a new version without downtime. Also, you can catch fatal (E_FATAL) errors with their stack traces with HPHP, a feature sorely lacking from PHP. There is only sparse online documentation of these specific features, but you can set up your own documentation server.
Among these new features, HPHP introduces some new errors that can occur in your scripts, mostly when you do strange stuff which you are not supposed to some datatypes., but PHP still allows. For example, in PHP this will not throw any errors: $var = null; $var['foo'] = ‘bar’;. In HPHP, this will throw an E_NOTICE. I will leave it as an exercise to the reader to decide which of the two behaviors actually is preferred. In general, HPHP is more strict and less forgiving, so that will probably cause latent bugs in your project to rise to the surface.
You gain some, you loose some: some PHP features such as fastcgi_finish_request() are not available under Hiphop. If you want any of the non included PHP extensions, you will have to port them to C++ yourself. Since they are already in C, it is not hard but you still need to do it.
Open source
One of the great things about Hiphop is that is is an open source project. The code for Hiphop is shared on Github. If you file a bug, Facebook’s maintainers can be pretty responsive. I filed a one (behavior difference from PHP for anonymous functions) and it was fixed four days later. Of course you can always fix it yourself on Github and try to get the fix pulled into the project. Not many people are actually using Hiphop in production, so the community is pretty small outside of Facebook.
Conclusions
Facebook’s Hiphop is really, really fast and offers useful features. However, you can imaging that with the behavioral differences and the changes you will need to your build and deploy process it may or may not be worth it for your web site.
About this entry
You’re currently reading “Some experiences using Facebook’s HPHP / Hiphop,” an entry on Willem Stuursma
- Published:
- June 12, 2011 / 17:42
- Category:
- Hiphop for PHP, Work

2 Comments
Jump to comment form | comment rss [?] | trackback uri [?]