Technology Tasting Wine, Cheese, and a Taste of New Technology February 2nd, 2010 Sharing innovations with the community Load Balancer (assigns a web server) Web Server (PHP assembles data) Services (fast, complicated) Memcached (fast, simple) Databases (slow, persistent) Facebook the data is interconnected Bob Erin Beth Servers Memcached (fast, simple) Databases (slow, persistent) Load Balancer (assigns a web server) Web Server (PHP assembles data) Services (fast, complicated) PHP at Facebook PHP is simple ? Simple to learn: small set of expressions and statements ? Simple to write: loose typing and universal "array" ? Simple to read: similar syntax to C++ and Java ? Simple to debug: no need to re-compile Hello World! Problems we encounter using PHP at scale 1) High CPU usage Problem 1: High CPU ? Tensofthousandsofwebservers ? Upto800msforeachrequest ? Slowerasthecodebase continuestogrow ? Hardwareisn?tfree 0 8 16 24 32 40 C++ Java C# Erlang Python Perl PHP CPU by Language Unit of Time CPU Usage 2) High memory usage 150MB 700MB (700M ? 150M) / 4,000,000 = 144 BYTES Problem 2: High Memory for ($i = 0; $i < 1000000; $i++) { $a[] = $i; } for ($i = 0; $i < 5000000; $i++) { $a[] = $i; } 3) Reuse of PHP logic in other systems Problem 3: Reuse of PHP logic in other systems HTML AJAX API C++ & Python Display Modules Application Logic / Data Modules Infrastructure Modules 4) Extensions are hard to write for most PHP developers 1 High CPU usage 2 High memory usage 3 Reuse of PHP logic in other systems 4 Extensions are hard to write for most PHP developers PHP is problematic for Facebook How can we solve these problems? Solutions considered since 2007 1.Rewritingourmillion+linePHPcodebasetoperformbetter ? buthowdowemaintainitwithnewhires? 2.MovingcomplexlogicfromPHPintoPHPExtensions(C++) ? ?movefast?isimportanttous 3.RewriteaspectsoftheZendEngineitself ? wehavebeenoptimizingPHPinternalsandcontributingpatchesback ? alreadyhighlyoptimized HipHop is a source code transformer HipHop transforms PHP into highly optimized C++ HipHop transforms PHP into highly optimized C++ and uses g++ to compile it HipHop executes the source code in a semantically equivalent manner HipHop sacrifices some rarely used features in exchange for performance Web: 50% less CPU with equal traffic API: 30% less CPU with 2x traffic Our deployment of HipHop 90% 0% six months How HipHop works Functionality Location Lines of code Core Runtime Extension Functions Parser and Static Analysis Utility Functions Unit Tests misc Total Lines cpp/base 60,000 cpp/ext 100,000 lib 45,000 util 30,000 test 37,000 38,000 310,000 HipHop Source Code 1 Code transformation 2 Runtime Two phases 1) Code Transformation $x = 1; if (...) {...} else {...} f(1, 2, 3); for ($i = 0; $i < $n; $i++) {...} $$x = $$y; eval($x . $y); $$$$$foo(); function foo($x) { include $x; } Mundane Magic Optimization Strategy Mundane ? We can greatly speed them up: ? static function calls ? static class methods and properties ? static variable lookups ? g++ -o3 optimizations: function inlining, etc. Magic ? C++ won?t give us that much advantage: ? dynamic function calls: jump table ? dynamic variable lookups: pre-hashing Mundane Magic Transformation Process 1.Staticanalysis ? Collectinformationonwhodeclareswhat,dependencies,etc. 2.TypeInference ? Pickthemostspecifictypeforeveryvariablepossible: ?C++scalars,String,Array,classes,ObjectandVariant ? Typehints 3.CodeGeneration ? ForthemostpartadirectcorrespondencefromPHPstatementsand expressionstoC++statementsandexpressions. Parser Static Analyzer Pre- Optimizer Type Inference Engine Post- Optimizer Code Generator g++ Transformation Process 2) Runtime Runtime vs Generation Programming with HipHop Supported magical PHP features ? dynamicfunctioncall,includingcall_user_func() ? dynamicobjectpropertiesandmethods ? dynamicvariables,includingextract() ? dynamicincludes ? re-declaredfunctions ? re-declaredclasses ? re-declaredconstants ? magicmethods:__toString(),__get(),__set(),__call() Features not supported 1.Dynamiccoding ? eval() ? create_function() ? preg_replacewhenusing/e 2.Order-dependentsymbollookups:function,class,constant if (function_exists(?foo?)) { print ?foo missing?; // side-effect } function foo() { } HPHPi ? The experimental interpreter ? Makeiteasyfordevelopersanddon?tchangetheirprocess ? HPHPiisanexperimentalinterpreter ? Wantedaninterpretertohelpcatchbugsinourimplementation ? Moreruntimechecks ? eval()support ? Itsoundedfun ? Innovation:Youdon?thavetocompileyourPHPtorunit!<applause> Deploying HipHop in production ? Pre-compiledbinaryversusPHPsourcecode ? HipHopisdifferent... ? Runsasoneprocesswithmultiplethreads ? Nodowntimeduringrestarts(porttakeover) ? Pushingalargebinary ? Wespreadourcompileacrossmultiplemachines HipHop currently uses its own very simple web server Roadmap Roadmap ? CatchupwithPHP5.3(currentlyon5.2) ? providessomelanguagechangesaround strongertyping ? Multi-threadingsupport ? SupportApacheasawebserveroption ? EvolvebasedonusageoutsideofFacebook Want to minimize differences between PHP and HipHop Thanks for coming! (c) 2010 Facebook, Inc. or its licensors.  "Facebook" is a registered trademark of Facebook, Inc. All rights reserved.


Envoyer le lien par email
#hiphop php  #facebook php  #php 

licence non indiquée


Partagé par  joloe56


Source:Non communiquée