Little did we know that, for the last six months, Facebook has made use of a custom compiler for PHP that they refer to as "Hip Hop." Essentially, it takes your PHP source code, and converts it into C++, and is then compiled with G++. As a result, they've managed to reduce their CPU usage by 50%. This project has apparently been in development for the last two years, but has only today been released to the community.
"HipHop for PHP isn't technically a compiler itself. Rather it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP's runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations."
"Scaling Facebook is particularly challenging because almost every page view is a logged-in user with a customized experience. When you view your home page we need to look up all of your friends, query their most relevant updates (from a custom service we've built called Multifeed), filter the results based on your privacy settings, then fill out the stories with comments, photos, likes, and all the rich data that people love about Facebook. All of this in just under a second. HipHop allows us to write the logic that does the final page assembly in PHP and iterate it quickly while relying on custom back-end services in C++, Erlang, Java, or Python to service the News Feed, search, Chat, and other core parts of the site."
The Problems Facebook Faced with PHP
- Can't reuse PHP logic in other systems
- Extensions are harder to write for PHP developers
- High CPU usage; can't be as efficient as something like C++
- Fully impractical to completely rewrite Facebook with a new language
How They Improved It
- It's a source code transformer
- Transforms PHP into optimized C++, then compiled with G++
- Has been in development for over two years.
- If you've used Facebook within the last six months, you've experience Hip Hop
- Facebook team have recorded a 50% decrease in CPU usage.
- They'll have dedicated evangelists to help train/familiarize the community with Hip Hop
- If you're on a shared host, using Apache, you'll most likely want to continue using standard PHP
Where Can We Start Playing with It?
Hip Hop is available now, and is hosted on Github. You can download it here.