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. View Slide

  2. asm89
    /
    @iam_asm89

    View Slide

  3. View Slide

  4. View Slide

  5. History

    View Slide

  6. View Slide

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

    View Slide

  8. HPHPc
    ● Compiled PHP C++

    View Slide

  9. HPHPc
    ● No eval
    ● No dynamic functions
    ● Two execution engines

    View Slide

  10. View Slide

  11. JIT compiler

    View Slide

  12. JIT compiler
    ● Zend VM
    – PHP Opcodes Runs on VM
    → →

    View Slide

  13. JIT compiler
    ● HHVM
    – PHP HHBC (ByteCode) HHIR (Intermediate Representation)
    → →
    Machine code

    View Slide

  14. Getting
    started

    View Slide

  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

    View Slide

  16. Homebrew
    https://github.com/facebook/hhvm/wiki/Building-and-installing-HHVM-on-OSX-10.9

    View Slide

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

    View Slide

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

    View Slide

  19. Can I run
    HHVM?

    View Slide

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

    View Slide

  21. Try it!

    View Slide

  22. Why HHVM?

    View Slide

  23. Not just
    PHP...

    View Slide

  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

    View Slide

  25. Technical
    debt

    View Slide

  26. View Slide

  27. View Slide

  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

    View Slide

  29. View Slide

  30. View Slide

  31. Demo

    View Slide

  32. Hack type system
    ● What must be annotated?
    – Class members
    – Function parameters
    – Return types
    The rest is inferred

    View Slide

  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, Map
    ● Generics: A, foo(T $x): T
    ● Constraints: foo(T $x): T

    View Slide

  34. Not just
    types

    View Slide

  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

    View Slide

  36. λ expressions

    View Slide

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

    View Slide

  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);

    View Slide

  39. Benefits of Lambda Expressions
    ● More concise
    ● Auto capturing references
    ● Makes it easier to use callback-based APIs (wait for it..)

    View Slide

  40. Collections

    View Slide

  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 $c): void { .. }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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);

    View Slide

  46. Demo

    View Slide

  47. More features
    ● Async/await
    ● Constructor promotion
    ● Trailing comma's in function calls
    ● Trait requirements
    ● User attributes (<> class C {})
    ● <>
    ● ...

    View Slide

  48. Try it!

    View Slide

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

    View Slide

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

    View Slide