You are on page 1of 45

Technology Tasting

Wine, Cheese, and a Taste of New Technology

February 2nd, 2010
Sharing innovations with the community
Load Balancer (assigns a web server)

Web Server (PHP assembles data)

Services (fast, complicated) Memcached (fast, simple) Databases (slow, persistent)
Facebook the data is interconnected
Bob Beth Erin

Load Balancer (assigns a web server)

Web Server (PHP assembles data)

Services (fast, complicated) Memcached (fast, simple) Databases (slow, persistent)
PHP at Facebook
PHP is simple
▪ Simple to learn: small set of
expressions and statements
▪ Simple to write: loose typing
and universal "array"
▪ Simple to read: similar syntax
to C++ and Java
▪ Simple to debug: no need to

Hello World!
Problems we encounter using PHP at scale
1) High CPU usage
Problem 1: High CPU
▪ Tens of thousands of web servers
CPU by Language
▪ Up to 800ms for each request
▪ Slower as the codebase 32

Unit of Time
continues to grow 24
▪ Hardware isn’t free 8
C++ Java C# Erlang Python Perl PHP
CPU Usage
2) High memory usage
Problem 2: High Memory

for ($i = 0; $i < 1000000; $i++) {
$a[] = $i;

for ($i = 0; $i < 5000000; $i++) {
$a[] = $i;

(700M – 150M) / 4,000,000 = 144 BYTES
3) Reuse of PHP logic in other systems
Problem 3: Reuse of PHP logic in other systems

HTML AJAX API C++ & Python

Display Modules Application Logic / Data Modules

Infrastructure Modules
4) Extensions are hard to write for
most PHP developers
PHP is problematic for Facebook

1 High CPU usage

2 High memory usage

3 Reuse of PHP logic in other systems

4 Extensions are hard to write for most PHP developers
How can we solve these problems?
Solutions considered since 2007
1.Rewriting our million+ line PHP codebase to perform better
▪ but how do we maintain it with new hires?
2.Moving complex logic from PHP into PHP Extensions (C++)
▪ “move fast” is important to us
3.Rewrite aspects of the Zend Engine itself
▪ we have been optimizing PHP internals and contributing patches back
▪ already highly optimized
HipHop is a source code transformer
HipHop transforms PHP into highly
optimized C++
HipHop transforms PHP into highly
optimized C++ and uses g++ to compile it
HipHop executes the source code in
a semantically equivalent manner
HipHop sacrifices some rarely used
features in exchange for performance
Web: 50% less CPU with equal traffic

API: 30% less CPU with 2x traffic
Our deployment of HipHop

0% 90%

six months
How HipHop works
HipHop Source Code

Functionality Location Lines of code
Core Runtime cpp/base 60,000
Extension Functions cpp/ext 100,000
Parser and Static Analysis lib 45,000
Utility Functions util 30,000
Unit Tests test 37,000
misc 38,000
Total Lines 310,000
Two phases

1 Code transformation

2 Runtime
1) Code Transformation
$x = 1;

if (...) {...} else {...}
f(1, 2, 3);

for ($i = 0; $i < $n; $i++) {...}

$$x = $$y;

eval($x . $y);

function foo($x) { include $x; }
Optimization Strategy
▪ We can greatly speed them up:
▪ static function calls
▪ static class methods and properties Magic
▪ static variable lookups
▪ g++ -o3 optimizations: function inlining, etc.
▪ C++ won’t give us that much advantage:
▪ dynamic function calls: jump table
▪ dynamic variable lookups: pre-hashing
Transformation Process
1.Static analysis
• Collect information on who declares what, dependencies, etc.
2.Type Inference
• Pick the most specific type for every variable possible:
• C++ scalars, String, Array, classes, Object and Variant
• Type hints
3.Code Generation
• For the most part a direct correspondence from PHP statements and
expressions to C++ statements and expressions.
Transformation Process

Static Pre- Post-
Parser Analyzer Optimizer
Code g++
Engine Generator
2) Runtime
Runtime vs Generation
Programming with HipHop
Supported magical PHP features
▪ dynamic function call, including call_user_func()
▪ dynamic object properties and methods
▪ dynamic variables, including extract()
▪ dynamic includes
▪ re-declared functions
▪ re-declared classes
▪ re-declared constants
▪ magic methods: __toString(), __get(), __set(), __call()
Features not supported
1.Dynamic coding
• eval()
• create_function()
• preg_replace when using /e
2.Order-dependent symbol lookups: function, class, constant

if (function_exists(‘foo’)) {
print ‘foo missing’; // side-effect

function foo() {
HPHPi – The experimental interpreter
▪ Make it easy for developers and don’t change their process
▪ HPHPi is an experimental interpreter
▪ Wanted an interpreter to help catch bugs in our implementation
▪ More runtime checks
▪ eval() support
▪ It sounded fun
▪ Innovation: You don’t have to compile your PHP to run it! <applause>
Deploying HipHop in production
▪ Pre-compiled binary versus PHP source code
▪ HipHop is different...
▪ Runs as one process with multiple threads
▪ No downtime during restarts (port takeover)
▪ Pushing a large binary
▪ We spread our compile across multiple machines

HipHop currently uses its own very simple web server
▪ Catch up with PHP 5.3 (currently on 5.2)
▪ provides some language changes around
stronger typing
▪ Multi-threading support
▪ Support Apache as a web server option
▪ Evolve based on usage outside of Facebook

Want to minimize differences between PHP and HipHop
Thanks for coming!
(c) 2010 Facebook, Inc. or its licensors.  "Facebook" is a registered trademark of Facebook, Inc. All rights reserved.