Hiphop php

2,598 views

Published on

new way to think PHP

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,598
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
37
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Hiphop php

  1. 1. HipHop Compiler for PHP Transforming PHP into C++ HipHop Compiler Team Facebook, Inc. May 2010 Facebook 2010 (confidential)
  2. 2. PHP is easy to read<?phpfunction tally($count) { $sum = 0; for ($i = 0; $i< $count; ++$i) { $sum += $i; } return $sum;}print tally(10) . “n”; Facebook 2010 (confidential)
  3. 3. PHP syntax is similar to C++/Java <?php class Tool extends Object { public $name; public use($target) {} } $tool = new Tool(); $tool->name = „hammer‟; $tool->use($nail); Facebook 2010 (confidential)
  4. 4. PHP Statements and Expressions FunctionStatement, ClassStatement, InterfaceStatement, ExpressionList, ClassVariable, AssignmentExpression, ClassConstant, SimpleVariable, MethodStatement, DynamicVariable, StatementList, StaticMemberExpression, BlockStatement, ArrayElementExpression, IfBranchStatement, DynamicFunctionCall, IfStatement, SimpleFunctionCall, WhileStatement, ScalarExpression, DoStatement, ObjectPropertyExpression, ForStatement, ObjectMethodExpression, SwitchStatement, ListAssignment, CaseStatement, NewObjectExpression, BreakStatement, UnaryOpExpression, ContinueStatement, IncludeExpression, ReturnStatement, BinaryOpExpression, GlobalStatement, QOpExpression, StaticStatement, ArrayPairExpression, EchoStatement, ClassConstantExpression, UnsetStatement, ParameterExpression, ExpStatement, ModifierExpression, ForEachStatement, ConstantExpression, CatchStatement, EncapsListExpression, TryStatement, ThrowStatement, Facebook 2010 (confidential)
  5. 5. PHP is weakly typed<?php$a = 12345;$a = “hello”;$a = array(12345, “hello”, array());$a = new Object();$c = $a + $b; // integer or array$c = $a . $b; // implicit casting to strings Facebook 2010 (confidential)
  6. 6. Core PHP library is small- Most are in functional style- ~200 to 500 basic functions<?php$len = strlen(“hello”); // C library$ret = curl_exec($curl); // open source Facebook 2010 (confidential)
  7. 7. PHP is easy to debug<?phpfunction tally($count) { $sum = 0; for ($i = 0; $i< $count; ++$i) { $sum += $i;var_dump($sum); } return $sum;} Facebook 2010 (confidential)
  8. 8. PHP is easy to learn easy to read easy to write easy to debug Hello, World! Facebook 2010 (confidential)
  9. 9. PHP is slow  http://shootout.alioth.debian.org/u64q/benchmark.ph p?test=all&lang=all50403020 CPU100 C++ Java C# Erlang Python Perl PHP Facebook 2010 (confidential)
  10. 10. Why is Zend Engine slow? Byte-code interpreter Dynamic symbol lookups  functions, variables, constants  class methods, properties, constants Weakly typing  zval  array() Facebook 2010 (confidential)
  11. 11. Transforming PHP into C++ g++ is a native code compiler static binding  functions, variables, constants  class methods, properties, constants type inference  integers, strings, arrays, objects, variants  struct, vector, map, array Facebook 2010 (confidential)
  12. 12. Static Binding – Function Calls<?php$ret = foo($a);// C++Variant v_ret;Variant v_a;v_ret = f_foo(v_a); Facebook 2010 (confidential)
  13. 13. Dynamic Function Calls<?php$func = „foo‟;$ret = $func($a);// C++Variant v_ret;Variant v_a;String v_func;V_func = “foo”;v_ret = invoke(v_func, CREATE_VECTOR1(v_a)); Facebook 2010 (confidential)
  14. 14. Function Invoke TableVariant invoke(CStrReffunc, CArrRefparams) { int64 hash = hash_string(func); switch (hash) { case 1234: if (func == “foo”) return foo(params[0]) } throw FatalError(“function not found”);} Facebook 2010 (confidential)
  15. 15. Re-declared Functions<?phpif ($condition) { function foo($a) { return $a + 1;}} else { function foo($a) { return $a + 2;}}$ret = foo($a);// C++if (v_condition) {g->i_foo = i_foo$$0;} else {g->i_foo = i_foo$$1;}g->i_foo(v_a); Facebook 2010 (confidential)
  16. 16. Volatile Functions<?phpif (!function_exists(„foo‟)) {bar($a);} else {foo($a);}function foo($a) {}// C++if (f_function_exists(“foo”)) {f_bar(v_a);} else {f_foo(v_a);}g->declareFunction(“foo”); Facebook 2010 (confidential)
  17. 17. Static Binding – Variables<?php$foo = „hello‟;function foo($a) {global $foo; $bar = $foo . $a; return $bar;}// C++String f_foo(CStrRefv_a) { Variant &gv_foo = g->GV(foo); String v_bar;v_bar = concat(toString(gv_foo), v_a); return v_bar;} Facebook 2010 (confidential)
  18. 18. GlobalVariables Classclass GlobalVariables : public SystemGlobals {public: // Direct Global VariablesVariant gv_foo; // Indirect Global Variables for large compilationenum _gv_enums {gv_foo, }Variant gv[1];}; Facebook 2010 (confidential)
  19. 19. Dynamic Variables<?phpfunction foo() { $b = 10; $a = b;echo($$a);}void f_foo() { class VariableTable: public RVariableTable { public: int64 &v_b; String &v_a; VariableTable(int64 &r_b, String &r_a) : v_b(r_b), v_a(r_a) {} virtual Variant getImpl(const char *s) { // hash – switch – strcmp } } variableTable(v_b, v_a);echo(variableTable.get("b”));} Facebook 2010 (confidential)
  20. 20. Static Binding – Constants<?phpdefine(„FOO‟, „hello‟);echo FOO;// C++echo(“hello” /* FOO */); Facebook 2010 (confidential)
  21. 21. Dynamic Constants<?phpif ($condition) {define(„FOO‟, „hello‟);} else {define(„FOO‟, „world‟);}echo FOO;// C++if (v_condition) {g->declareConstant("FOO", g->k_FOO, "hello”);} else {g->declareConstant("FOO", g->k_FOO, "world”);}echo(toString(g->k_FOO)); Facebook 2010 (confidential)
  22. 22. Static Binding with Classes Class methods Class properties Class constants Re-declared classes Deriving from re-declared classes Volatile classes Facebook 2010 (confidential)
  23. 23. Summary - Dynamic Symbol Lookup Problem is nicely solved  Rule of 90-10 Dynamic binding is a general form of static binding Generated code is a super-set of static binding and dynamic binding Facebook 2010 (confidential)
  24. 24. Problem 2. Weakly Typing Type Inference Runtime Type Info (RTTI)-Guided Optimization Type Hints Strongly Typed Collection Classes Facebook 2010 (confidential)
  25. 25. Type Coercions VariantDouble String Array ObjectIntegerBoolean Facebook 2010 (confidential)
  26. 26. Type Inference Example<?php$a = 10;$a = „string‟;Variant v_a; Facebook 2010 (confidential)
  27. 27. Why is strong type faster?$a = $b + $c;if (is_integer($b) &&is_integer($c)) { $a = (int)$b + (int)$c;} else if (is_array($b) &&is_array($c)) { $a = array_merge((array)$b + (array)$c);} else { …}int64 v_a = v_b + v_c; Facebook 2010 (confidential)
  28. 28. Type Inference Blockers<?phpfunction foo() { if ($success) return 10; // integer return false; // doh‟}$arr[$a] = 10; // doh‟++$a; // $a can be a string actually!$a = $a + 1; // $a can become a double, ouch! Facebook 2010 (confidential)
  29. 29. RTTI-Guided Optimization<?phpfunction foo($x) { ...}foo(10);foo(„test‟);void foo(Variantx) { ...} Facebook 2010 (confidential)
  30. 30. Type Specialization Method 1template<typename T>void foo(Tx) { // generate code with generic T (tough!)}-Pros: smaller generated code-Cons: no type propagation Facebook 2010 (confidential)
  31. 31. Type Specialization Method 2void foo(int64 x) { // generate code assuming x is integer}void foo(Variantx) { // generate code assuming x is variant}-Pros: type propagation-Cons: variant case is not optimized Facebook 2010 (confidential)
  32. 32. Type Specialization Method 3void foo(int64 x) { // generate code assuming x is integer}void foo(Variantx) { if (is_integer(x)) { foo(x.toInt64()); return; } // generate code assuming x is variant}-Pros: optimized for integer case-Cons: large code size Facebook 2010 (confidential)
  33. 33. Type Hints<?phpfunction foo(int$a) {string $b;}class bar { public array $c;}bar $d; Facebook 2010 (confidential)
  34. 34. Strongly Typed Collection Classes That omnipotent “array” in PHP  Swapping out underlying implementation:  Array escalation  PHP classes:  Vector  Set  Map: un-ordered  Then Array: ordered map Facebook 2010 (confidential)
  35. 35. Compiler Friendly Scripting LanguageIf all problems described here are considered when designing a new scripting language, will it run faster than Java? Facebook 2010 (confidential)

×