Wednesday, February 03, 2010

HipHop for PHP - running PHP at C++ speed

The Facebook folks have been at it again. After giving the world Cassandra, Thrift and Tornado, they are now on the verge of giving to the open-source world another piece of exciting technology - HipHop for PHP (a bit of a silly name, IMHO :-) )

In short, HipHop for PHP is a platform consisting of a PHP to C++ transformer, a C++ runtime library (instead of the Zend Engine) and a simple web server (with Apache webserver support on the way) to allow developers to develop/test code in PHP but run the final code as C++ code on the server. This means developers can work with a simple, dynamic-typed language such as PHP, without sacrificing runtime performance.

There is also a custom PHP interpreter called HPHPi to facilitate debugging. Of course, a developer can always use the standard PHP interpreter since he / she will be writing pure PHP code.

Despite some syntactic similarities, PHP and C++ are essentially two very different languages. PHP is dynamically + weakly typed, whereas C++ is statically + strongly typed. Effective transformation of PHP code to C++ code would inevitably involve global code analysis and type inference to achieve early binding and specific typing, etc.

The Facebook guys explains this thousands times better than I can, so check out this excellent video to see how this all works:



One point I have picked up from the video in particular is the tradeoff between the dynamicness in your original PHP code and the performance of the final C++ code. CPU / Memory performance gain seems to come primarily from early binding and specific typing (using primitive C++ types, for example). While HipHop for PHP seems to be very clever, the fact remains that if your PHP code results in loads of dynamic function calls and dynamic variable lookups, or even the abundant use of "variant" types in the final C++ code, you may not see such a big gain.

Whether this will prove beneficial to the PHP community, only time will tell... But it certainly looks good!

P.S. Funny thing is I have just spent the last year or so working on a GWT project, writing Javascript frontend in Java. (and of course we ended up using a bit of Jython for some of the backend stuff...) I seem to be living a life in translation :-)

No comments: