advertise
« In Memory: Grace Hopper to Programmers: Mind Your Nanoseconds! | Main | Stuff The Internet Says On Scalability For December 6th, 2013 »
Monday
Dec092013

Site Moves from PHP to Facebook's HipHop, Now Pages Load in .6 Seconds Instead of Five

If you code in PHP have you ever wondered about moving to Facebook's HipHop JIT Virtual Machine for PHP? With HipHop Facebook achieved over a 9x increase in web request throughput and over a 5x reduction in memory consumption compared to Zend PHP 5.2 engine + APC.

But will HipHop really work for you? Is it really drop-in compatible? Is it really as fast as they say?

To answer questions like this nothing beats a good experience report and here's a great one: Adventures in Configuring and Running Facebook's HipHopVM (hhvm) JIT Compiler for PHP by Yermo Lamers.

Yermo selected PHP to implement a number of content web sites. He took an interesting approach, he created a forms, views, validation, and business logic description language to remove the drudgery of creating the same code over and over again for each page. Having done this in Perl I think it's a great a approach. The problem is it can be slow. PHP's slow string handling makes dynamically evaluating a description template for each page very slow. Up to 5 seconds.

Rather than rewrite in C++ Yermo tried HipHop. The results were impressive:

  • Pages which loaded in 5 seconds now load in 0.6 seconds.
  • Very few modifications to the codebase were required, with only two functions out of 500 files needing to be changed. Though tracking down problems was quite involved.
  • Uses half as much memory.
  • When problems were encountered the community in IRC was very helpful in finding and fixing the problems with Facebook employees responding in detail.

Though not quite a proper drop-in experience, the results seem worth the effort. Many others have reported similar or better experiences. So if you are using PHP and are looking for a magic bullet type fix HipHop may be just what you are looking for.

And kudos to Yermo for writing a domain specific language to help with the incredible tedium of typical web processes. Many programmers have probably thought of doing something similar when coding yet another form screen, so just do it. You don't always have to use someone else's framework. You can build your own.

Reader Comments (6)

Constructive criticism:

php 5.2? How does it compare to a modern php (5.4, 5.5) best-practice implementation? This seems a very roundabout way to improve performance, verging on architecture astronautics, unless he is trying to get a job at facebook.

" a number of content web sites. " Unless I missed it, none of the site(s) in question were linked. I guarantee you something else was creating poor performance. There is no way to know for sure.

"Having done this in Perl I think it's a great a approach." -- either this is a typo, or more implementation questions have to be asked. Again, no links, no source code, apart from a (rather obnoxiously inlined) config file. We have to take his word for everything.

While I don't doubt the facebook vm improves things a great deal (it's been documented!), modern php is not slow, unless you are a top-500 site, in which case, everything is slow -- and if we are talking about a top-500 site, let's see the link.

99% of websites would be better served just running the latest stock version of everything on a powerful enough VPS, and this approach is about 10 times easier than what is described here (and if you claim to be in the 1%, give me a link!)

December 9, 2013 | Unregistered Commentercan not reproduce this

If your web pages take 5 seconds to load, you're doing it wrong. Being that hiphop only speeds up PHP code, not database queries there must have been some pretty bad code in there. Now I guess the bad code goes faster.

December 9, 2013 | Unregistered CommenterNick

Todd, probably there is a mistake in the text. Instead of "Zend PHP 5.2 engine + APC" it should be "Zend PHP 2.5.0 engine + APC". Zend PHP 2.5.0 engine is used in PHP 5.5.x, this means that the article is actually about doing proper comparison in 2013 :)

December 9, 2013 | Unregistered CommenterAnton Babenko

PHP at scale is a very hard thing to do. Its not ram hungry so much as CPU in our case. Any real numbers to show CPU reduction instead of it's "just faster" ?

December 9, 2013 | Unregistered CommenterChris

Thank you for the kind words!

I always second guessed whether implementing an intermediate description language was the right way to go. PHP, especially in string handling, is so incredibly slow in addition to being wasteful of ram. Building parse trees from the XML, doing my own variable expansion and then evaluating expressions myself (I never trusted just sending strings to eval()) involves quite a ridiculous bit of overhead.

Bad code? Maybe. My design goals at the time were different.

The original plan was to implement a PHP extension in C++ to do my expression evaluation and then implement a compiler for my XML files to achieve a sizable performance gain. That would be "doing it right" but would essentially be working around the slowness of PHP by dropping into C++ thereby losing most of the cross platform advantages of doing it in PHP in the first place.

HipHopVM completely alleviates the need to do any of that since, by my off the cuff estimates, just running the code on HipHopVM on my old hardware achieves a greater performance gain than I would be able to had I done the months of work to "do it right".

Interestingly, because it's a JIT and they do some profiling of the code before actually compiling it, the resulting binary is often actually faster than the comparative statically linked C++, according to the developers. The performance is impressive to the degree that things you would normally think of doing in a statically typed language suddenly become possible in PHP itself.

IMHO, HipHopVM may very well become a game changer in the PHP world.

And there's a really cool debugger included: Configuring and Using the HipHopVM (hhvm) Remote Debugger

December 10, 2013 | Unregistered CommenterYermo

I've put together an article that covers HHVM performance gains in a bit more detail and in a fashion that can be reproduced by third parties.

How much faster is HipHopVM than PHP?

December 13, 2013 | Unregistered CommenterYermo

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>