Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as primarily opinion-based by gnat, Dan Pichelman, MichaelT, Bart van Ingen Schenau, jwenting Oct 6 at 7:38

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

11  
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. –  delnan Nov 19 '12 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 '12 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. –  delnan Nov 19 '12 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 –  delnan Nov 19 '12 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> –  Mason Wheeler Nov 20 '12 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.

share|improve this answer
3  
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 '12 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. –  delnan Nov 20 '12 at 15:13
    
@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 at 15:33
    
@Alice The kind of analysis necessary to put an AOT PHP compiler in the same league as a JIT compiler is expensive (in compile time) even for an AOT compiler and also quite expensive in implementation effort. Fundamentally, an AOT compiler needs to figure out very complicated flow- and data-dependent information (most importantly, the types actually stored in variables and data structures) that a JIT compiler gets for free. That information is the great difference; clearly if an AOT compiler has the same information it can beat a JIT compiler by optimizing more. –  delnan Nov 16 at 16:12
1  
@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 6 hours ago

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.

share|improve this answer
7  
Note that this is obsolete; that was their first try, but Facebook no longer does it this way. See my answer, below. –  Mason Wheeler Nov 20 '12 at 0:18
    
@MasonWheeler - great link and update. –  GlenH7 Nov 20 '12 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.

share|improve this answer
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 '12 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 '12 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 '12 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. –  Stephen Nov 19 '12 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. –  Gavin Coates Nov 20 '12 at 12:27

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.

share|improve this answer

“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++).
share|improve this answer
    
+1, need more upvotes. –  Pacerier Oct 16 at 20:19

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.

share|improve this answer
    
I can think of dozens, now that WT is successful. –  Alice Nov 16 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. –  James Anderson Nov 18 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 6 hours ago

protected by gnat Oct 1 at 15:26

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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