- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
HipHop: High-Performance PHP
展开查看详情
1 .
2 .HipHop : High-Performance PHP Ali-Reza Adl-Tabatabai HipHop Team Facebook
3 .Facebook: Move fast & build things 3
4 .PHP General-purpose scripting language tailored for web development Interactive Weakly typed & dynamic <?php var_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
5 .PHP f unction foo($x) { echo “foo: “. $x. ”
6 .PHP if (...) { class B { ... } } else { class B { ... } } class C extends B { ... } 6 General-purpose scripting language tailored for web development Interactive Weakly typed & dynamic
7 .PHP $a = ‘f’; $b = ‘c’; $c = ‘oo’; $func = $a . $ $b; $func(); 7 General-purpose scripting language tailored for web development Interactive Weakly typed & dynamic
8 .PHP c lass C { public $declProp = 1; } $obj = new C; $obj->dynProp = 2; // This is OK! echo $obj->declProp . “
9 .PHP 9 General-purpose scripting language tailored for web development Interactive Weakly typed & dynamic i f (function_exists(‘foo’)) { ... } i f (class_exists($c)) { ... }
10 .Memory management PHP is reference counted Precise destruction semantics 10 c lass C { function __destruct() { echo “bye!”; } } $x = new C; $x = 1; // prints “bye!”
11 .Concurrency Single - threaded programming model Multiple requests run in parallel No shared memory, synchronization, or direct communication 11
12 .Performance... Source: http:// shootout.alioth.debian.org /u64q/ benchmark.php?test = all&lang =all 12
13 .Implications for Facebook Bad performance can limit user features Poor efficiency requires lots of re$ource $ INTERNET webservers storage . . . . . . 13
14 .What have we done? Apache Facebook (PHP) PHP/ Zend Facebook (binary) 14
15 .HipHop c ompilation flow 15 Facebook (PHP) hphpc Facebook (C++) PHP Runtime Webserver Gcc Facebook (binary)
16 .HipHop c ompiler ( hphpc ) 16 PHP AST Optimize, Infer Types Parser C++ C++ Code Generator
17 .Representing PHP data type data KindOfString & “ Lorem ipsum .” KindOfInt 13
18 .Type inference: fewer tags! data & “ Lorem ipsum .” 13 type
19 .Basic operations $a + $b
20 .Basic operations: t ype dispatch $a + $b switch (a-> m_type ) { case KindOfInt : switch (b-> m_type ) { … } case KindOfArray : switch (b-> m_type ) { … } … } … } $a + $b
21 .Type inference: avoiding d ispatch $a + $b given $a :: Int ,$b :: Int add %rcx, %rdx
22 .HipHop compiler: performance 22 Disclaimer: estimated based on running Facebook
23 .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
24 .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
25 .HipHop b ytecode ( 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 0 101: Loc 1 106: IssetM <H E> 113: JmpZ 32 118: String “Hit! “ 123: Loc 0 128: Loc 1 133: CGetM <H E> 140: Concat 141: Print 142: PopC 143: True 144: RetC 145: String “Miss!” 150: Print 151: PopC 152: False 153: RetC
26 .Hhvm JIT Beyond static type inference: dynamic type specialization Observe types Generate specialized code Guards to check types 26 $n = 3 * $n + 1; 224: Loc 0 229: Int 3 238: Loc 0 243: CGetH 244: Mul 245: Int 1 254: Add 255: SetH 256: PopC ;; Typecheck : int ($n)? cmpl $4, -4(% rbp ) jne __retranslate ;; Type-spec xlation mov $ 3, %r12d mov -16(% rbp ), %r13 mov %r13, %r14 imul %r14, %r12 add $1, %r12 mov %r12, %r13 mov $0x40000000, %r8 mov %r8, -8(% rbp ) mov %r13, -16(% rbp )
27 .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;
28 .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
29 .Perf progress 6/11-7/14