Facebook’s HipHop – King of PHP Accelerators?

Facebook is one of the world’s busiest websites. By some measures, Facebook has passed Google in traffic. Facebook has at least 30,000 servers to serve all their traffic – and that was back in October 2009.

As Facebook is written predominantly in PHP, many of those servers are running PHP. What if Facebook only needed half the number of its PHP servers to serve the same traffic?

To make it’s PHP code efficient, Facebook developed it’s HipHop technology (not “Hip Hop”). Facebook serves 90% of it’s traffic with HipHop. Facebook gets a 50% reduction in CPU usage over Apache and PHP for the same traffic. In Facebook’s API tier, which presumably is more code intensive, they can serve double the amount of API calls with a 30% reduction in CPU.

Facebook has made HipHop open source so you can use it. It’s available at GitHub.

How HipHop Works

PHP is scripting language, and scripting languages, like Perl, Bash & Python, are usually interpreted. Scripting languages are great to develop in, as the development cycle is usually quicker. Scripted languages can be executed immediately, in place. Scripted languages usually have high level primatives, such as arrays and hashes which make them fast to develop with.

Compiled languages such as C or Java, typically go through compilation, packaging & deployment, before the program can be tested. So each cycle of editing code, compiling, and deploying adds time to the development process.

Interpreted languages: faster to develop, slower to run.
Compiled languages: slower to develop, faster to run.

HipHop is a PHP cross compiler. HipHop compiles PHP to optimized C++. The C++ is then compiled to directly executable binary code.

By effectively compiling PHP to an executable, HipHop takes PHP from an interpreted language to a compiled language.

Other PHP Compilers

HipHop isn’t the only PHP compiler out there. There’s also phpc, Roadsend and possibly others. PHP Classes have a cool PHP compiler benchmark. Given HipHop’s deployment at Facebook and it’s benchmark numbers, it’s likely to be your best choice for a PHP compiler.

Does Hip Hop Use Apache?

HipHop does not use Apache to serve web pages. Apache is very flexible, extensible and stable, but not all that efficient. HipHop has it’s own simple, lightweight web server based on libevent.

PHP Compiled to Optimized C++

What does “optimized” mean when HipHop says it compiles to “optimized C++”. HipHop does static analysis of the PHP code (versus analysis at runtime) to determine whether to apply optimizations. For example, if HipHop can tell the operands of an operation are all integers, then HipHop will use an integer type in C++.

Unlikely to Noticeably Speed Up Pages

As Rasmus Lerdorf points out, HipHop is unlikely to speed up web page load times significantly for most sites. For many sites, most of the webpage response time is from network transfer (those 3 CSS files and 6 JavaScript files) and shared data retrieval – from a database, memcache or a cloud filesystem.

If you have a very complicated PHP application with many object layers (EZ CMS comes to mind) or the backend work is very light (say just a memcache call), there is a potential to to speed up page response.

Same Traffic, Half the PHP Servers

Where HipHop really excels is in scaling. With HipHop, chances are you’ll need half the number of PHP servers to serve the same traffic. That’s awesome!

What are the Cons for HipHop?

Well, as for compile languages, you’ll need to compile, build and deploy your PHP code to get the performance boost. For now, you’ll also be limited to PHP 5.2 features (e.g. no namespaces from PHP 5.3), and there are are few restrictions, e.g. you can’t use eval().

What are HipHop’s Competitors?

Well, any of the PHP accelerators, such as APC, eAccelerator or Zend Accelerator. These typically work by compiling the PHP into bytecode. This bytecode isn’t CPU instructions (as produced by the full HipHop compilation process), but instead a form of PHP that’s much more efficient to interpret (e.g. it’s in binary form, comments are stripped, variable names are shrunk). The PHP accelerators can cache the bytecode on disk or for a big speed boost in shared memory.

The advantage of the bytecode-style PHP accelerators over HipHop is that they’re typically a drop-in PHP module. They transparently convert new PHP to bytecode, so normal PHP deployment processes can be used.

The advantage of HipHop over bytecode-style PHP accelerators is that it’s fully compiled. In most cases, HipHop will execute PHP faster and more efficiently than a bytecode-style PHP accelerator.

If you’re trialling HipHop or using it in production please let us know in the comments below.

Bookmark the permalink.

8 Responses to Facebook’s HipHop – King of PHP Accelerators?

  1. gopi says:

    I converted my php to c++ code using php hip hop. Now i want to compile it using cross compiler. Please guide

  2. gopi says:

    Hi Tas,

    I already have an cross compiler. I use openembedded to cross-compile opensource software; I have the CPP code, I don’t understand how the program flows. I have directories called
    and some header files. First of all let me manually compile the converted php to cpp source for my x86; Then I could do it for powerpc. I already setup powerpc cross compiler and successfully compiled a hello world c program.


Leave a Reply to gopi Cancel reply

Your email address will not be published. Required fields are marked *