Loading...

Facebook f8: HipHop for PHP

Submitted by: etlgfx
Updated: ; Posted:

PHP is an extremely flexible language. Pretty easy to debug, easy to deploy, easy to learn. This goes for most scripting languages, and like most scripting languages PHP is uses a lot more memory and CPU cycles than, say, a compiled language like C/C++. Facebook was mostly built on PHP, and since Facebook needs to be able to serve 400 million users, they decided to look into different ways of improving the performance of PHP itself.

Enter HipHop for PHP: It's basically a PHP to C++ compiler. I'm going to be walking you through the talk I attended at Facebook's f8 hosted by Haiping Zhao.

PHP is problematic

PHP is problematic for Facebook for a variety of reasons:

High CPU usage and high memory usage means the language isn't as scalable as it could be. It basically means buying a lot more hardware to meet increased traffic demands.

Extensions are hard to write for most PHP developers. When you run into performance bottle nencks, one way to try to increase performance is to turn chunks of functionality into a PHP extension. To do this you need to a pretty good understanding of C++ and the Zend API, which proves to be a pretty large hurdle for a lot of devs.

PHP is awesome

PHP also has a lot of benefits that Facebook developers wanted to keep:

PHP is easy to learn. There is a relatively small set of expressions and statements to learn.

PHP is easy to write. Loose typing and flexible universal array types make writing PHP code very easy.

PHP is easy to read. The syntax is very similar to C++, Java and many other languages.

PHP is easy to debug. There's no need to recompile your code, simple make a code change and refresh the page.

What HipHop does is transform PHP into highly optimized C++, and then using the (proven) g++ compiler create a binary. Hopefully the resulting binary will run a lot faster and use less memory. This way you can easily link with the extensive set of libraries available to C++. As an additional benefit, where you would previously consider writing a new PHP extension, you can now just write a new library in native C/C++ and link with that instead.

Facebook's servers are all running the HipHop compiled code now, and they are reporting 50% less CPU usage with double the amount of traffic on their external web servers, and 30% less CPU usage with double the amount of traffic inside their core API code.

Compiler Walk through

There are basically two types of PHP code. 'Simple' and 'Magic'. Thing like if statements, function calls, variable assignments and what not fall under the 'Simple' moniker. Things like $$x, eval(), etc. fall under the 'Magic' category.

The simple code is extremely easy to transform and optimize, they basically have a 1 to 1 mapping with C++ statements, and you can basically just run g++ -O3 on the resulting C++ code for advanced compiler optimizations like loop unrolling and inlining.

Dynamic function calls and variable look ups are much harder to optimize. PHP's weak typing also presents a challenge, so the HipHop compiler has a set of static analysis and type inference tools built in.

There are some features that are officially not supported by HipHop, like eval(), create_function(), preg_replace() using a /e modifier (which is just like eval).

HipHop currently supports all of PHP 5.2's language features, and they are working to support all of PHP 5.3's features as well. I imagine they are referring to things like late static binding here.

They will be trying to add multi-threading support in the future.

The Binary

As mentioned before, the result from the transformation is a binary executable. This binary acts as a standalone web server and listens on port 80 automatically. Obviously this means a lot of more advanced features that are available in servers like Apache and Lighttpd are not supported. But they said that a lot of PHP extensions are already supported by HipHop. Basically all of the extensions that Facebook uses will work.

My Opinion

Like my thoughts on Facebook's XHP, I think HipHop is a very interesting piece of technology, several of the audience members asked Haiping at the conference at what level of load it made sense to apply HipHop. For Facebook it's obviously a no brainer, they have a huge amount of traffic on their servers, and every bit of optimization is worth it. You do lose a few features on the web server side, since you're no longer dealing with a proper full featured web server.

It's available now at github: http://github.com/facebook/hiphop-php

See the source link for some more info.

Related Blog Posts