hiphop compiler for php

35
Facebook 2010 (confidential) HipHop Compiler for PHP Transforming PHP into C++ HipHop Compiler Team Facebook, Inc. May 2010

Upload: howe

Post on 23-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

HipHop Compiler for PHP. Transforming PHP into C++ . HipHop Compiler Team Facebook, Inc. May 2010. PHP is easy to read.

TRANSCRIPT

Page 1: HipHop  Compiler for PHP

Facebook 2010 (confidential)

HipHop Compiler for PHP

Transforming PHP into C++

HipHop Compiler TeamFacebook, Inc.

May 2010

Page 2: HipHop  Compiler for PHP

PHP is easy to read

Facebook 2010 (confidential)

<?php

function tally($count) { $sum = 0; for ($i = 0; $i < $count; ++$i) { $sum += $i; } return $sum;}

print tally(10) . “\n”;

Page 3: HipHop  Compiler for PHP

PHP syntax is similar to C++/Java

Facebook 2010 (confidential)

<?php

class Tool extends Object { public $name;

public use($target) {}}

$tool = new Tool();$tool->name = ‘hammer’;$tool->use($nail);

Page 4: HipHop  Compiler for PHP

PHP Statements and Expressions

Facebook 2010 (confidential)

ExpressionList, AssignmentExpression, SimpleVariable, DynamicVariable, StaticMemberExpression, ArrayElementExpression, DynamicFunctionCall, SimpleFunctionCall, ScalarExpression, ObjectPropertyExpression, ObjectMethodExpression, ListAssignment, NewObjectExpression, UnaryOpExpression, IncludeExpression, BinaryOpExpression, QOpExpression, ArrayPairExpression, ClassConstantExpression, ParameterExpression, ModifierExpression, ConstantExpression, EncapsListExpression,

FunctionStatement, ClassStatement, InterfaceStatement, ClassVariable, ClassConstant, MethodStatement, StatementList, BlockStatement, IfBranchStatement, IfStatement, WhileStatement, DoStatement, ForStatement, SwitchStatement, CaseStatement, BreakStatement, ContinueStatement, ReturnStatement, GlobalStatement, StaticStatement, EchoStatement, UnsetStatement, ExpStatement, ForEachStatement, CatchStatement, TryStatement, ThrowStatement,

Page 5: HipHop  Compiler for PHP

PHP is weakly typed

Facebook 2010 (confidential)

<?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

Page 6: HipHop  Compiler for PHP

Core PHP library is small

Facebook 2010 (confidential)

- Most are in functional style- ~200 to 500 basic functions

<?php

$len = strlen(“hello”); // C library$ret = curl_exec($curl); // open source

Page 7: HipHop  Compiler for PHP

PHP is easy to debug

Facebook 2010 (confidential)

<?php

function tally($count) { $sum = 0; for ($i = 0; $i < $count; ++$i) { $sum += $i; var_dump($sum); } return $sum;}

Page 8: HipHop  Compiler for PHP

PHP is easy to learn

Facebook 2010 (confidential)

easy to read easy to write easy to debug

Hello, World!

Page 9: HipHop  Compiler for PHP

PHP is slow

Facebook 2010 (confidential)

C++ Java C# Erlang Python Perl PHP0

10

20

30

40

http://shootout.alioth.debian.org/u64q/benchmark.php?

test=all&lang=all

CPU

Page 10: HipHop  Compiler for PHP

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)

Page 11: HipHop  Compiler for PHP

Transforming PHP into C++

Facebook 2010 (confidential)

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

Page 12: HipHop  Compiler for PHP

Static Binding – Function Calls

Facebook 2010 (confidential)

<?php$ret = foo($a);

// C++Variant v_ret;Variant v_a;

v_ret = f_foo(v_a);

Page 13: HipHop  Compiler for PHP

Dynamic Function Calls

Facebook 2010 (confidential)

<?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));

Page 14: HipHop  Compiler for PHP

Function Invoke Table

Facebook 2010 (confidential)

Variant invoke(CStrRef func, CArrRef params) { int64 hash = hash_string(func); switch (hash) { case 1234: if (func == “foo”) return foo(params[0]) } throw FatalError(“function not found”);}

Page 15: HipHop  Compiler for PHP

Re-declared Functions

Facebook 2010 (confidential)

<?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);

Page 16: HipHop  Compiler for PHP

Volatile Functions

Facebook 2010 (confidential)

<?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”);

Page 17: HipHop  Compiler for PHP

Static Binding – Variables

Facebook 2010 (confidential)

<?php$foo = ‘hello’;function foo($a) { global $foo; $bar = $foo . $a; return $bar;}

// C++String f_foo(CStrRef v_a) { Variant &gv_foo = g->GV(foo); String v_bar; v_bar = concat(toString(gv_foo), v_a); return v_bar;}

Page 18: HipHop  Compiler for PHP

GlobalVariables Class

Facebook 2010 (confidential)

class GlobalVariables : public SystemGlobals {public: // Direct Global Variables Variant gv_foo;

// Indirect Global Variables for large compilation enum _gv_enums { gv_foo, } Variant gv[1]; };

Page 19: HipHop  Compiler for PHP

Dynamic Variables

Facebook 2010 (confidential)

<?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”));}

Page 20: HipHop  Compiler for PHP

Static Binding – Constants

Facebook 2010 (confidential)

<?phpdefine(‘FOO’, ‘hello’);echo FOO;

// C++echo(“hello” /* FOO */);

Page 21: HipHop  Compiler for PHP

Dynamic Constants

Facebook 2010 (confidential)

<?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));

Page 22: HipHop  Compiler for PHP

Static Binding with Classes

Class methods Class properties Class constants Re-declared classes Deriving from re-declared classes Volatile classes

Facebook 2010 (confidential)

Page 23: HipHop  Compiler for PHP

Summary - Dynamic Symbol Lookup Problem is nicely solved

Rule of 90-10Dynamic binding is a general

form of static bindingGenerated code is a super-set of

static binding and dynamic binding

Facebook 2010 (confidential)

Page 24: HipHop  Compiler for PHP

Problem 2. Weakly Typing

Type InferenceRuntime Type Info (RTTI)-Guided

OptimizationType HintsStrongly Typed Collection Classes

Facebook 2010 (confidential)

Page 25: HipHop  Compiler for PHP

Type Coercions

Facebook 2010 (confidential)

Page 26: HipHop  Compiler for PHP

Type Inference Example

Facebook 2010 (confidential)

<?php$a = 10;$a = ‘string’;

Variant v_a;

Page 27: HipHop  Compiler for PHP

Why is strong type faster?

Facebook 2010 (confidential)

$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;

Page 28: HipHop  Compiler for PHP

Type Inference Blockers

Facebook 2010 (confidential)

<?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!

Page 29: HipHop  Compiler for PHP

RTTI-Guided Optimization

Facebook 2010 (confidential)

<?phpfunction foo($x) { ...}

foo(10);foo(‘test’);

void foo(Variant x) { ...}

Page 30: HipHop  Compiler for PHP

Type Specialization Method 1

Facebook 2010 (confidential)

template<typename T>void foo(T x) { // generate code with generic T (tough!)}

-Pros: smaller generated code-Cons: no type propagation

Page 31: HipHop  Compiler for PHP

Type Specialization Method 2

Facebook 2010 (confidential)

void foo(int64 x) { // generate code assuming x is integer}void foo(Variant x) { // generate code assuming x is variant}

-Pros: type propagation-Cons: variant case is not optimized

Page 32: HipHop  Compiler for PHP

Type Specialization Method 3

Facebook 2010 (confidential)

void foo(int64 x) { // generate code assuming x is integer}void foo(Variant x) { if (is_integer(x)) { foo(x.toInt64()); return; } // generate code assuming x is variant}

-Pros: optimized for integer case-Cons: large code size

Page 33: HipHop  Compiler for PHP

Type Hints

Facebook 2010 (confidential)

<?phpfunction foo(int $a) { string $b;}

class bar { public array $c;}

bar $d;

Page 34: HipHop  Compiler for PHP

Strongly Typed Collection Classes

That omnipotent “array” in PHP

Swapping out underlying implementation:Array escalationPHP classes:

VectorSetMap: un-orderedThen Array: ordered map

Facebook 2010 (confidential)

Page 35: HipHop  Compiler for PHP

Compiler Friendly Scripting Language

If all problems described here are considered when designing a new scripting language, will it run faster than Java?

Facebook 2010 (confidential)