Notes from Facebook's HipHop for PHP Debut

Posted Feb 2, 2010 by Kris Jordan | Comments ( 2 ) | Filed in: PHP | | | | |

This evening the developers behind HipHop dove into more depth on the technology over a Live UStream cast. In case you missed it, here are my notes from the presentation.

Performance compared to PHP with APC opcode cache turned on:

  • 30% less CPU time serving traffic in API usage (shorter requests pulling data from fewer sources)
  • 50% less CPU time serving traffic in WEB page rendering usage

PHP, being a scripted language, has a certain amount of 'non-magic' code that looks and feels much like static code you might write in C/C++. This code and logic can be compiled down to very fast static function calls, static variable look-ups, static types. This is where HipHop's performance gains come from. The 'magic' / dynamic features of PHP are much harder to optimize and run roughly as fast as they do on the Zend engine. Magic includes:

$$x = $$y;
eval($x . $y);
function foo($x) { include $x; }

There are some tricks that can be applied for this style of dynamic code, things like prehashing dynamic variable look-ups, efficient jump tables, etc.
 "If the code has a lot of dynamicness it will execute at roughly the same speed."

Transformation Process:

1. Static analysis
* Collect info on who declares what, dependencies
2. Type inference
* Pick most specific type for every variable possible
** C++ scalars string array, classes, object, and variant
* Type hints in code are analyzed
3. Code Generation

Facebook hand wrote the runtime libraries for string/array/object classes. Similar to Zend engine's. They rewrote Zend engine because it primarily deals with variant types. In Hip-Hop there are special cases for static types made possible by type inference. Code for "what if this variable is an integer", etc. Another reason to rewrtie the runtime is that once the whole runtime in C++ it should be easier to author extensions based on primitive C++/FaceBook types than their Zend engine, dynamic counter-parts.

"We want people to write PHP like they're used to writing PHP."

Supported magical PHP features:

  • dynamic function call including call_user_func()
  • dynamic object properties and methods
  • dynamic variables, including extract()
  • dynamic includes
  • redeclared functions
  • redeclared classes
  • redeclared constance
  • magic methods __toString(), __get(), __set(), __call()

Features not supported:
1. eval, create function, preg_replace /e
2. "if(function_exists('foo'){print 'foo';})
function foo() {}"

HipHop runs as one process with multiple threads. No downtimes during restarts (port takeover). Uses libevent internally.

In the next month the goal is to bring it up to speed with 5.2.12. After that the focus switches to PHP 5.3+. HipHop may potentially support some of the type suggestion and typing features that did not make the 5.3 cut.

Some Q&A
Do you support old PHP extensions?
We've converted extensions because they have their own datastructures. They've converted a lot of them because facebook's codebase uses a lot of extensions. Keep same logic switching out data structures.

There are some PHP extensions that are not thread-safe, how do we handle them?
We've fixed the problems they had and made them thread-safe. Once the source is opened you will get the full list of supported extensions.

How did you know it "worked".
- We counted the number of function calls and compare with PHP, it has to be identical.
- We compared network traffic between machines running HipHop and running PHP and made sure the in and out were practically identical.
- High enough volume that every machine gets roughly the same kind of traffic making this analysis possible.

Have you tried to support other compilers like Intel's?
We want to try intel's with great support for memcopy.

Why did you choose not to go the just-in-time / LLVM route?
So V8 has great optimization over Javascript but it was not available at the time we started the project. We absolutely considered LLVM. Subconciously I was thinking about having a tool that automatically converts code to C++. We may be able to borrow some of the optimization techniques being used by V8.



This is is really exciting technology. Once the source is released we'll be working out the details on how to compile Recess on HipHop as soon as possible.


Comments are moderated. It may take some time for your comment to appear.

Recess can only be as good as the thoughts that go into it. Let us hear yours...

  1. Posted by Matt Terenzio on Feb 3, 2010
    Actually, I think it was:

    30% less CPU time serving traffic in API usage (shorter requests pulling data from fewer sources) AT TWICE THE TRAFFIC

    In other words the savings on the API were higher than regular web because of the shorter requests
  2. Posted by Systrix on Feb 3, 2010
    HipHop seems really interesting, we have had to switch to lighttpd and xcache to achieve huge performance benefits for our objectCMS framework which is now faster that wordpress, joomla and drupal. I am wondering how hiphop compares to lighttpd and xCache, does anyone know when hiphop is going to be released to the public.

You must login to comment on this page.