HHVM – Facebook solution for blasting fast PHP

How is it possible that most popular website nowadays Facebook is running on the worst performing language PHP? Why don’t they use C++ or Java as other big players?  What is the secret? Can you learn from their experience? Can you boost performance of your website using their solutions? Hell, yes!

If it works, leave it alone — there’s no need to understand it.
If it  fails, try to fix it — there’s no time to understand it.
( Bill Pfeifer )

Let’s start with a small story
More than six years ago, Facebook developed HipHop — PHP to C++ compiler (HPHPc). However, beside dramatic increase of performance (up to 10x), HipHop had several downsides limiting further improvements. In 2010, Facebook started to develop HHVM, a PHP virtual machine built on top of HPHPc. HHVM converts PHP code into a high-level bytecode. This bytecode is then translated into x64 machine code dynamically at runtime by a just-in-time (JIT). Although HHVM used to work as a server, current version runs in FastCGI mode, much the same way as PHP-FPM.

HHVM is the greatest thing since sliced bread
(Kunal Mehta, administrator at Wikipedia)

Real world
So this was a bit of history and theory. Now, how does HHVM perform in the real world? Like a hell! Compared to pure PHP 5.4 (and older) increases performance between 4x – 10x. If you use some PHP accelerator such as ZendOpcache or native PHP OpCache (implemented in PHP 5.5), there is still speed-up around 2x. Of course, it always depends on website itself and you can find benchmarks with comparable results to opcache accelerators.

Zeev Suraski – Zend CTO & Co-founder, has made great comparison of PHP 5.4 / 5.5 with OpCache, HHVM and future PHP-NG.

It worths to mention that HHVM eats a bit more memory, about 20%, compared to PHP-FPM. Another fact is, HHVM is not 100% compatible with PHP. Regarding HHVM team, even top 20 most popular PHP frameworks are supported, there is still room for improvements. However, Facebook has HHVM in production since 2013 and there are many websites already using HHVM, so I see it mature enough to give a try. Wikipedia, as a one of contributors, is going to implement HHVM too.

If you are into compiling stuff, read this interesting official article about how HHVM compiles PHP to binary code.

PHPNG
PHP is preparing big update in their kitchen and according unofficial benchmarks its speed looks amazing, similar to HHVM. Also, memory usage went dramatically down. However, as they announced in roadmap, we need to wait for a final release until the second half of 2016. HHVM is on fire now with fast developing cycles and according to their statement with still enoough room for further improvements, so I’m afraid PHP will be a bit late in the day.

Summary
There are pros and cons. Regular PHP accelerators are stable, compatible and established solutions. In combination with Memcached or Varnish, your website can load even faster that it could with HHVM. On the other hand, superior performance of HHVM can help you partially rid of these annoying caches which conflicts with dynamic content. Everyone who ever tried to configure Varnish with Magento (or similar solution) knows what I’m talking about.

Official website of HHVM project.