HHVM

 HHVM

Get started with HHVM. Learn about running and debugging your application on HHVM and why HHVM can be so much faster than Zend PHP!

http://www.phpconference.nl/

D07a7a143b14fc8309f9abb78d569344?s=128

Alexander

June 27, 2014
Tweet

Transcript

  1. None
  2. asm89 / @iam_asm89

  3. None
  4. None
  5. History

  6. None
  7. HPHPc • Open-sourced 2010 • 6x speedup vs Zend VM

  8. HPHPc • Compiled PHP C++ →

  9. HPHPc • No eval • No dynamic functions • Two

    execution engines
  10. None
  11. JIT compiler

  12. JIT compiler • Zend VM – PHP Opcodes Runs on

    VM → →
  13. JIT compiler • HHVM – PHP HHBC (ByteCode) HHIR (Intermediate

    Representation) → → Machine code →
  14. Getting started

  15. $ sudo wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key \ > | sudo

    apt-key add - $ echo deb http://dl.hhvm.com/ubuntu trusty main \ > | sudo tee /etc/apt/sources.list.d/hhvm.list $ sudo apt-get update $ sudo apt-get install hhvm https://github.com/facebook/hhvm/wiki/Prebuilt-packages-on-Ubuntu-14.04
  16. Homebrew https://github.com/facebook/hhvm/wiki/Building-and-installing-HHVM-on-OSX-10.9

  17. fastcgi https://github.com/facebook/hhvm/wiki/FastCGI

  18. $ hhvm hi-dpc.php hi dpc!

  19. Can I run HHVM?

  20. http://hhvm.com/frameworks/

  21. Try it!

  22. Why HHVM?

  23. Not just PHP...

  24. What is at stake? • Consequence of subtle bugs: –

    Corrupt database, etc.. • Consequence of difficult APIs: – Copy pasta, $needle or $haystack first • Consequence of difficult refactoring – Bad APIs stay bad
  25. Technical debt

  26. None
  27. None
  28. • A statically typed language for HHVM • Compatible with

    PHP: – Interoperates with no overhead – Same representation at runtime • Evolved from PHP: – If you know PHP, you know Hack! • Designed for incremental adoption: – Gradual typing
  29. None
  30. None
  31. Demo

  32. Hack type system • What must be annotated? – Class

    members – Function parameters – Return types The rest is inferred
  33. Hack types • All PHP types: int, MyClassName, array •

    Nullable: ?int, ?MyClassName • Mixed: anything (careful) • Tuples: (int, bool, X) • Closures: (function(int): int) • Collections: Vector<int>, Map<string, int> • Generics: A<T>, foo<T>(T $x): T • Constraints: foo<T as A>(T $x): T
  34. Not just types

  35. Type annotations at run time function foo(int $x): void {

    .. } foo('baz'); // Output Fatal error: Expected int, string given function bar(): bool { return 3.14; } // Output Fatal error: Expected bool, float given
  36. λ expressions

  37. PHP closure expressions // PHP closure syntax $b = 42;

    $fn = function($a) use ($b) { return $a + $b; }; $x = $fn(42);
  38. Hack lambda expressions // PHP closure syntax $b = 42;

    $fn = function($a) use ($b) { return $a + $b; }; $x = $fn(42); // Hack lambda syntax $b = 42; $fn = $a ==> $a + $b; $x = $fn(42);
  39. Benefits of Lambda Expressions • More concise • Auto capturing

    references • Makes it easier to use callback-based APIs (wait for it..)
  40. Collections

  41. The Hack Collections framework is a set of language features

    and APIs that serve as a complete alternative to PHP arrays // PHP arrays $a = array(1, 2, 3); $b = array('a' => 4, 'b' => 5); function foo(array $c): void { .. } // Hack collections $a = Vector {1, 2, 3}; $b = Map {'a' => 4, 'b' => 5}; function foo(Map<string,int> $c): void { .. }
  42. Map/filter example // PHP arrays $x = array_filter(array_map($fn1, $a), $fn2);

  43. Map/filter example // PHP arrays $x = array_filter(array_map($fn1, $a), $fn2);

    // Hack collections $x = $a->map($fn1)->filter($fn2);
  44. Map/filter example // PHP arrays $x = array_filter( array_map( function($a)

    use ($b) { return $a + $b; }, $array ), function($a) { return $a > 10; } );
  45. Map/filter example // PHP arrays $x = array_filter( array_map( function($a)

    use ($b) { return $a + $b; }, $array ), function($a) { return $a > 10; } ); // Hack collections $x = $a->map($a ==> $a + $b) ->filter($a ==> $a > 10);
  46. Demo

  47. More features • Async/await • Constructor promotion • Trailing comma's

    in function calls • Trait requirements • User attributes (<<Entity>> class C {}) • <<Override>> • ...
  48. Try it!

  49. https://github.com/facebook/hhvm #hhvm @ freenode IRC

  50. @iam_asm89 https://joind.in/10863