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
10  
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
6  
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
show 1 more comment

6 Answers

“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
add comment

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
add comment

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
2  
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
6  
@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
add comment

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
6  
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
add comment

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
add comment

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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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