Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++...

20
Hack for HipHop Julien Verlaguet (Facebook) HipHop Team

Transcript of Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++...

Page 1: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  for  HipHop  Julien  Verlaguet  (Facebook)  

HipHop  Team  

Page 2: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

What  is  PHP  good  at?  

•  PHP  features:  – Very  fast  installaAon  Ame  – A  lot  of  libraries  – Easy  to  learn  – Scales  well  (avoids  concurrency  problems)  

•  But  do  we  really  care?  

Page 3: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

PHP:  a  FAST  feedback  loop  

FAST  dyna

mic  

Page 4: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

PHP:  the  challenges  at  scale  

•  Performance  (runAme):  – At  this  scale,  1%  maKers!  – Hard  to  opAmize  

•  Development:  – Refactoring  is  difficult  – Bugs  are  caught  at  runAme  – Tooling  is  primiAve  

Page 5: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Scaling  PHP  runAme:  HipHop  

Compiler  C++(ProducAon)  

Interpreter  (Sandboxes)  

PHP  

Page 6: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Scaling  PHP  runAme:  HHVM  

PHP  

JIT  Compiler  (HHVM)  

Page 7: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Scaling  PHP  development:  Hack  

PHP  

JIT  Compiler  (HHVM)  

HACK  

Page 8: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

HACK  (or  Hack  for  HipHop)  

Page 9: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  

•  A  sta+cally  typed  language  for  HHVM  •  CompaAble  with  PHP:  –  Interoperates  with  no  overhead  – Same  representaAon  at  runAme  

•  Evolved  from  PHP:  –  If  you  know  PHP,  you  know  Hack!  

•  Designed  for  incremental  adop+on:  – Gradual  typing  

Page 10: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

DEMO  

Page 11: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  Type  System  

•  What  must  be  annotated?  – Class  members  – FuncAon  parameters  – Return  types  

•  What  is  inferred?  – All  the  rest  

•  AnnotaAng  is  an  incremental  process  

Page 12: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  Types  

•  Nullable:  ?int,  ?MyClassName  •  Tuples:  (int, bool, X) •  Closures:  (function(int): int) •  CollecAons:  Vector<int>,  Map<string, int> •  Generics:  A<T>,  foo<T>(T $x): T •  Constraints:  foo<T as A>(T $x): T •  Type  aliasing:  [new]type t = … •  Extensible  records:  shape(‘x’ => int)

Page 13: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

DEMO  

Page 14: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

HACK  INTERNALS  

Page 15: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Working  at  scale:  Hack  

•  We  knew  we  wanted  an  IDE  from  day  one  •  Big  code  base  •  The  soluAon,  a  server:  – The  server  type-­‐checks  all  the  files  – Keeps  track  of  the  dependencies  – Recomputes  types  when  something  changed  

Page 16: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Working  at  scale:  The  Constraints  

•  Auto-­‐complete  requires  very  low  latency  •  Users  use  version  control  (e.g.,  switching  between  branches)  

•  We  must  use  a  reasonable  amount  of  RAM  •  We  must  have  a  reasonable  iniAalizaAon  Ame  •  Must  be  stable  

Page 17: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  is  wriKen  in  Ocaml!  

•  OCaml  was  a  good  choice:  –  Ideal  for  symbolic  computaAon  – Excellent  performance  – Can  be  compiled  to  JS  –  Interoperates  well  with  C  

•  The  challenge:  – The  runAme  doesn’t  support  mulAcore  

Page 18: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

Hack  architecture  

MASTER  

Worker1   WorkerN  …  

SHARED  HEAP  

C  

Page 19: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

OCaml  at  Scale  •  IPC:  –  Pipes,  sockets  etc  …  –  Caching  layers  to  avoid  deserializaAon  cost  –  Carefully  crabed  lock  free  data  structures  (C  code)  

•  Garbage  collecAon:  – Workers  keep  a  small  heap  –  Shared  memory  is  compacted  by  the  master  

•  OCaml  makes  you  think  hard  about  shared  objects:  – And  that’s  a  good  thing!  ;-­‐)  

Page 20: Hack%for%HipHop%cufp.org/2013/slides/verlaguet.pdf · Scaling%PHP%runAme:%HipHop% Compiler%C++ (Producon) Interpreter% (Sandboxes)% PHP%

QuesAons?