Boost your PHP Performance 50% with HipHop

HipHopNo, I’m not talking about a break-dance move that will magically give you extra PHP performance. I’m referring, rather, to a new open source project announced by Facebook. HipHop is a “source code transformer” designed to reduce CPU and memory usage by transforming your PHP code into C++ code.

To understand why this helps, you need to know a little bit about how PHP works. PHP is a scripting language, like Ruby, Python, and Perl. This is great for developer productivity, as there’s no need for the  code to be compiled. The trade-off for this flexibility is in performance, with a scripting language requiring more CPU power and memory than a compiled language such as C++. What HipHop does is transform your PHP code into true C++ code, which can then be compiled into machine code. This is not the sort of thing you would do while developing, but when you are deploying your work you can compile it into the superior C++ code.

HipHop Performance Boosts:

  • Web — 50% less CPU usage with the same traffic
  • API — 30% less CPU usage with double the traffic

The performance boosts that Facebook has seen is based on its own real-world experience, with over 90% of Facebook servers being converted to HipHop code in the last six months. The benefits released were split into two categories, the Web tier and API tier, and the benefits witnessed for each. The web tier reduced 50% of server load with the same amount of connections, and the API tier saw a 30% decrease, even with a twofold increase in traffic. The difference is explained by the web layer requiring more presentation logic and more abstract information (related information) than the API tier. megaupload movies

What’s the catch?
You’ll have to avoid a few PHP functions, such as eval() and create_function(). Because of the dynamic nature of these functions, the HipHop team was unable to transform them into C++ code; however, these functions are rarely used in most PHP applications in general.

Currently your HipHop code has to run through a custom-written HTTP server bundled with HipHop; however, using HipHop code with Apache is high on the development road map.

While HipHop will increase the performance of your PHP code, it’s not necessarily going to be the silver bullet for your performance woes; for example, if your bottleneck is in your database performance, HipHop will be unable to help you. There’s no need to worry about it making your code slower though, as the entire PHP runtime has been emulated in a way that even the most complicated code will run at the same speed as pure PHP code.

Where can I find it?
HipHop will be released very soon to the open source community, but in the meantime you can read the full write-up on the Facebook blog, or view the Facebook presentation.

What do you think?
I’m interested in doing some benchmarks and seeing how HipHop performs with the SitePoint codebase. Do you think you’ll be checking it out when it’s released?

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • evertcollab

    here comes the hype..

  • http://www.brothercake.com/ brothercake

    Sounds like a maintenance nightmare to me – every time you make a change in your code, however small, you have to re-compile the whole thing?

  • Anthony Nandaa

    Brilliant idea. I’ll be checking it out once it’s released. Thanks for the update!

  • http://www.cemerson.co.uk Stormrider

    I’ll definitely be checking it out when it is released!

  • http://www.optimalworks.net/ Craig Buckler

    Will you have to re-compile? Or does it work like .NET and re-compile automatically any time a change is made?

  • http://www.deanclatworthy.com Dean C

    brothercake: I don’t see that as a major problem. Just write a routine to compile on save. I would guess that compilation is done quicker than you can move to your other monitor and click refresh ;)

  • http://www.optimalworks.net/ Craig Buckler

    @Dean C
    I’m not convinced compiling would be that fast. HipHop pre-compiles PHP to C, then compiles that. Large applications would take time.

    However, you could develop your application using the standard PHP interpreter then compile on release. The only issue: is a HipHop-compiled application guaranteed to work in exactly the same way? A single eval() could choke it, so you’d still need to test the resulting version.

    Even so, this could be a viable alternative to Zend and other PHP optimisers.

  • Joe Rhoney

    I’ll want to know which (if any) web applications it supports. Particularly Drupal and WordPress.

  • http://www.optimalworks.net/ Craig Buckler

    @Joe
    WordPress certainly uses a number of eval calls and there are quite a few dynamically generated function calls on runtime (especially for plugins). I doubt it’d work out of the box, but there’s no reason it couldn’t be fixed.

    That said, WP speed isn’t much of a problem if you use plugins such as WP-SuperCache. HipHop may speed up the admin interface but, typically, that’s not frequently used by lots of users. The speed improvement could be negligible.

  • Anonymous

    I will check it out when once it is released!

  • Arkh

    Before doing their stats, what was FB servers configuration (code caching boost a lot of perfs for example).

    And, all this is screaming “big exploits incoming”.

  • kammeyer

    Very cool. I must find time to check this out.

    Thanks for sharing!

  • Jean-Nicolas

    You can compile for production and for testing you use the usual…

    I think that for blogs and other basic websites it won’t be that useful. But for websites that have allot of traffic and/or use allot the CPU then it will be very useful.

    I can’t wait to get my hand on it. I do have very intense CPU apps. This will really help and will make a big difference.

  • Mal Curtis

    @Arkh the performance boosts were on top of their existing apcaching so this is a boost on top of their already highly cached performance

    @brothercake to be honest, for some people this may be a big increase in the workflow for deploying an app but, for example, every time we make a release here at Sitepoint we tag a release in GIT, then use scripts to deploy it across our cloud instances. Writing the code to handle the HipHop compiling would only take a few hours, and would only need to be done once – then the deploy operation from the developers end would be identical.

  • Mal Curtis

    @arkh re:exploits, if you watch the presentation from the developers (second link) you’ll see that they talk about how they had to ensure that the C++ version made EXACTLY the same amount of function calls, and had EXACTLY the same amount of traffic and connections through the servers. Because of this, there won’t be any new exploits as it’s running the exact same code.

  • Mal Curtis

    @jean-nicolas I forgot to mention in the article, but Facebook are working on an interpreter, HPHPi, that you can use during development.

  • ctx2002

    if you use WP or Drupal to build web site, 100% i can say, hiphop is not for you. the reason is WP or Drupal is for small web site.

    after reading some articles on the internet, i see 99.99% person or organizations do not need the hiphop.

  • seanr

    i can’t wait until someone finds a serious bug in it…
    i wonder how much testing has been done?

  • Mal Curtis

    @seanr They did testing for six months before deploying it. I think it’s pretty safe to say that after 6 months running the number one website on the internet that it’s a pretty rock solid product…

  • rblon

    @brothercake
    in development you don’t need to compile – only when releasing. So just make it part of your release script and that’s it.

    However, I do see some other issues:
    - who would be keen to switch to this custom-written http server? Configuring your webserver is a pretty important stuff if you care about security and performance. And if you don’t care about performance, why bother about HipHop?
    - often the performance bottleneck is in a few scripts (eg a polling script). If you rewrite these scrips to a compiled language, possibly using multithreading, you can make huge performance improvements (ie 50% is not impressive in those cases).

  • almasry

    Unless you become very aware of the consequences, this project is not for you .. Sooner or later PHP is implementing more advanced and sophisticated code and techniques that will be hard to convert into HipHop, you’ll need to contribute your self to writing the converter which I think is a big headache. HipHop is just for facebook and for today.

  • http://www.phptoruby.com Shaq

    as with anything that is innovative, hiphop has yet to be thoroughly tested and flaws / problems identified. For the edge cases where websites are looking to squeeze every inch of performance out of their servers, then this could be a good investment as part of the deployment strategy.

  • Mal Curtis

    @almasry there’s no product lock in with HipHop, and if you wanted to move to a new version of PHP with new features that weren’t compatible with it there would be absolutely nothing stopping you, so your point is invalid.

    I think that once the code is released and a few big sites get their hands on it and really test it we’ll know whether it’s a useful, viable tool or wether it’s a gimmick :)

  • Antonis

    This is definetely good news! I am waiting for it!
    You can boost your php apps by using cache methods like Pear’s CacheLite or out-of-the-box quickCache. Joomla, WordPress, Exlis CMS, Drupal etc. have their own caching methods. But HipHop combined with cache methods mentioned above will definetely help!

    Antonis @ http://www.01-global.net