Facebook Hopes to Revolutionize PHP with

Facebook Hopes to Revolutionize PHP with “Hip Hop”

Little did we know that, for the last six months, Facebook has made use of a custom compiler for PHP that they refer to as “Hip Hop.” Essentially, it takes your PHP source code, and converts it into C++, and is then compiled with G++. As a result, they’ve managed to reduce their CPU usage by 50%. This project has apparently been in development for the last two years, but has only today been released to the community.

“HipHop for PHP isn’t technically a compiler itself. Rather it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP’s runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations.”

The Announcement

“Scaling Facebook is particularly challenging because almost every page view is a logged-in user with a customized experience. When you view your home page we need to look up all of your friends, query their most relevant updates (from a custom service we’ve built called Multifeed), filter the results based on your privacy settings, then fill out the stories with comments, photos, likes, and all the rich data that people love about Facebook. All of this in just under a second. HipHop allows us to write the logic that does the final page assembly in PHP and iterate it quickly while relying on custom back-end services in C++, Erlang, Java, or Python to service the News Feed, search, Chat, and other core parts of the site.”

The Problems Facebook Faced with PHP

  • Can’t reuse PHP logic in other systems
  • Extensions are harder to write for PHP developers
  • High CPU usage; can’t be as efficient as something like C++
  • Fully impractical to completely rewrite Facebook with a new language

How They Improved It

  • It’s a source code transformer
  • Transforms PHP into optimized C++, then compiled with G++
  • Has been in development for over two years.
  • If you’ve used Facebook within the last six months, you’ve experience Hip Hop
  • Facebook team have recorded a 50% decrease in CPU usage.
  • They’ll have dedicated evangelists to help train/familiarize the community with Hip Hop
  • If you’re on a shared host, using Apache, you’ll most likely want to continue using standard PHP
  • Where Can We Start Playing with It?

    Hip Hop is available now, and is hosted on Github. You can download it here.

    Hip Hop Process Graph

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Andreas

    interesting stuff… they have the money… they deliver good things for the community…

  • http://www.mimrankhan.com Imran Khan

    php to C++ kind a interesting….

  • http://wptricks.net WP Tricks

    I am didn’t understand about php and g++ but it’s look like interesting method

  • http://edfuh.com edfuh

    My entire belief system has been shaken.

    • korah

      mine too..

      • Chris

        agreed

      • Matt

        This.

    • chandrashekar

      absolutely true.

    • http://www.alfystudio.com Ahmad Alfy

      ROFL!!!

  • http:/www.biberltd.com Can Berkol

    hardcore. i bet they still need more optimization. it’s amazing how they survive that many human driven database transactions with so few losses.

  • Zulfur Raju

    The download link is not working

  • http://www.iamsupernicko.co.cc/ Nicko Picazo

    2 thumbs up for facebook developers! very interesting!

  • Armin

    So, where can we get it to test it out?

    the github page doesn’t exist (heres facebook’s repo: http://github.com/facebook/)

    • http://tutorialswalk.info Abhishek Bhardwaj

      Remove the bracket in your URL and try loading the page. It works for me!

  • http://www.avey.de Way

    Great stuff and a good push for php. Keep going fb!

  • uu12nal

    Hi,

    Video don’t play video

  • Skunkie

    Writing a code transformer for the used language in order to avoid having to rewrite the whole site in another language – what a strange and funny idea this is. But if it works – cool!

  • http://www.designlovr.com DesignLovr

    I’m not sure if the average php-guy will actually profit from such a complex code transformer, but I definitely appreciate the effort!

  • http://www.msinkinson.co.uk Mark Sinkinson

    Is this why Facebook is so rubbish now? For the last 6 months I have seen ‘An Error has Occurred’ or ‘Something Has Gone Wrong’ being reported by numerous friends. It is why I switched to Facebook Lite.

    Great idea but shoddy, bug-ridden code….

  • c0mrade

    Has anyone managed to open this link?

    http://developers.facebook.com/hiphop-php/

    Is the code released yet?

  • http://www.dynamicpalette.com Chris Seckler

    I don’t know if the link on the other page isn’t working, but http://github.com/facebook/hiphop-php takes me to the main github.com page.

    • http://www.andreaolivato.net Andrea Olivato

      Yeah same here… really would like to try it out

    • Camilo

      I have the same problem… anybody knows where has the code?

    • Miguel

      Yeah, I can’t open it either. And it’s the same link they have at facebook and at many other sites I’ve checked.

  • khaled

    Nice stuff! wow

  • http://www.jordanwalker.net/index.php Jordan Walker

    Great news for the PHP community!

  • Chandrashekar

    Intresting I love PHP and have explored as much as i could but this is something i always thought about “cpu usage associated with php”, great that it is implented like this and in on of my favorite site, kudos facebook team.

  • Chandrashekar

    Intresting I love PHP and have explored as much as i could but this is something i always thought about “cpu usage associated with php”, great that it is implented like this and in one of my favorite site, kudos facebook team.

  • http://www.wdonline.com Jeremy McPeak

    It’s about time PHP became JITed. It’ll be interesting to hear what the zealots, for the lack of a better word, say.

    • Marshall

      Haha, which ones? PHP zealots or C++ zealots?

  • Patrick

    This is absolutely useless for small php devs. This tool is ONLY for websites with really high loads and traffic. This is NOT for small sites like tutsplus-Network or something like them.

    And the name… Terrible. Hip Hop for PHP. Pure nonsense.

    • Torkild Dyvik Olsen

      That’s not true at all. Yes, you will have much more to save with a large site, but you will still have something to save on smaller. I’m not saying you should start using HipHop on your blog right away, but you should maybe think about it if your blog gets enough hits.

      I know little about Tuts+ hosting, but I guess they have control over their servers, and might have both money and environment to save by implementing something like HipHop.

      Regarding the name, I kind of agree with you. It’s not terrible, but definitely not good.

  • http://www.rubyonrails.org Paul

    praise php all u want…RUBY WINS!!!

  • Dimitar Stoyanov

    It looks like HipHop is not available for download… http://github.com/facebook/hiphop-php is not exists, it returns to Github’s index page.

  • no

    You could always improve your sites by not using php in the first place.

    What a duh moment.

    • Patrick

      u’re kidding us, right? It’s really possible to code high performance sites with php. It just need advanced skills and wise ideas.

    • Bretticus

      Apparently, you forgot about twitter being down about every day for months on end. So much for Ruby on Rails scalability. ;)

  • Sean Foushee

    Seems like an ingenious way to get around rebuilding the service in a different language, but I wonder how reliable the “transformer” is and what sort of coding practices it requires from developers to ensure eveything is transformed and compiled properly.

    Definitely on my “to-do” list for this month. Thanks for the post!

    • lilwiki

      “Reliable” !!! Facebook is using it in 90% of page fetches.

      As regards to coding practice, I don’t know what to think at the moment. They built it (hiphop for PHP), then applied it to an existing code base, thats a more positive signal than if it were the reverse.

  • http://twitter.com/sergi_bosch sergi

    i love the fact that they were brilliant enough to invent the compiler, then successfully implement it, then release it to “us”. kudos to facebook.

  • http://www.phpandstuff.com Burak

    This could be an alternative to Zend Guard.

  • Khalil

    great technology… how can we use it with php?

    Thanks for sharing.

  • http://www.systrix.com Systrix

    HipHop seems really interesting, we have had to switch to lighttpd and xcache to achieve huge performance benefits for our objectCMS framework which is now faster that wordpress, joomla and drupal. I am wondering how hiphop compares to lighttpd and xCache, does anyone know when hiphop is going to be released to the public.

  • http://blog.tallkey.com koder

    the link doesn’t work!!!!

  • http://www.freshclickmedia.com Shane

    It’s a little short-sighted, but also inevitable, that some people have chosen to pick on PHP’s suitability for large sites, when the site in question is one of the biggest on the whole of the internet.

    From a technical standpoint, it’s very interesting, but the fact is that PHP would never be as fast as optimised C++, so what they’ve done is both ingenious and thought-provoking.

  • Ed

    This is very cool!

    I do a lot of PHP work but I also do a lot of ColdFusion work as well and most people don’t know that ColdFusion like PHP is a scripting language – but the key difference is ColdFusion compiles into Java that runs in any J2EE server and the kicker is, they have been doing this for years now…

    I like both development tools, but I have to give credit to ColdFusion for being ahead of the game on this aspect.

  • Matt

    I saw Jeffery tweet this last night, and I was like “8 hours until Nettus post, clock it”

    I was right.

    Also, this sounds awesome, though I guess it still doesn’t do much for PHP on the desktop front (though, it was never supposed to).

  • http://itpixels.com itpixels

    this is pretty exciting…but i’m just wondering if it can be beneficial for small to mid scale systems…

  • Stephen Coley

    I’m curious as to whether they’ll begin writing new Facebook code in C++ now that they have Hip Hop to convert their original code, or if they’ll run new PHP code through Hip Hop.

    There CPU usage may be down, but I’ve noticed Facebook being very buggy these past six months.

  • Talal Bin Ghizal

    where is hiphop ?

    Github returning to index page when visiting this url:

    http://github.com/facebook/hiphop-php

  • Robert

    This is a great approach by facebook. Even tho in the agency where I work we’ve got in house C++ gurus and when we are making something really heavy we take another approach; writing a driver for Apache which I think is more suitable than this approach. I’m not a heavy user of Facebook, but the little experience I have with C/C++ tells me that facebook will run into lots, and lots of bugs unless they’ve managed to do something really revolutionary.

  • http://www.socialjitney.com Iphone developer

    I decided to create an application that uses the Facebook Platform. writing the application in PHP and I thought it might be useful for others to know how to write their own Facebook PHP applications.

  • ellisgl

    I wouldn’t call it revolutionary, since it’s not going to convert all PHP into C++, since there are a lot of restrictions. But I believe that this will help a lot with GTK in the future.

  • nathan

    Why not to improve php itself, rather than transforming it into c++?
    Though I appreciate Facebook guys’ work.

    • adelle

      I Would say so ..

  • Robin Varghese

    Wow Amazing approach by facebook

  • sam

    .NET any one?? lol

  • http://www.webcodez.net/ webcodez

    Sounds interesting, go Facebook – nice post.

  • http://www.signpostdesign.co.uk Charlie Harvey

    well done facebook

  • http://varaible3.com Harsha M V

    nice… lets explore it

  • Arun

    Nice work !!!

  • http://www.doopromotion.com/ doopromotion

    Hip Hop can run on Apache2.2? I want to use it for improve my app. Thanks for share about information.

  • http://www.buycheapreviews.com/ Buy Cheap Products Reviews

    Cool technology… Thanks for sharing.

  • http://www.qualitystorefixtures.com John@Store Fixtures, Retail Fixtures & Store Displays

    Cheers for facebook

  • http://www.capitalpawn.co.za John @ Capital Pawn Shop

    Haha. Now ASP.Net has some competition. ASP.Net always used the excuse that their source code is compiled so it runs faster. Now PHP has come back fighting thanks to Facebook.