Created in 2010 by Facebook in order to save server resources, HHVM works as a self-contained web server that executes PHP scripts, replacing Apache and mod_php altogether. Although the initial performance of this virtual machine didn’t present an appealing alternative to our standard PHP-APC setup, many factors have changed since the inception of HipHop.
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. In a real world scenario, it’s not often that you would 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.
Unfortunately, not everyone has direct access to a gigabit fiber optic Internet to enjoy the 4ms speed gain.
Memory Utilization 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 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 on one of our custom built 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. One of the issues is the server requirements – HHVM is pre-compiled only for selected Linux distributions and doesn’t run on 32-bit machines. This poses constraints on server selection and subsequently not all existing servers can enjoy this upgrade.