Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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/

Alexander

June 27, 2014
Tweet

More Decks by Alexander

Other Decks in Programming

Transcript

  1. JIT compiler • HHVM – PHP HHBC (ByteCode) HHIR (Intermediate

    Representation) → → Machine code →
  2. $ 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
  3. 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
  4. • 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
  5. Hack type system • What must be annotated? – Class

    members – Function parameters – Return types The rest is inferred
  6. 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
  7. 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
  8. PHP closure expressions // PHP closure syntax $b = 42;

    $fn = function($a) use ($b) { return $a + $b; }; $x = $fn(42);
  9. 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);
  10. Benefits of Lambda Expressions • More concise • Auto capturing

    references • Makes it easier to use callback-based APIs (wait for it..)
  11. 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 { .. }
  12. Map/filter example // PHP arrays $x = array_filter(array_map($fn1, $a), $fn2);

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

    use ($b) { return $a + $b; }, $array ), function($a) { return $a > 10; } );
  14. 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);
  15. More features • Async/await • Constructor promotion • Trailing comma's

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