hiphop for php tech tasting

Post on 18-Nov-2014

18.230 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Technology TastingWine, Cheese, and a Taste of New Technology

February !nd, !"#"

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 interconnectedBob ErinBeth

Servers

Memcached (fast, simple) Databases (slow, persistent)

Load Balancer (assigns a web server)

Web Server (PHP assembles data)

Services (fast, complicated)

PHP at Facebook

PHP is simple▪ Simple to learn: small set of

expressions and statements

▪ Simple to write: loose typingand universal "array"

▪ Simple to read: similar syntaxto C++ and Java

▪ Simple to debug: no need to re-compile

Hello World!

Problems we encounter using PHP at scale

!) High CPU usage

Problem !: High CPU▪ Tens of thousands of web servers

▪ Up to $""ms for each request

▪ Slower as the codebase continues to grow

▪ Hardware isn’t free"

$

#%

!&

'!

&"

C++ Java C# Erlang Python Perl PHP

CPU by Language

Un

it o

f Tim

e

CPU Usage

http://shootout.alioth.debian.org/u!"q/benchmark.php?test=all&lang=all

") High memory usage

!#$MB

%$$MB

(%$$M – !#$M) / &,$$$,$$$ = !&& BYTES

Problem ": High Memory

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

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

!) Reuse of PHP logic in other systems

Problem !: Reuse of PHP logic in other systems

HTML AJAX API C++ & Python

Display Modules Application Logic / Data Modules

Infrastructure Modules

&) Extensions are hard to write for most PHP developers

! High CPU usage

" High memory usage

# Reuse of PHP logic in other systems

$ Extensions are hard to write for most PHP developers

PHP is problematic for Facebook

How can we solve these problems?

Solutions considered since "$$%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: #$' less CPU with equal traffic

API: ($' less CPU with "x traffic

Our deployment of HipHop

90%0%

six months

How HipHop works

Functionality Location Lines of code

Core Runtime

Extension Functions

Parser and Static Analysis

Utility Functions

Unit Tests

misc

Total Lines

cpp/base %","""

cpp/ext #"","""

lib &(,"""

util '","""

test '),"""

'$,"""

'#","""

HipHop Source Code

! Code transformation

" Runtime

Two phases

!) Code Transformation

$x = 1;

if (...) {...} else {...}

f(1, 2, 3);

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

$$x = $$y;

eval($x . $y);

$$$$$foo();

function foo($x) { include $x; }

Mundane

Magic

Optimization StrategyMundane ▪ We can greatly speed them up:

▪ static function calls

▪ static class methods and properties

▪ static variable lookups

▪ g++ -o' optimizations: function inlining, etc.

Magic ▪ C++ won’t give us that much advantage:

▪ dynamic function calls: jump table

▪ dynamic variable lookups: pre-hashing

Mundane

Magic

Transformation Process1.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.

Parser Static Analyzer

Pre-Optimizer

Type Inference

EnginePost-

OptimizerCode

Generatorg++

Transformation Process

") 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 supported1.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

Roadmap

Roadmap▪ Catch up with PHP (.' (currently on (.!)

▪ 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

(c) !"#" Facebook, Inc. or its licensors. *"Facebook" is a registered trademark of Facebook, Inc. All rights reserved.

top related