One of the key values at Facebook are to move fast. For the past six years, we had been able to accomplish a IoT thanks to rapid pace of development that PHP offers. As a programming language, PHP is simple. Simple-to-learn, simple-to-write, simple-to-read, and simple-to-debug. We is able to get new engineers ramped on Facebook a lot faster with PHP than with other languages, which allows us to Innovate faster.
Today I ' m excited to share the project a small team of amazing people and I had been working on for the past II years; HipHop for PHP. With HipHop we ' ve reduced the CPU usage on our WEB servers on average by about fifty percent, depending on the page. Less CPUs means fewer servers, which means less overhead. This project had had a tremendous impact on Facebook. We feel the Web at large can benefit from HIPHOP, so we is releasing it as open source this evening in hope that it bring s a new focus toward scaling large complex websites with PHP. While HipHop have shown us incredible results, it's certainly not complete and you should is comfortable with beta software Before trying it out.
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 Eva L ()-in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP ' s runtime system, and a rewrite of many common PHP Extension s to take advantage of these performance optimizations.
Scaling PHP as a Scripting Language
PHP ' s roots is those of a scripting language, like Perl, Python, and Ruby, all of which has major benefits in terms of P Rogrammer productivity and the ability to iterate quickly in products. This is compared to more traditional compiled languages like C + + and interpreted languages like Java. On the other hand, scripting languages is known to generally is less efficient when it comes to CPU and memory usage. Because of this, it's been challenging to the scale of Facebook-to-over-billion php-based page views every month.
One common to address these inefficiencies are to rewrite the more complex parts of your PHP application directly in c+ + as PHP Extensions. This largely transforms PHP to 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 is able to work on your Entire application. Learning C + + is only the first step to writing PHP Extensions, and the second is understanding the Zend APIs. Given that we engineering team is relatively small-there be over one million users to every engineer-we can ' t afford To make parts the codebase less accessible than others.
Scaling Facebook is particularly challenging because almost every page view are a logged-in user with a customized Experien Ce. When your view your home page We need to look up all of the Your friends, query their most relevant updates (from a custom serv Ice we ' ve built called Multifeed), filter the results based on your privacy settings, then fill out the stories with Comme NTS, photos, likes, and all of the rich data that people love is about Facebook. All of the This in just under a second. HipHop allows us to write the logic this does the final page assembly in PHP and iterate it quickly when relying on custo M Back-end Services in C + +, Erlang, Java, or Python to service the News Feed, search, Chat, and other core parts of the SI Te.
Since we ' ve thought about a few different ways to solve these problems and has even tried implementing a few of them . The common suggestion is to just rewrite Facebook in another language, but given the complexity and speed of development O F The site this would take some time to accomplish. We ' ve rewritten aspects of the Zend engine-php ' s internals-and contributed those patches back into the PHP project, BU T ultimately haven ' t seen the sort of performance increases that is needed. HipHop ' s benefits is nearly transparent to our development speed.
Hacking up HipHop
One night at a Hackathon a few years ago (see Prime time Hack), I started my first piece of code transforming PHP into C + + . The languages is fairly similar syntactically and C + + drastically outperforms PHP when it comes to both CPU and memory us Age. Even PHP itself is written in C. We knew that it is impossible to successfully rewrite a entire codebase of this size by hand, but wondered what would ha Ppen If we built a system to do it programmatically.
Finding new ways to improve PHP performance isn ' t a new concept. At run time, the Zend Engine turns your PHP source into opcodes which is then run through the Zend Virtual machine. Open source projects such as APC and Eaccelerator cache This output and is used by the majority of PHP powered websites. There ' s also Zend Server, a commercial product which makes PHP faster via opcode optimization and caching. Instead, we were thinking about transforming PHP source directly into C + + which can then is turned into native machine cod E. Even compiling PHP isn ' t a new idea, open source projects like Roadsend and PHC compile PHP to C, Quercus compiles PHP To Java, and Phalanger compiles PHP to. Net.
Needless to say, it took longer than this single Hackathon. Eight months later, I had enough code to demonstrate it's indeed possible to run faster with compiled code. We quickly added Iain Proctor and Minghui Yang to the team to speed up the pace of the project. We spent the next ten months finishing up all the coding and the following six months testing on production servers. We is proud to say in this point, we is serving over 90% 's our WEB traffic using HIPHOP, all only six months after Deployment.
How HipHop Works
The main challenge of the project was bridging the gap between PHP and C + +. PHP is a scripting language with dynamic, weak typing. C + + is a compiled language with static typing. While PHP allows your to write magical dynamic features, the most PHP is relatively straightforward. It's more likely that's see if (...) {...} else {:} than it is to see function foo ($x) {include $x;}. This is the where we gain in performance. Whenever possible our generated code uses static binding for functions and variables. We also use type inference to pick the most specific type possible for our variables and thus save memory.
The transformation process includes three main steps:
Static analysis where we collect information on the WHO declares what and dependencies,
Type inference where we choose the most specific type between C + + scalars, String, Array, classes, Object, and Variant, an D
Code generation which for the most part was a direct correspondence from PHP statements and expressions to C + + statements A nd expressions.
We have also developed HPHPI, which are an experimental interpreter designed for development. When using the hphpi you don ' t need to compile your PHP source code before running it. It's helped us catch bugs in HipHop itself and provides engineers A is the use HipHop without changing how they write PHP.
Overall HipHop allows us to keep the best aspects of PHP while taking advantage of the performance benefits of C + +. In total, we have written through 300,000 lines of code and more than 5,000 unit tests. All of this is released this evening on GitHub under the open source PHP license.
Learn more This Evening
This evening we ' re hosting a small group of developers to dive deeper into HipHop for PHP and would be streaming this tech Talk Live. Check back here around 7:30pm Pacific time if you ' d like to watch.
As I ' m sure there'll be plenty of questions, starting this evening take a look at the HipHop wiki or join the HipHop Dev Eloper Mailing list. You'll also find us at FOSDEM, scale, PHP UK, Confoo, TEK X, and OSCON over the next few months talking about HipHop for P Hp. We ' re very excited to evolve HipHop in a thriving open source project along with all of your.
http://www.bkjia.com/PHPjc/440212.html www.bkjia.com true http://www.bkjia.com/PHPjc/440212.html techarticle One of the key values at Facebook are to move fast. For the past six years, we had been able to accomplish a IoT thanks to rapid pace of development that PHP offers. As a programmi ...