Do you know what is HHVM?

Want to speed up your PHP applications?


Facebook is the quintessential large-scale in PHP.

In 2008 it hit 100 million active users and the facebook realized that they had a big problem.

The problem is that they had explosive growth and they’d didn’t even have enough people to put working in the servers to account for their growth rate, so they literally couldn’t react to the mark they are doing.

In the process, they profiled the entire site and they realized that the problem was with the generations of pages. Some other things that were in C + + were also profiled, as well as MySQL. They profiled load balancers and everything involved and discovered what was causing the slowness was a problem with the PHP.

All pages on facebook, even a fragment, is dynamic, even in a feed there friend requests, comments, friends of friends and lot of more pieces.

Then they discovered that the problem was with PHP, but what in PHP that was causing the slowness?

They found that 20 percent of the time spent in PHP was in native code, eighty percent of which remained was interpreted code, so the challenge was to accelerate 80%.

They realized that they should reduce from 80% to zero, thus removing any part of code interpretation, the only would be 20 percent of native code, and much increase the speed.

I will not go into detail here why PHP is slow in some situations, or about benefits and cons of any language. PHP is an interpreted language, then you already realize that there are rather more slowly than a compiled language.

In 2008 to 2010 facebook was trying to solve this problem with the traffic with hundred million active users, and they was reaching to four hundred billion PHP generated page views per month.

That’s a hundred and fifty-four Thousand pages per second

In 2010 facebook invented something called HipHop for PHP to fix their performance problem.

So What is Hip Hop for PHP?

HipHop for PHP is a multi-threaded embed Web Server with a bulti-in PHP runtime. So typically you have the Zend PHP Runtime, which you know how to install, how to work with, but the HipHop replaces and optimizes.

There are two different generations of HipHop. There HPHPc, which literally translates PHP to C + + and HHVM wich is the new generation, which is called Hip-hop Virtual Machine.

Usually there is a visible improvement of 2x-5x performance increase over PHP + APC

HipHop for PHP Compiler — (HPHPc)

It translates every line for line from PHP to C++ human-readable, in doing this it actually keep the same class name that you had in PHP to c++, so, on facebook this created a very interesting opportunity for them, they could compiler PHP code in the front-end and then reuse those same classes in C++ in the back-end.

Besides the problems with dynamic typing, imagine compile a PHP code where there is a function expecting an integer result and C + + it delivers you a boolean, and problems with parity function, the biggest problem faced by this model was the long delay in compilation.

Facebok took 20 minutes to compile their whole code base, but were more than 100 machines to compile everything, so you can imagine any change that they made they pushed for production and expected hundreds of minutes or even hundreds of hours waiting for the result.

HipHop Virtual Machine (HHVM)

HipHop Virtual Machine, unlike the previous version where you had to build everything by hand down to C+ + and then run that HHVM has an embedded interpreter and one Just in Time (JIT) compiler built-in so runs as a service on a system and interactively compile your code while it’s running so if you’d call HHVM would interpret the PHP file and serve immediately

One interesting thing is that the bytecode cache survives a server restart, so unlike with PHP, if you restart Apache or Nginx than your bytecode goes away, but with HHVM the bytecode survives

How HHVM Works?

The HHVM works as follows:

A request is received and entered on the server.
If there is no native code already interpreted for that application, it executes and generates bytecode in a separate thread

See a comparison between the two models of HIP-HOP

If you do a little search on the internet about the subject you will find amazing comparative charts about the HHVM.

I have written an article (I’m still editing) where I make a comparison between WordPress running on the traditional LAMP (Linux + Apache + MySQL + PHP) versus the same running on HHVM, I can assure you that the result was surprising, I already advance that HHVM outperformed 44%.

http://www.youtube.com/watch?v=0JcugSQTfIQ

Some links:

http://hhvm.com/

https://github.com/facebook/hhvm

Thank you, see u in the next