We are ready to deploy the HipHop Virtual Machine (HHVM)
for some selected customers who may benefit from a performance boost.
Originally created in 2010 by Facebook to save server resources, HHVM works as a self-contained web server that executes PHP scripts, replacing Apache and mod_php altogether. The initial performance of this virtual machine didn't present an appealing alternative to our standard PHP-APC setup. But many factors have changed since the inception of HipHop. We hope to paint a comprehensive picture for our customers, so that they understand the nature of HHVM, and how it might benefit their projects.
How does it work?
Currently we use the Alternative PHP Cache (APC) to optimize our servers. APC compiles PHP scripts into bytecode and stores the result in shared memory. This optimization typically yields 5x speed gain, effectively eliminating script executing time as the bottleneck.
Unlike other accelerators which use similar bytecode and caching mechanism, HHVM is built from ground up using its own implementation of PHP. What really sets HHVM apart is its just-in-time (JIT) compiler, which converts bytecode further into x64 machine code. JIT is the little gem that runs PHP in nearly native code speed.
Impacts on Speed
When we ran our production code on HHVM and measured against Apache-PHP-APC, we observed no apparent speed gain. This is because our applications (LCHH
and Gyroscope) are already highly optimized. The Fibonacci benchmark, however, was impressive. What took APC 5 seconds to run took HHVM only a fraction of a second. But realistically, how often do you compute recursive mathematical sequence in a PHP web application?
We also measured the latency improvement. Again the marginal time saved by HHVM is eclipsed by the network delay. Fortunately we had access to the ORION network
. With the help of a gigabit Ethernet connection we managed to observe the latency of a simple script reduced from 7ms to 3ms.
Memory Utilization Gain
But not everyone has direct access to a gigabit fiber optic Internet to enjoy the 4ms speed gain. What really won our heart is HHVM's memory characteristics. Years ago we established that a PHP script cannot consume less memory than a segment
. We measured a segment to be 256k on a 32-bit machine, and 512k on a 64-bit machine. No matter how lean the script is, you cannot go below the segment barrier.
HHVM uses its own implementation of PHP and generates machine code through JIT, bypassing Apache and the traditional Zend PHP interpreter altogether. As a result, a 3-segment page in APC takes only 30k, or 0.11 segment in HHVM. This effectively improves memory utilization by almost 30 times!
Now if we compare a HHVM-run Gyroscope application to a un-optimized blog site that's running Wordpress (60 segs), the memory multiplier is calculated as 60*30=1,800. In other words, the same amount of memory that serves one Wordpress visitor can accommodate 1,800 concurrent users to a Gyroscope application, using the same hardware. Of course the number fluctuates, but it's reasonable to expect a x1000 improvement.
HHVM is not for everybody. Although it runs all our codes out of the box, HHVM's PHP implementation isn't complete enough
to run many popular PHP frameworks. This is the least of our concern as we've been advising our customers to steer away from these clumsy packages. A more practical issue is the server requirement. HHVM is pre-compiled only for selected Linux distributions and doesn't run on 32-bit machines. This poses a constraint on our server selection. Subsequently not all our server instances can enjoy this upgrade.