33

HipHop : High-Performance PHP Ali-Reza Adl-Tabatabai HipHop Team Facebook

  • Upload
    mabyn

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

HipHop : High-Performance PHP Ali-Reza Adl-Tabatabai HipHop Team Facebook. Facebook: Move fast & build things. PHP. General-purpose scripting language tailored for web development Interactive Weakly typed & dynamic.

Citation preview

Page 1: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook
Page 2: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop: High-Performance PHPAli-Reza Adl-TabatabaiHipHop TeamFacebook

Page 3: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Facebook: Move fast & build things

3

Page 4: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP• General-purpose scripting language tailored for web

development• Interactive• Weakly typed & dynamic

<?phpvar_dump(6 + 7);var_dump('6' + '7');var_dump('six' + 'seven');var_dump((1<<63) + 0);var_dump((1<<63) + (1<<63));

int(13)int(13)int(0)int(-9223372036854775808)float(-1.844674407371E+19)

4

Page 5: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP

function foo($x) {echo “foo: “. $x. ”\n”;

}foo(“hello”); // prints “foo: hello”foo(10); // prints “foo: 10”

5

• General-purpose scripting language tailored for web development

• Interactive• Weakly typed & dynamic

Page 6: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP

if (...) { class B { ... }} else { class B { ... }}class C extends B { ... }

6

• General-purpose scripting language tailored for web development

• Interactive• Weakly typed & dynamic

Page 7: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP

$a = ‘f’;$b = ‘c’;$c = ‘oo’;$func = $a . $ $b;$func();

7

• General-purpose scripting language tailored for web development

• Interactive• Weakly typed & dynamic

Page 8: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP

class C {public $declProp = 1;

}$obj = new C;$obj->dynProp = 2; // This is OK!echo $obj->declProp . “\n”; // prints “1”echo $obj->dynProp . “\n” // prints “2”

8

• General-purpose scripting language tailored for web development

• Interactive• Weakly typed & dynamic

Page 9: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

PHP

9

• General-purpose scripting language tailored for web development

• Interactive• Weakly typed & dynamic

if (function_exists(‘foo’)) { ...} if (class_exists($c)) { ...}

Page 10: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Memory management

• PHP is reference counted• Precise destruction semantics

10

class C {function __destruct() { echo “bye!”;

}}$x = new C;$x = 1; // prints “bye!”

Page 11: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Concurrency

• Single-threaded programming model• Multiple requests run in parallel• No shared memory, synchronization, or direct

communication

11

Page 12: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Performance...

C++ Java C# Ocaml PHP Ruby Python0

5

10

15

20

25

30

35

40

45

CPU Time

Source: http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=all12

Page 13: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Implications for Facebook

1. Bad performance can limit user features2. Poor efficiency requires lots of re$ource$

INTERNET

webservers

storage

. . .

. . .

13

Page 14: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

What have we done?

Apache

Facebook (PHP)

PHP/Zend Facebook(binary)

14

Page 15: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop compilation flow

15

Facebook(PHP)

hphpc Facebook(C++)

PHP Runtime

Webserver

Gcc

Facebook(binary)

Page 16: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop compiler (hphpc)

16

PHP AST

Optimize, Infer Types

Parser

C++

C++ Code Generator

Variant

Double

Integer

Boolean

String Array Object

Page 17: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Representing PHP data

type data

KindOfString

& “Lorem ipsum.”

KindOfInt 13

Page 18: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Type inference: fewer tags!

data

& “Lorem ipsum.”

13

type

Page 19: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Basic operations

$a + $b

Page 20: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Basic operations: type dispatch

$a + $b

switch (a->m_type) { case KindOfInt: switch (b->m_type) { … } case KindOfArray: switch (b->m_type) { … } … } …}

$a + $b

Page 21: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Type inference: avoiding dispatch

$a + $b given$a :: Int,$b :: Int

add %rcx, %rdx

Page 22: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop compiler: performance

22

C++ Java C# Ocaml PHP Ruby Python0

5

10

15

20

25

30

35

40

45

CPU Time

Disclaimer: estimated based on running Facebook

Page 23: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop compiler: pros & cons

• Good for production servers • Inadequate for development – Solution: the HipHop interpreter (hphpi)

• Leverages HipHop runtime & webserver

• Open problem:

23

Can we get the best of both

worlds?hphpi ≠ hphpc

Page 24: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop Virtual Machine (hhvm)

• Ambitious goal: replace both the HipHop Interpreter and Compiler

24

PHP AST

Optimize, Infer Types

Parser

HHBC

Bytecode Generator

HHVM Interpreter

HHVM JIT

Page 25: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

HipHop bytecode (hhbc)

• In-house design• Stack-base VM• Closer to PHP than

machine code

25

function lookup($cache, $key) { if (isset($cache[$key])) { echo “Hit! “ . $cache[$key]; return true; } else { echo “Miss!”; return false; }}

96: Loc 0101: Loc 1106: IssetM <H E>113: JmpZ 32

118: String “Hit! “123: Loc 0128: Loc 1133: CGetM <H E>140: Concat141: Print142: PopC

143: True144: RetC

145: String “Miss!”150: Print151: PopC

152: False153: RetC

Page 26: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Hhvm JIT

• Beyond static type inference: dynamic type specialization1. Observe types2. Generate specialized code3. Guards to check types

26

$n = 3 * $n + 1;224: Loc 0229: Int 3238: Loc 0243: CGetH244: Mul245: Int 1254: Add255: SetH256: PopC

;; Typecheck: int($n)?cmpl $4, -4(%rbp)jne __retranslate

;; Type-spec xlationmov $3, %r12dmov -16(%rbp), %r13mov %r13, %r14imul %r14, %r12add $1, %r12mov %r12, %r13mov $0x40000000, %r8mov %r8, -8(%rbp)mov %r13, -16(%rbp)

Page 27: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Translation cache: Reuse & specialization

27

;; Typecheck: INT($n)?cmpl $4, -4(%rbp)jne __retranslate

;; Type-spec INT;; translation. . .

Translation Cache

T1:

Translator. . .

__retranslate:. . .

;; Typecheck: DOUBLE($n)?cmpl $8, -4(%rbp)jne __retranslate

;; Type-spec DOUBLE;; translation. . .

T2:

T2

$n = 1.5;...$n = 3 * $n + 1;

Page 28: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Current state

• hhpc– Runs www.facebook.com– Paper to appear in SPLASH ‘12

• hhvm– www.facebook.com works– Developers using it– ~27% slower than hphpc

• Download from github: https://github.com/facebook/hiphop-php/

28

Page 29: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Perf progress

6/11-7/14

Page 30: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Ongoing & future work

• Performance– Profile-guided, SSA-based 2nd gear JIT– Type prediction– Tuning for the HW– Array shapes: turn hash tables into structs

• Tracing garbage collection– Copy-on-write arrays– Precise destruction semantics

• Language extensions

30

Page 31: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Summary

• PHP enables us to move fast• Performance suffers because of interpreter• Hiphop compiler– Compiles PHP to C++ offline– Significantly improves user experience & data center

efficiency• HipHop virtual machine– A new language VM tailored to PHP– Brings dynamic JIT compilation & optimization to PHP

• Both open sourced on github

31

Page 32: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook

Thanks!

Questions?

Page 33: HipHop :  High-Performance PHP Ali-Reza  Adl-Tabatabai HipHop  Team Facebook