HipHop for PHP: More Optimizations for Efficient Servers

by Xin Qi on Wednesday, March 30, 2011 at 1:18pm ·

Facebook switched all its production servers to HipHop in early 2010, also releasing the project’s source code at that time. At the time of the switch, HipHop reduced our average CPU usage by 50%, the six months after its release saw an additional 1.8x performance improvement, and in the past six months the team in conjunction with the open source community has made an additional 1.7x improvement.

 

The following chart shows the relative throughput improvements on our Web servers over the past several months (excluding the impact of our product changes):

 

Throughput is one of the most important metrics of server efficiency. It is defined as the average rate of Web requests successfully served over a given period of time. Therefore, the work over the past several months has resulted in HipHop being able to serve roughly 70% more traffic on the exact same hardware infrastructure.

 

The performance improvement is achieved through various optimization techniques in the HipHop compiler and the run-time system including:

 

Almost-Serialization-Free APC

HipHop comes with an implementation of Alternative PHP Cache (APC), which uses Intel Threading Building Blocks for a low lock contention. Semantically, an object is serialized and unserialized when it is stored into and fetched from APC. However, serialization and unserialization are costly operations, commonly dominating the cost of APC data fetching itself. Thus, we reworked the APC implementation in HipHop, getting rid of almost all the serialization/unserialization operations, while keeping the semantics equivalent to before.

 

Faster Serialization and JSON Encoding

Although we got rid of most serialization cost in APC, objects still need to be serialized or JSON-encoded in order to transfer them over the wire. To make things faster, we optimized various aspects of these operations, including UTF8/UTF16 conversions, object property accesses, number parsing, and so forth.

 

Less Reference Counting

In PHP, every string, array, or object has a reference count. The count is incremented when a variable is bound to the value, and decremented when the variable goes out of scope or gets bound to something else. It may sound surprising that such simple operations account for a sizable chunk of the CPU time, but they actually do as they are exercised a lot. We implemented several compiler and run-time optimizations to avoid reference counting when we can infer it is not necessary.

 

More Compact Binary Code

Facebook has a huge amount of PHP code, which is compiled by HipHop into a large executable, and deployed to our Web servers. Because of the sheer size of the executable, instruction cache misses become a significant factor in Web server performance. Therefore, reducing the size of the generated binary, especially on hot execution paths, could often lead to improved performance. We consolidated and even uninlined various parts of the generated code, including stack frame management, memory allocation, and method invocation, to make the executable smaller and faster.

 

Improved Memory Allocation

jemalloc, a more performant malloc implementation, has helped improve our ability to monitor and to profile memory usage in HipHop. With its help we identified and optimized several places where large chunks of memory were frequently allocated and deallocated.

 

Faster Compilation

Besides optimizing the compiled code, a lot of effort has been spent on improving the compiler itself. Several phases in the compiler, including parsing, optimization, and code generation, are now parallelized. Hyves contributed changes to the generated C++ code to make it compile faster without losing any run-time efficiency. We can build a binary that is more than 1GB (after stripping debug information) in about 15 min, with the help of distcc. Although faster compilation does not directly contribute to run-time efficiency, it helps make the deployment process better.

 

It has been a wonderful and rewarding experience working on improving server efficiency. It makes Facebook and everyone else using HipHop scale better with more responsive user experiences.

· Comment · Share
    • Tom de Vries Nice to read this and impressive that you guys can continue improving the performance. Keep it up!
    • Matt Falcon
      First of all, WOW, that's a lot of PHP. I guess when you're pretty much the biggest, or second-biggest, website on the entire internet (next to Google if at all)... you're gonna have a lot of code. Perhaps just spread across a lot of server...s, but still. Hugely impressive.

      Second, now that your servers aren't on fire, how about investing some dev resources into fixing the buggy user experience? Hundreds of thousands of FB users are constantly complaining about stupid changes like "press enter to post comment" (I habitually hit enter to insert a new line, as above, and this will NEVER change for me)... or the buggy "theater" photo viewer... or forcing download-to-view-high-resolution photos... or the "stops responding on first click" issues with Google Chrome? To us users, it looks like Facebook isn't doing any dev work at all (although I love the new "Questions" thing, but that's all I've seen)! I love hearing about the back-end work, but there's gotta be some front-end cleanup as well...
      See More
    • Matt Falcon Oh, and don't even get me started on "Facebook for Android". Man, you guys just have to gut that whole app and start over. Need some advice? I'd be happy to provide some free "this is what everyone is whining about" consultation on that thing. Just so I could have a more usable FB app. ;)
    • James Flamish ‎@zakaria dude 1gb is not that much nowadays memory wise, heck one Teribyte isn't even that much anymore
    • Jonathan Hassall It was fun when HipHop broke about 10% of every FBML request for months.
    • Zakaria El Fassi ‎@James: But think of it as a VERY NEAT compiled C++ code ...!
    • Matt Falcon ‎@James: Yeah, you try writing out 1gb of PHP code and let me know how that goes. We're not talking about storage here... :P
    • James Flamish That's still not a lot of code. Look at the time magazine article a couple months ago or look it up online. It shows how big their servers are and how much memory each server holds, it's incredible.
    • James Flamish ‎@matt: all I'm trying to say is that there is a lot more than 1gb of Facebook code out there and it is impressive. I come from the days when 32k was a lot of memory or code
    • Matt Falcon Code != data... Facebook probably stores some untold terabytes of data, of course. But it's highly unlikely that there would be some gigabyte(s) of actual, hand-written, running, CODE, behind it. The servers generate terabytes of output code every day, sure. But I find it hard to imagine an entire gigabyte of code behind all of Facebook! Seriously? A gigabyte of data, I can't argue that. Just not... "code".
    • James Flamish
      Facebook as I have wrote on past articles has to start working on getting rid of these rouge virus's out there on there servers rather than doing so many improvements. I know of 3 people so far that had to have their computers restored beca...use of Facebook virus's and 1 totally rebuilt. It's ridiculous and the sad part is that the Internet teaches u how to do it. Just google "make an Internet virus" and the program is there to download for free and a utube video teaches u how to do it. Only place I havent seen a virus on yet is a apple product, cause 95% of the world runs pc's and that's who the hackers are after now.See More
    • Luke Adlam Why don't you people commenting with bugs try putting them in the bug tracker instead?
    • Ronald Rivas Wooow, c'est genial !!! Une félicitation à toute l'équipe de FaceBook, parce que ils améliorent les technologies comme PHP et HipHop. La réduction d'usage de CPU est une stratégie de succès.
    • James Flamish ‎@Luke cause we shouldn't have to and Facebook won't do anything about it anyhow. I'll try though thanks for the link.
    • James Flamish ‎@Luke well I tried it but I'm not a developer so it just directed me to the regular Facebook help page which does nothing. I guess maybe if I was a developer it might work. There are like 12 rouge virus's out there now that I know of and regular antivirus does nothing.
    • Luke Adlam So don't use facebook, its not like you have paid for the privilege or are forced into it. Your comments have nothing to do with this post and are just taking up space on what was otherwise an interesting post.
    • James Flamish ‎@Luke I use Facebook for what's it's meant for and was designed for, reuniting with old friends as well as new friends. I just feel sorry for all the people out there who get virus's from theses hackers out there and not even know it till it's to late. Hackers that put virus's out there should go to jail just like any other criminal, don't u agree?
    • Luke Adlam I certainly do, but Facebook is not the police and is in no positionto go putting people in jail, nor is this line of discussion relevant to HipHop for PHP.
    • James Flamish All I'm trying to do is to get the word out to Facebook engineering. Their help section doesn't help and calling them does no good unless u have a police issue then someone gets back to u when u leave a message. If u know of another place to post it let me know besides the developer one which I cant access cause I'm not a developer.
    • Matt Falcon
      Luke, you ignorant... are you freaking kidding me? "So don't use Facebook"? What kind of completely bogus response is that?? We have issues to report, things Facebook is ignoring, when FACEBOOK LIVES ON MAKING ITS USERS HAPPY? Your response... is just to... "not use Facebook"? Sure, let's just walk across the street to the other Facebook-E-Mart, and get our commodity "social interaction" there. No, genius. Facebook is THE Facebook. There is no "just go somewhere else". If Facebook has issues, and we have to use Facebook, we have to live with Facebook's issues. That's why we whine and cry like little babies hoping Facebook will listen and actually fix their problems. I mean, nevermind the fact that Facebook should be interested in fixing bugs, especially ones that users report (they fix enough of 'em in sandbox environments during development - what makes 'em think they should ignore ones that come up in production?). But WTF kind of response is "So don't use facebook"?!See More
    • Dams Ramadhan where is "easy usage" ? :|
    • Luke Adlam
      ‎@Matt my point is that you haven't paid to use Facebook at any point have you? You don't need it to survive, do you? Yes, there are some things the Facebook devs obviously need to fix. If any of you seriously think that complaining about t...hem in a comment on a completely unrelated post will get them fixed, then you are the ignorant one. Maybe you should try something like this page http://www.facebook.com/help/?topic=suggestions instead.See More
    • Rohit Srivastava ‎@wow ...ultimate work on dis project.......thanx to hiphop work team
    • Pesimist Userr Carry on frens its good 2 c u quarrel
    • Gaurish Sharma Where Can I find more information about HipHop and its compatibility with other php based open source project like wordpess or drupal?
    • Jess Summerhill I wonder if anyone is ever going to look into the go programming language:
      http://golang.org/
      Could be useful in something like this.
    • Mark MyClose We need love/like/agree/disagree button to going forward :)
    • Richa Mathur nice..
    • Greg Knapp Can some of these optimisations be put back into PHP? The improved reference counting and optimised APC serialization?
    • Frans Ettiene Terblanche Dear FACEBOOK
      Can u please put the mobile notifications twindow back the way it was? The current method irritates the sht out of me.
      Regards
      FT
      ..........¥
    • James Flamish ‎@matt. Thanks!! At least u know where I'm coming from.
    • Joshua Dickerson Can any of the HipHop improvements be passed back to PHP?
    • Ahmed Mohammed very fast
    • Tom Loftis I am switching to hip-hop PHP source code http://nabacar.com/