The Codex of Business Writing Software for Real-World Solutions 2.pptx
Hiphop php
1. HipHop Compiler for PHP
Transforming PHP into C++
HipHop Compiler Team
Facebook, Inc.
May 2010
Facebook 2010 (confidential)
2. PHP is easy to read
<?php
function tally($count) {
$sum = 0;
for ($i = 0; $i< $count; ++$i) {
$sum += $i;
}
return $sum;
}
print tally(10) . “n”;
Facebook 2010 (confidential)
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)
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. PHP is easy to debug
<?php
function tally($count) {
$sum = 0;
for ($i = 0; $i< $count; ++$i) {
$sum += $i;
var_dump($sum);
}
return $sum;
}
Facebook 2010 (confidential)
8. PHP is easy to learn
easy to read
easy to write
easy to debug
Hello, World!
Facebook 2010 (confidential)
9. PHP is slow
http://shootout.alioth.debian.org/u64q/benchmark.ph
p?test=all&lang=all
50
40
30
20 CPU
10
0
C++ Java C# Erlang Python Perl PHP
Facebook 2010 (confidential)
18. GlobalVariables Class
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];
};
Facebook 2010 (confidential)
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. 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. Problem 2. Weakly Typing
Type Inference
Runtime Type Info (RTTI)-Guided Optimization
Type Hints
Strongly Typed Collection Classes
Facebook 2010 (confidential)
30. Type Specialization Method 1
template<typename T>
void foo(Tx) {
// generate code with generic T (tough!)
}
-Pros: smaller generated code
-Cons: no type propagation
Facebook 2010 (confidential)
31. Type Specialization Method 2
void 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. Type Specialization Method 3
void 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)
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. 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)