Facebook today released HipHop for PHP, a source code transformer that changes PHP into highly optimized C++, which is then compiled with G++.
The company confirmed that it has been working on the project for two years, and that it is designed to speed up execution of the code that underlies around 80% of the Facebook codebase. This is not the first time Facebook has spent development money on PHP acceleration technology.
HipHop for PHP is part of a larger effort at Facebook to speed up its PHP infrastructure. The company has already produced lex-pass, a tool written in Haskell that helps developers to refactor their PHP code.
Facebook is also responsible for PHPEmbed, another open-source tool that improves upon the PHP APIs for embedding chunks of code written in the language into other applications. Facebook has also written PHPSH, a shell for interfacing with PHP in a manner similar to Python.
Facebook developer Haiping Zhao posted on the Facebook developers blog detailing the specifics on HipHop for PHP, such as its genesis.
“One night at a Hackathon a few years ago, I started my first piece of code transforming PHP into C++,” wrote Zhao. “The languages are fairly similar syntactically, and C++ drastically outperforms PHP when it comes to both CPU and memory usage. Even PHP itself is written in C. We knew that it was impossible to successfully rewrite an entire codebase of this size by hand, but wondered what would happen if we built a system to do it programmatically.”
Perhaps the most interesting thing about HipHop for PHP is the fact that Facebook has been using it already in production for some time. Zhao wrote that “with HipHop, we’ve reduced the CPU usage on our Web servers on average by about 50%, 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.”
So why did Facebook choose to undertake such a large and complex problem, rather than use existing PHP accelerators? As Zhao explained on his blog: “Finding new ways to improve PHP performance isn’t a new concept. At runtime, the Zend Engine turns your PHP source into opcodes, which are then run through the Zend Virtual Machine.
“Open-source projects such as APC and eAccelerator cache this output and are used by the majority of PHP-powered websites. There’s also Zend Server, a commercial product that makes PHP faster via opcode optimization and caching.
“Instead, we were thinking about transforming PHP source directly into C++, which can then be turned into native machine code. Even compiling PHP isn’t a new idea; open-source projects like Roadsend and phc compile PHP to C; Quercus compiles PHP to Java; and Phalanger compiles PHP to .NET,” wrote Zhao.
Paul Biggar is maintainer of PHC, an open-source PHP compiler. He’s worked on that project since 2005, and he expressed surprise on his blog at the sudden interest in compiled PHP. He pointed out that his own compiler project, along with other PHP compiler projects, have been somewhat stagnant in recent years. He said that he has tried repeatedly to find someone to take over PHC and has failed to do so.
That being said, Biggar wrote in his blog that HipHop will still be a useful tool. “People seem to want to know if HPHP [HipHop] is widely useful outside of Facebook, and some people are saying ‘no.’ I disagree strongly,” he wrote.
“In order for HPHP to be useful, you need to have a PHP application that is suffering due to PHP interpreter performance. That matches Facebook perfectly, and they’ve always been the canonical example I use to explain why PHP compilers are interesting. But you don’t have to be Facebook-size or scale to have performance problems.”