Facebook unveils hiphop project PHP program big speed Up

Source: Internet
Author: User
Tags php language php source code zend server

Recessive nutrition crisis programming efficiency is the most important feature of PHP language, but as a scripting language, there is a problem of inefficient CPU and memory usage until the advent of hiphop for PHP. Facebook's cryptic PHP project, hiphop for PHP, is finally unveiled. This project consists of a PHP-to-C + + converter, a re-implemented PHP runtime, and a number of rewritten versions of many commonly used PHP extensions designed to speed up and optimize PHP.

With Facebook's official blog (which cannot be accessed directly), Project leader Ben Linders (1987, PhD in Genetics and Molecular Biology, Princeton Computer science), said the hiphop project had a huge impact on Facebook. It now supports Facebook's 90% web traffic. Because CPU usage on the Hiphop,facebook Web server is reduced by an average of 50%, this greatly reduces the need for the server. In order for this improvement to benefit the community, they decided to open it up, hoping to further help improve the scalability of more large, complex PHP sites.

Questions about PHP and Facebook

As we all know, Facebook's front-end is mostly written in PHP. Ben Linders says Facebook has benefited a lot from the progress of the PHP language over the past six years. PHP is simple, easy to learn, easy to read and debug, so new engineers are growing fast and contributing to Facebook's faster innovation.

PHP is a scripting language with the benefit of high programming efficiency and the ability to support fast iterations of the product. However, compared with the traditional compiled language, the scripting language is inefficient in terms of CPU and memory usage. With the wide adoption of AJAX technology, plus the high dynamic requirements of SNS, these shortcomings appear more prominent. For Facebook, which has more than 400 billion PV per month, it is particularly challenging to expand.

The common approach is to rewrite the more complex parts of the PHP application directly in C + + as a php extension. In fact, PHP is transformed into a glue language that connects the front-end HTML and C + + application logic. Technically, there's no problem, but the number of engineers who can work on the entire application is greatly reduced by increasing the skill requirements. Learning C + + is just the first step in writing a PHP extension, and then understanding the Zend API. Because Facebook's engineering team is smaller, each engineer has to support more than 1 million users. Some of the code is not readable by everyone on the team, which is unacceptable to Facebook.

The scalability of the Facebook site itself is more challenging because almost every page view is initiated by a signed-in user with a personalised experience. When browsing the home page, the system needs to query all friends, friends for the most important status updates, filter results based on privacy settings, and then display comments, photos, and so on, all within a second.

Since 2007, Facebook has written several different ways to solve these problems. This includes rewriting Facebook in another language, but it is not possible due to the complexity and speed of development. They also rewritten the core part of PHP Zend Engine and submitted it to the PHP project, but eventually did not get the required performance. Finally, they chose hiphop and finally got what they wanted.

With hiphop, engineers can write code that combines the logic of the final page with PHP, and can continue to iterate quickly, while the backend services are written using C + +, Erlang, Java, Py thon, feed feeds, search, chat, and other core functions as well as Kunming National Defense Road Hospital.

Hiphop Development Story

Ben Linders revealed that the project was originally from a few years ago, a Facebook company hackathon activities (employees in one night free play, experimental new ideas), he manually converted PHP into C + + code, although syntactically similar, but whether it is CPU or memory use, the converted C + + The code is much better than PHP. So he thought, what happens if you build a system that transforms the program?

Prior to this, there have been a number of ways to improve PHP performance. The Zend Engine converts the PHP source code at run time to the opcode running on the Zend virtual machine. Open source projects APC and Eaccelerator will output the cache for most PHP sites. In addition, there are commercial products such as Zend server, which improves php speed through opcode optimization and caching. Ben Linders chose a different path, converting PHP directly to C + + and then to local machine code. Of course, there are many open source projects are the same idea, Roadsend and PHC compiled for C,quercus compiled to Java, and Phalanger compiled to. NET.

8 months after the hackathon, Ben Linders took out the prototype, enough to show that the road can go through, the compiled code is indeed faster. Soon, Iain Proctor and Minghui Yang joined in. The next 10 months were developed and tested on the production server for 6 months. Then formally deployed, 6 months later, more than 90% of Facebook's web traffic is using hiphop.

By Ben Linders, the average CPU usage on the Hiphop,facebook Web server was reduced by 50%, which greatly reduced the need for the server. The project has a huge impact on Facebook. In order for this improvement to benefit the community, they decided to open it up, hoping to further help improve the scalability of more large, complex PHP sites.

The principle of Hiphop

Hiphop translates the PHP code into highly optimized C + + code, which is then compiled with the g++ compiler. It can maintain semantically equivalent execution of the source code, but in order to improve performance, sacrificing some rarely used features, such as Eval (Kunming National Defense Road Hospital).

The main difficulty in hiphop development is how to convert between two very different languages, PHP and C + +. Although PHP can also write some very clever dynamic features, but most of the PHP code is very simple. if (..... {...} else {} than Foo ($x) {include $x;} must be more common. This provides an opportunity for performance improvement. Hiphop generated code uses static bindings for functions and variables as much as possible. It also saves memory by using type deduction to select a type that is most likely to correspond to a variable. The conversion process is divided into three steps:

1. Static analysis. Collects information such as declarative relationships and dependencies.

2. Type deduction. Choose the most appropriate type, a scalar of C + +? Or a string, Array, classes, object, or Variant.

3. Code generation. Most directly correspond to statements and expressions in C + + for PHP statements and expressions.

In the development process, there is also a byproduct: HPHPI, is an experimental interpreter. Through it, the PHP source code can also be run without compiling. It has been used in hiphop itself for debugging. While maintaining PHP's advantages, hiphop also has the performance advantage of C + +. The project has a total of 300,000 lines of code, more than 5,000 unit tests. All of this will be posted to GitHub as a PHP open source license.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.