Channels ▼
RSS

Design

Facebook Releases 'HipHop for PHP'



HipHop for PHP is a Facebook open source project that turns PHP source code into highly optimized C++, then compiles it with g++ to build binary files. You keep coding in simpler PHP, then HipHop executes your source code in a semantically equivalent manner and sacrifices some rarely used features -- such as eval() -- in exchange for improved performance.

Facebook claims that it sees about a 50% reduction in CPU usage when serving equal amounts of Web traffic when compared to Apache and PHP. Facebook's API tier can serve twice the traffic using 30% less CPU.

According to Facebook senior engineer Haiping Zhao the main challenge with 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 you to write magical dynamic features, most PHP is relatively straightforward. It's more likely that you see if (...) {...} else {..} than it is to see function foo($x) { include $x; }. This is where we gain in performance. Whenever possible our generated code uses static binding for functions and variables. HipHop for PHP also uses type inference to pick the most specific type possible for our variables and thus save memory.

The transformation process includes three main steps:

  1. Static analysis where developers collect information on who declares what and dependencies,
  2. Type inference where they chose the most specific type between C++ scalars, String, Array, classes, Object, and Variant
  3. Code generation which for the most part is a direct correspondence from PHP statements and expressions to C++ statements and expressions.

Facebook has also developed HPHPi, which is an experimental interpreter designed for development. When using 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 way to use HipHop without changing how they write PHP.

Overall Zhao says that HipHop lets Facebook keep the best aspects of PHP while taking advantage of the performance benefits of C++. To date, they've written more than 300,000 lines of code and more than 5,000 unit tests, all of which has been released under the open source PHP license.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video