I read that Facebook started out in PHP, and then to gain speed, they now compile PHP as C++ code. If that's the case why don't they:

  1. Just program in c++? Surely there must be SOME errors/bugs when hitting a magic compiler button that ports PHP to c++ code , right?

  2. If this impressive converter works so nicely, why stick to PHP at all? Why not use something like Ruby or Python? Note -- I picked these two at random, but mostly because nearly everyone says coding in those languages is a "joy". So why not develop in a super great language and then hit the magic c++ compile button?

  • 14
    Both your alternatives would likely mean throwing out all the PHP code, PHP-specific tools and expertise, half of the supporting infrastructure, etc. that's already there and starting from scratch.
    – user7043
    Nov 19, 2012 at 18:54
  • Why? If you can convert code to C++, surely anyone can use their favorite language, hit a convert button, and have it get committed to the C++ Codebase. No?
    – user72245
    Nov 19, 2012 at 21:02
  • 7
    No. Compilers, by and large, produce working but ugly and unnatural code, and strip things like variable names, comments, not to mention all kinds of abstractions. To a large degree, this is inevitable. While there are some projects aiming to actually translating into a maintainable codebase in another language, the problem is much harder, especially with widely different languages. Also, even assuming perfectly idiomatic C++ comes out, everyone working on the codebase would have to learn C++ or be fired and replaced with people who know C++. And then you still haven't addressed tooling.
    – user7043
    Nov 19, 2012 at 21:13
  • 1
    Also (I just discovered this myself, but it's in line with my gut feeling and my experience with other dynamic language implementations), note that the PHP-to-C++ compiler is being phased out and replaced with a bytecode interpreter + JIT called HHVM (developed later as part of the same umbrella project) which massively outperforms it and has less restrictions. See github.com/facebook/hiphop-php/wiki
    – user7043
    Nov 19, 2012 at 21:17
  • @Delnan: Bad compilers produce ugly and unnatural code. But it's hardly inevitable. Have a look at Smart, which compiles down to JavaScript. The output is very readable, unless you turn on obfuscation and/or minification of course. <snark> (Insofar as JS can ever be called "readable", that is.) </snark> Nov 20, 2012 at 0:20

6 Answers 6


They don't. Not anymore, at least. Turns out doing it that way causes too many problems, including deployment headaches and nullifying one of the prime advantages of using a scripting language in the first place--being able to change scripts without needing to recompile--so they revamped the HipHop system into a VM architecture with a transparent JIT phase, and deprecated the C++ compiler.

Interestingly enough, apparently doing it this way is also about twice as fast (as in performant) as the original C++ trans-compilation approach.

  • 4
    All I get from this is that Facebook are having a difficult time balancing the business needs against developer capabilities. Interesting all the same, although I'd add that getting better performance from a JIT solution over a native one just means their PHP->C++ jiggerypokery was in fact pants.
    – James
    Nov 20, 2012 at 14:47
  • 7
    @James While I doubt "HipHopc" was the greatest optimizing compiler ever, that particular result does not show that they suck at writing compilers, it just shows that static compilation of dynamic languages is much less effective than dynamic compilation. Which was found to be true repeatedly, by people who definitely know how write optimizing compilers. A JIT compiler can perform a wealth of optimizations easily. An AOT compiler (without very expensive whole-program analysis) can rarely do much more than remove the overhead of the interpretation itself, without actually removing dynamicness.
    – user7043
    Nov 20, 2012 at 15:13
  • 2
    @delnan Well, yes, if you cripple the prime benefit of an AOT compiler (whole program analysis) by pointing out the entire point of an AOT compiler (having a lot of time to do analysis), then sure, it won't compare to a JIT doing what a JIT is good at (quick peekhole optimizations). But it's hardly fair, now is it?
    – Alice
    Nov 16, 2014 at 15:33
  • 2
    @delnan This is simply not true, or at least intellectually dishonest. A JIT has extremely limited time compared to an AOT to do optimizations; Java wrote papers on register allocators that are less than ideal, but fast enough for JIT use. Using SSA allows for enormous amounts of optimizations for free that most JIT's struggle to keep up with. AOT's can use proven type inference algorithms (Hindley–Milner and algorithm W) which are not complicated, while a JIT absolutely does not get that for free, paying costs in terms of memory. A JIT can do some optimizations better, and so can an AOT.
    – Alice
    Nov 22, 2014 at 10:58
  • 1
    @Alice We're talking about highly dynamic languages. There are no simple and effective AOT (i.e. static) type inference algorithms for languages like Python or JavaScript. There are complicated on-line/run-time algorithms (as used in SpiderMonkey for example) that are effective, and there are complicated AOT algorithms (e.g. Starkiller) which so far failed to prove themselves effective. Algorithm W does not even begin to address the complexities of dynamic languages.
    – user7043
    Nov 22, 2014 at 11:41

Facebook's Senior Engineer Haiping Zhao probably answers your questions best.

  1. 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.

  2. One common way to address these inefficiencies is to rewrite the more complex parts of your PHP application directly in C++ as PHP Extensions. This largely transforms PHP into a glue language between your front end HTML and application logic in C++. From a technical perspective this works well, but drastically reduces the number of engineers who are able to work on your entire application.

The rest of the blog post is a good read, and I recommend it. It gives some insight into the programming challenges Facebook deals with, and how they are trying to address those problems.

  • 7
    Note that this is obsolete; that was their first try, but Facebook no longer does it this way. See my answer, below. Nov 20, 2012 at 0:18
  • @MasonWheeler - great link and update.
    – user53019
    Nov 20, 2012 at 0:19

Just program in c++? Surely there must be SOME errors/bugs when hitting a magic compiler button that ports PHP to c++ code , right?

Right, but programming in C++ would entail replacing their entire existing codebase- an idea world-renowned for being utterly stupid and devastating.

If this impressive converter works so nicely, why stick to PHP at all? Why not use something like Ruby or Python? Note -- I picked these two at random, but mostly because nearly everyone says coding in those languages is a "joy". So why not develop in a super great language and then hit the magic c++ compile button?

Because that would, again, entail replacing their existing PHP codebase.

In an ideal world, they would simply code in C++ from scratch. Unfortunately, because they have a shitload of existing code in PHP, that's not possible. So instead, they hack around the problem. It's just so much cheaper.

  • 2
    +1 for this: "So instead, they hack around the problem. It's just so much cheaper." Its true - if they have 3500 engineers working on their product, its way cheaper to get a small team of 5-50 people focussed on writing a good PHP->C++ compiler, than to have the entire engineering team rewrite 6 years worth of code.
    – Suman
    Nov 19, 2012 at 19:54
  • Sorry, I'm confused. Why would they have to rewrite it. You just just said it youreself - HipHop converts all of the code to C++. So just convert it, then stick in C++.
    – user72245
    Nov 19, 2012 at 21:00
  • 16
    @user72245 just because it converts it to C++ doesn't mean it converts it to readable or maintainable C++
    – Mr.Mindor
    Nov 19, 2012 at 21:09
  • Why is this they hack around the problem? Optimizing code using C++ or even assembly isn't new at all, been doing it since before there was a PC.
    – Steve
    Nov 19, 2012 at 23:56
  • also bear in mind the Facebook programmers are PHP programmers. Sure you could convert it all over to C++ and start programming in C++, but your existing programmers have no experience in this language. You would need to retrain them, or hire new programmers in order to continue development. Nov 20, 2012 at 12:27

“Indeed, why not work in assembly directly, since the C++ code is ultimately translated into machine code instructions?”

– That, in its essence, is what the argument reduces to. And hopefully this makes it obvious why it isn’t done:

  • A (vastly!) different skill set is required to program in assembly (C++) than in C++ (PHP).
  • It’s potentially much harder to program, for a variety of reasons
  • The code produced by a assembler/compiler may not be human-readable (speak: maintainable), even though you can, from scratch, write readable programs in assembly (C++).
  • 2
    I once maintained an insurance application written in assembly conceived in the 1970s. In October, I was tasked to change the greetings on a "letter" to say the equivalent of "Happy Holidays". It was only completed in February the next year due to complexity. I became very proficient in assembly and could write optimal code, provided it wasn't more than a couple of thousands of lines. However, the COBOL and C compilers kicked my ass and produced way more optimal code for the platform we were running on, especially for systems exceeding 1m lines of assembly. It makes no business sense.
    – bloudraak
    Apr 13, 2015 at 3:28

I am not at Facebook, but my best guess at the motives would be "to avoid significant risks". At this point, switching to a different language is no longer a technology decision: above all, it is a business decision.

When you are a big company that grew organically to the size of FB, you slowly attract people who then gain expertise in your programming platform (in FB's case, that's PHP). One by one, you get a few thousand employees with great expertise at PHP. At this point, switching to any other language becomes very dangerous: your engineers will not be up to speed on the new ecosystem, and may require a significant time to achieve the level of expertise demanded by their current jobs, let alone improving their skills.

Leaving aside the relative merits of PHP and the alternative languages, with the amount of investment the FB made into PHP technology it would be too arrogant to think that a switch would be painless, and too foolish to give it a try. In business, technology is means to an end, so the "joy" of programming does not even enter discussions.


I can think of only one major website which was implemented in C++. H2G2

Even then the current implement ion is actually an interpreter with a large number of text and database manipulation functions built in (does that not sound a bit like and early PHP :-) ).

Facebook is quite happy with the functionality of their website. They have just grown to the point where vanilla PHP cannot support the volumes they process. Hence the compiling of there PHP into C++ thence machine code. The could have written a full compiler for PHP, but they would have missed out on the 20 years of subtle optimization that have gone into the gcc compiler stack. The point is the "C++" code is not meant to be human readable or maintainable its just an intermediate step on the way to machine code.

Like many programmers on this site I feel you undervalue the amount of work invested in hte business logic and functionality embedded in existing applications, and, value code for its own sake.

  • I can think of dozens, now that WT is successful.
    – Alice
    Nov 16, 2014 at 15:35
  • @Alice -- interesting! But I cannot find anyone using this for a high volume site. Plus hello world 30 something lines of code to do 5 lines of PHP code. Nov 18, 2014 at 7:09
  • Comparing a "hello world" example is sort of ridiculous. In less than 100 lines, I can set up a websockets enabled, long poll fall back, progressively enhanced widget with optimal SEO, automatic clean URLs without full page loads using AJAX, and a tiny CPU/RAM footprint. PHP, at least in typical configurations, cannot do websockets, long poll, clean URLs without help, clean URLs with AJAX at all, and it will definitely use an enormous (comparatively) amount of RAM/CPU. For webapps and not simple examples, WT and C++ are drastically superior, and with C++11, comparable in length.
    – Alice
    Nov 22, 2014 at 10:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.