A look at Facebook's HipHop for PHP library, both the first generation, HPHPc, and the new HHVM. This presentation walks through the challenges that faced Facebook and their PHP-based solution from a year after their explosive growth in 2007 to late-2013.
Scaling API-first – The story of a global engineering organization
IPC 2013 - High Performance PHP with HipHop
1. Steve Kamerman | ScientiaMobile, Inc.
Co-Founder / Software Architect
High-Performance PHP with HipHop
2.
3. 2008 - Facebook's Challenge
●
Explosive growth
●
Need more servers!
●
Bottleneck: Page generation
●
20% of PHP time was in native code
4. Why is PHP so slow?
●
Symbol table lookups
●
No static binding
●
Dynamic typing
●
Share-Nothing-Architecture
5. 2008-2010 – HipHop for PHP is born
●
400 Billion PHP-generated pageviews per
month!
●
Facebook invents HipHop for PHP
●
Deployed system-wide in 2010
7. What is HipHop for PHP?
• Multi-threaded web server + PHP runtime
• HPHPc: PHP -> C++ translator
• HHVM: PHP virtual machine with JIT
compiler
• Typically 2x – 5x speed increase over stock
PHP + APC
8. HipHop for PHP Compiler (HPHPc)
●
Translates PHP code into C++
●
Dynamic typing makes this difficult
●
Function parity lacking
●
Long compilation time
17. Avoid global scope
●
●
Code in the global scope is interpreted since it
is (nearly) impossible to type-track
Even wrapping global code helps:
class foo{public static function bar(){
$something = "test";
echo $something;
}}foo::bar();
●
Better approach: don’t write crappy code!
18. Unique Classes, Functions, Constants
●
HHVM supports namespaces – use them!
●
MyAppBar and MyAppFooBar are not ambiguous
●
●
●
Avoid “dynamic” constants:
define("MY_CONST", $foo? "foo": "bar");
I can almost picture a feature request for
undefine() and redefine()
If they are truly constant, use const
19. Better approach: don't use define()
namespace MyApp;
class Config {
const MY_CONST = "I'm real constant";
private static $storage = array(
"PSEUDO_CONSTANT" => "What did you call me?",
);
public static function get($name) {
if (!array_key_exists($name, self::$storage)) {
throw new Exception(__CLASS__." property [$name] does
not exist");
}
return self::$storage[$name];
}
}
echo Config::MY_CONST."n";
20. Avoid dynamic variables
●
If you are using this syntax, there is a better way to solve
the problem:
$foo = "Wow, this hurts my brain!";
$var_name = "foo";
echo $$var_name;
●
●
extract() and compact() are sometimes used more
legitimately (ex: view rendering), but avoid them
If you need get_defined_vars() for non-testing code,
you need to rewrite it before someone sees it :)
21. Declare class properties
●
They should be declared anyway
$foo = new stdClass();
echo $foo->bar; // PHP Notice: Undefined
property
●
Getters are optimized
22. Perfomance Gain
●
●
Less than 5%, but still good practice
Sub-optimal functions still work, even
eval() and goto!
http://php.net/manual/en/control-structures.goto.php
24. Current state of HipHop
●
HHVM v2.2.0 just released
●
Very close to PHP 5.4
●
Dozens of extensions
●
Performance nearly 5x
●
Goal: Support top 20 frameworks in 2013
26. Why is it open source? Will it last?
●
Under PHP license, hard to separate
●
Public activity slowed in 2011
●
Under active public development in 2012-13
●
Dev team is active on IRC (freenode #hhvm)
27. Facebook's Commitment
“In years past we've had a few examples of projects which have not
been well supported, but we are now much better at growing and
sustaining community projects, including, lately, in mobile.
[HipHop for PHP] is absolutely a project that we stand strongly
behind… stay tuned.”
James Pearce, Head of Developer Advocacy at Facebook
28. References
●
●
●
●
●
Special thanks to the HipHop for PHP team!
https://www.facebook.com/hphp
Twitter: @HipHopVM
http://www.hhvm.com/
Sebastian Bergmann – Static Code Analysis with HipHop
http://sebastian-bergmann.de/archives/918-Static-Analysis-with-HipHop-for-PHP.html
http://www.slideshare.net/nickgsuperstar/static-analysis-for-php
https://github.com/sebastianbergmann/hhvm-wrapper
Haiping Zhao – HipHop Compiler for PHP? Transforming PHP into C++
http://www.youtube.com/watch?v=p5S1K60mhQU
Sara Golemon – Scaling with HipHop
http://www.youtube.com/watch?v=Dwek7dZDFN0
Ben Foster – Facebook Growth Data
http://www.benphoster.com/facebook-user-growth-chart-2004-2010/
Notes de l'éditeur
{"5":"154k/second, or 154/sec per server on 1000 servers\n","11":"Facebook was customer\nReduced latency from 20ms (Apache) to 2m\nPeak traffic over 1500 req/sec, load tested at over 10k req/sec per serverCode rebuild took almost 10 minutes per server\nHPHP and Apache shared the same document root\nCompilation of HPHP itself was painful! Super-specific dependencies\n","23":"To use:\nAnalyze:\nhhvm --hphp --target hhbc --input-list <(find -name "*.php") -k1 -l3\nMove in hhvm.hhbc\nRestart server\n","8":"Literally translated PHP code into human-readable C++, preserving class names\nAt Facebook, PHP and C++ are common languages, so this works well\nMany versions of each function may be generated with different parameter types to optimized code\nCompilation at Facebook took 20min (across 100 machines)\n"}