Facebook has developed a PHP Virtual Machine that it's making available for general use. HipHop VM (HHVM) is open-source and uses just-in-time compilation to combine faster performance while maintaining the flexibility that has made PHP popular. HipHop VM has realized a greater than five times increase in throughput for Facebook compared with Zend PHP 5.2.
According to the Facebook page about HipHop (well, it has to have a Facebook page!), the HHVM team has managed a 15 per cent performance increase during May:
As the post says:
“That's faster page generation, higher requests-per-second limit, and fewer servers needed to run the same amount of traffic.”
The project's GitHub page explains that one of the explicit design goals leading into HipHop VM was the ability to continue writing complex logic directly within PHP. Most companies with large PHP codebases move the more complex code areas to C or C++ then access them as PHP extensions. Facebook wanted to keep all the code in PHP to maximize the number of programmers who can work on all the code rather than a subset.
HHVM has been in use on all the Facebook servers since earlier this year, according to a discussion about the virtual machine at OSCON (O'Reilly Open Source Conference) where Joel Pobar, a Facebook engineering manager, said that Facebook’s goal is “to make PHP run really, really quickly."
The predecessor to HHVM was a compiler called HipHop that could translate PHP code into C++ for compilation. This obviously achieved a good performance, but Facebook was looking for other ways to improve the speed of delivery of the dynamically created Web pages to Facebook’s billion users. The JIT approach of HHVM means the virtual machine can "make smarter decisions at runtime," said Pobar in his OSCON session. He said that if a call is made to the MySQL database to read a row of data, the HHVM can work out the data type, and either generate the code or call pre-existing code on the fly to best handle that data type. This contrasts with the previous HipHop version which Pobar said:
"could at best analyze the entire Facebook codebase, reason about it and then specialize code based on its reasoning. But it can't get all of the reasoning right. There are parts of the code base that you can not simply infer about or reason about,"
HHVM runs PHP programs by converting the PHP source into HipHop bytecode (HHBC) and executing the bytecode using both a bytecode interpreter and an x64 JIT compiler that seamlessly interoperate with each other. The JIT is used where possible, and the interpreter is used as an execution engine of last resort.
While the Facebook team hopes developers will try out the VM, Pobar warns that it is optimized for handling very large, and heavily used, PHP codebases, and smaller less heavily used websites wouldn’t see dramatic improvements, because the CPU execution time probably wouldn’t be the limiting factor for performance. Smaller systems with lighter use have other speed limitations. As Pobar put it:
“Chances are its spending too much time talking to the database or spending too time talking to the memcache caching layer.”