HHVM? Huh?

HHVM? Huh?

An introduction to what HHVM is, how it works, and how to use it.

78cb08b10705a9d56888236115dfe7e5?s=128

geevcookie

April 10, 2014
Tweet

Transcript

  1. HHVM? Huh?

  2. Where did it start?

  3. » Facebook had the "problem" of being too busy

  4. » Facebook had the "problem" of being too busy »

    Hip Hop Compiler (hphpc)
  5. » Facebook had the "problem" of being too busy »

    Hip Hop Compiler (hphpc) » Compiles PHP to C++
  6. » Facebook had the "problem" of being too busy »

    Hip Hop Compiler (hphpc) » Compiles PHP to C++ » Hip Hop Virtual Machine (HHVM)
  7. » Facebook had the "problem" of being too busy »

    Hip Hop Compiler (hphpc) » Compiles PHP to C++ » Hip Hop Virtual Machine (HHVM) » JIT (Just In Time) Compiler
  8. So what is it?

  9. » Complete custom implementation of PHP

  10. » Complete custom implementation of PHP » Can act as

    server or run on Apache with FCGI
  11. » Complete custom implementation of PHP » Can act as

    server or run on Apache with FCGI » Can also run from command line
  12. » Complete custom implementation of PHP » Can act as

    server or run on Apache with FCGI » Can also run from command line alias composer='hhvm /usr/local/bin/composer'
  13. How does it work?

  14. » Compiles PHP to HHVM bytecode

  15. » Compiles PHP to HHVM bytecode » Easier to translate

    to native code
  16. » Compiles PHP to HHVM bytecode » Easier to translate

    to native code » Can be interpreted by CPU
  17. » Compiles PHP to HHVM bytecode » Easier to translate

    to native code » Can be interpreted by CPU » Cache implemented as SQLite DB on disk
  18. Comparison time!

  19. None
  20. None
  21. None
  22. None
  23. Compatibility

  24. » 100% compatibility with 18 of the most popular frameworks/libraries

  25. » 100% compatibility with 18 of the most popular frameworks/libraries

    » Average compatibility of 97.49%
  26. » 100% compatibility with 18 of the most popular frameworks/libraries

    » Average compatibility of 97.49% » List checked: assetic,codeigniter,composer,doctrine2, drupal,facebookphpsdk,guzzle,idiorm,joo mla,joomlacms,klein,kohana,laravel,mage nto2,mediawiki,mockery,monolog,mustache ,paris,pear,phpbb3,phpmyadmin,phpunit,p roxymanager,ratcher,reactphp,silverstri pe,slim,symfony,twig,vfsstream,yii,zf2
  27. How to use HHVM

  28. Command Line hhvm /path/to/script.php

  29. Daemon hhvm --config /path/to/config.hdf --user www-data --mode daemon

  30. Daemon Config VirtualHost { * { Pattern = .* RewriteRules

    { dirindex { pattern = ^/(.*)/$ to = $1/index.php qsa = true } } } }
  31. FCGI: Apache cd /path/to/your/apache/conf ln -s ../mods-available/mod_proxy.load mods-enabled/mod_proxy.load ln -s

    ../mods-available/mod_proxy.conf mods-enabled/mod_proxy.conf ln -s ../mods-available/mod_proxy_fcgi.load mods-enabled/mod_proxy_fcgi.load httpd.conf ProxyPass / fcgi://127.0.0.1:9000/path/to/your/www/root/goes/here/ VirtualHost ProxyPassMatch ^/(.*.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/www/root/goes/here/$1
  32. FCGI: Nginx root /path/to/your/www/root/goes/here; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME

    /path/to/your/www/root/goes/here$fastcgi_script_name; include fastcgi_params;
  33. The magic of Hack

  34. Hack: Example <?hh class AnnotatedClass { public int $x; private

    string $s; protected array $arr; public AnotherClass $ac; function bar(string $str, bool $b): float { if ($b && $str === "Hi") { return 3.2; } return 0.3; } }
  35. » Easy to move over to hack as it is

    basically PHP with type annotations
  36. » Easy to move over to hack as it is

    basically PHP with type annotations » Contains new collections (Vector, Set, Map, etc)
  37. » Easy to move over to hack as it is

    basically PHP with type annotations » Contains new collections (Vector, Set, Map, etc) » Contains new Special-Cased functions
  38. » Easy to move over to hack as it is

    basically PHP with type annotations » Contains new collections (Vector, Set, Map, etc) » Contains new Special-Cased functions » invariant, invariant_violation, invariant_callback_register, fun, class_meth, inst_meth, meth_caller
  39. Hack: invariant <?hh function int_not_null(?int $x): int { // Calls

    invariant_violation which in turn throws an exception. invariant($x !== null, 'Cannot work with a null int'); return $x; }
  40. Hack: fun <?hh function f(string $s): void {} function test():

    void { // Will fail. $f = 'f'; $f(1); // Will find get valid callable for function, but will produce a type error. $f = fun('f'); $f(1); }
  41. The End