hiphop for php tech tasting

45
Technology Tasting Wine, Cheese, and a Taste of New Technology February nd,

Upload: david-recordon

Post on 18-Nov-2014

18.230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: HipHop for PHP Tech Tasting

Technology TastingWine, Cheese, and a Taste of New Technology

February !nd, !"#"

Page 2: HipHop for PHP Tech Tasting

Sharing innovations with the community

Page 3: HipHop for PHP Tech Tasting

Load Balancer (assigns a web server)

Web Server (PHP assembles data)

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

Page 4: HipHop for PHP Tech Tasting

Facebook the data is interconnectedBob ErinBeth

Servers

Page 5: HipHop for PHP Tech Tasting

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

Load Balancer (assigns a web server)

Web Server (PHP assembles data)

Services (fast, complicated)

Page 6: HipHop for PHP Tech Tasting

PHP at Facebook

Page 7: HipHop for PHP Tech Tasting

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!

Page 8: HipHop for PHP Tech Tasting

Problems we encounter using PHP at scale

Page 9: HipHop for PHP Tech Tasting

!) High CPU usage

Page 10: HipHop for PHP Tech Tasting

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

Page 11: HipHop for PHP Tech Tasting

") High memory usage

Page 12: HipHop for PHP Tech Tasting

!#$MB

%$$MB

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

Problem ": High Memory

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

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

Page 13: HipHop for PHP Tech Tasting

!) Reuse of PHP logic in other systems

Page 14: HipHop for PHP Tech Tasting

Problem !: Reuse of PHP logic in other systems

HTML AJAX API C++ & Python

Display Modules Application Logic / Data Modules

Infrastructure Modules

Page 15: HipHop for PHP Tech Tasting

&) Extensions are hard to write for most PHP developers

Page 16: HipHop for PHP Tech Tasting

! 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

Page 17: HipHop for PHP Tech Tasting

How can we solve these problems?

Page 18: HipHop for PHP Tech Tasting

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

Page 19: HipHop for PHP Tech Tasting
Page 20: HipHop for PHP Tech Tasting

HipHop is a source code transformer

Page 21: HipHop for PHP Tech Tasting

HipHop transforms PHP into highly optimized C++

Page 22: HipHop for PHP Tech Tasting

HipHop transforms PHP into highly optimized C++ and uses g++ to compile it

Page 23: HipHop for PHP Tech Tasting

HipHop executes the source code in a semantically equivalent manner

Page 24: HipHop for PHP Tech Tasting

HipHop sacrifices some rarely used features in exchange for performance

Page 25: HipHop for PHP Tech Tasting

Web: #$' less CPU with equal traffic

API: ($' less CPU with "x traffic

Page 26: HipHop for PHP Tech Tasting

Our deployment of HipHop

90%0%

six months

Page 27: HipHop for PHP Tech Tasting

How HipHop works

Page 28: HipHop for PHP Tech Tasting

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

Page 29: HipHop for PHP Tech Tasting

! Code transformation

" Runtime

Two phases

Page 30: HipHop for PHP Tech Tasting

!) Code Transformation

Page 31: HipHop for PHP Tech Tasting

$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

Page 32: HipHop for PHP Tech Tasting

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

Page 33: HipHop for PHP Tech Tasting

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.

Page 34: HipHop for PHP Tech Tasting

Parser Static Analyzer

Pre-Optimizer

Type Inference

EnginePost-

OptimizerCode

Generatorg++

Transformation Process

Page 35: HipHop for PHP Tech Tasting

") Runtime

Page 36: HipHop for PHP Tech Tasting

Runtime vs Generation

Page 37: HipHop for PHP Tech Tasting

Programming with HipHop

Page 38: HipHop for PHP Tech Tasting

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()

Page 39: HipHop for PHP Tech Tasting

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() {}

Page 40: HipHop for PHP Tech Tasting

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>

Page 41: HipHop for PHP Tech Tasting

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

Page 42: HipHop for PHP Tech Tasting

Roadmap

Page 43: HipHop for PHP Tech Tasting

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

Page 45: HipHop for PHP Tech Tasting

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