As a website grows, it requires more servers, more memory, and a larger investment from the enterprise that runs it.
Imagine how much that overhead would be if you had to serve up 400 billion page views each month to more than 300 million individual users. That was Facebook's challenge, and the company solved that problem by creating a PHP-to-C++ transformer, which was able to cut CPU requirements by 50 percent for system-intensive direct calls and by 30 percent for API-related calls. The transformer also drastically reduced Facebook's memory requirements (have you seen how much server RAM costs?) and actually sped up site performance.
Introducing HipHop for PHP
After running as much as 90 percent of its traffic on HipHop for the past six months, Facebook announced yesterday that it is releasing HipHop as a new open source project. Web developers can now implement this powerful transformer on their own sites and enjoy similar benefits.
"I'm excited to share the project a small team of amazing people and I have been working on for the past two years; HipHop for PHP," Facebook's Haiping Zhao wrote in a blog post. "With HipHop we've reduced the CPU usage on our web servers on average by about fifty percent, depending on the page. Less CPU means fewer servers, which means less overhead. This project has had a tremendous impact on Facebook. We feel the web at large can benefit from HipHop, so we are releasing it as open source this evening in hope that it brings a new focus toward scaling large complex websites with PHP. While HipHop has shown us incredible results, it's certainly not complete and you should be comfortable with beta software before trying it out."
Watch the HipHop for PHP Announcement
How HipHop Works
According to Zhao, "the main challenge of the project was bridging the gap between PHP and C++. PHP is a scripting language with dynamic, weak typing. C++ is a compiled language with static typing. While PHP allows you to write magical dynamic features, most PHP is relatively straightforward. It's more likely that you see if (...) {...} else {..} than it is to see function foo($x) { include $x; }. This is where we gain in performance. Whenever possible our generated code uses static binding for functions and variables. We also use type inference to pick the most specific type possible for our variables and thus save memory."
HipHop transforms the PHP (1) first by doing a static analysis wherein it discovers which PHP functions and methods make declarations and what dependencies those declarations have; (2) by using basic type inference (remember PHP is loosely typed so that my variables can be strings one moment and arrays the next), selecting specific C++ types; and (3) finally, by generating new code in C++ that directly corresponds to the PHP statements and expressions.
HipHop uses g++ (which is the common nickname of the the GNU C compiler) to compile the C++.
Because C++ does most of its work before runtime, the language is a lot more efficient than PHP when it comes to the speed, CPU use, and memory.
So Why Not Write in C++
If C++ is so much better, why not just use it? Because it is a lot harder. C++ is a much more difficult language to use than PHP or other scripting languages. If you want to be able to iterate or innovate quickly, PHP is a better choice, particularly if you have a relatively small engineering team. C++ could require a lot more engineering up front.
Is HipHop For You?
Facebook has yet to provide much more information about HipHop. There is an empty HipHop Google Group to join and a short mention of the transformer on Facebook's developer site.
If HipHop is not difficult to implement, it has the promise of saving nearly every company that operates a PHP-based website a significant amount of money on web servers and memory, while improving user experience.
There are some tradeoffs, however. HipHop cannot manage some of PHP's more powerful ("magical" to use Zhao's word) capabilities like eval().
