1 / 67

Kalinichenko

HHVM and Drupal

AlexBogush
Download Presentation

Kalinichenko

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HHVM and Drupal Hip Hop (Hack) Virtual Machine with Love from FACEBOOK

  2. About Me Oleksiy Kalinichenko Drupal Exp.: 12 years E-mail: AexChecker@gmail.com Skype: AexChecker

  3. 01. HHVM and Drupal 1. 2. 3. 4. 5. 6. 7. 8. 9. Typechecking Generics Features of Hack PHP Features Not Supported in Hack Collections Async XHP Configuring and Deploying HHVM hphpd: Interactive Debugging Hack Tools 10.

  4. 01. Typechecking

  5. 01. Typechecking Drupal && Typing Drupal && Typing ~ 370%+ ~ 370%+

  6. 01. Typechecking Why Use the Typechecker? Setting Up the Typechecker ○ Autoload Everything ○ Reading Error Messages ○ Function Return Types ○ Function Parameters ○ Properties Hack’s Type System ● ● ●

  7. 01. Typechecking -> Why Use the Typechecker? The argument in favor of Hack typechecking sounds similar to the argument often used in favor of statically typed languages. The typechecker is able to look for mistakes without running the program, so it can catch problems even with codepaths that aren’t run during testing. Because it doesn’t need to run the program, it catches problems earlier in development, which saves development time. Static analysis capability makes refactoring easier, as it can ensure that there are no breakages at module boundaries. ●

  8. 01. Typechecking -> Setting Up the Typechecker $ touch .hhconfig $ hh_client Running hh_client first checks for a running hh_server process. If there isn’t one, the client will start one, so you should never have to start one yourself. The server will find the .hhconfig file and analyze every Hack file it finds in the directory containing that file and all directories below it. You can use “<?php” and “<?hh” syntax. ● ●

  9. 01. Typechecking -> Setting Up the Typechecker -> Autoload Everything PHP provides autoloading for classes, and HHVM supports this, through both "__autoload()" and "spl_autoload_register()". HHVM provides an additional feature that allows autoloading for functions and constants in both PHP and Hack, plus autoloading for type aliases in Hack only.

  10. 01. Typechecking -> Setting Up the Typechecker -> Reading Error Messages <?hh $ hh_client function main() { /home/aex/test.hh:4:3,6: an int does not allow array append (Typing[4006]) /home/aex/test.hh:3:8,9: You might want to check this out. $a = 10; $a[] = 20; }

  11. 01. Typechecking -> Setting Up the Typechecker -> Function Return Types function returns_an_int(): int { /* … */ } function returns_a_string(): string { /* … */} $add_one = function ($x): int { return $x + 1; }; $add_n = function ($x): int use ($n) { return $x + $n; };

  12. 01. Typechecking -> Setting Up the Typechecker -> Function Parameters function f(int $start, string $thing) { /* … */ } function f(SomeClass $obj = null) { /* … */ }

  13. 01. Typechecking -> Setting Up the Typechecker -> Properties class C { public static int $logging_level = 2; private string $name; }

  14. 01. Typechecking -> Hack’s Type System Primitive types Primitive types: bool , int , float , string , array , and resource; Object types: Object types: The name of any class or interface—built-in or non-built-in—can be used in a type annotation; Enums: Enums: The name of an enum can be used as a type annotation; Tuples: Tuples: Tuples are a way to bundle together a fixed number of values of possibly different types. The most common use for tuples is to return multiple values from a function; mixed: mixed: mixed means any value that can possibly exist in a Hack program, including null; void void and this this Type aliases, Shapes, Nullable types, Callable types, Generics Type aliases, Shapes, Nullable types, Callable types, Generics ● ● ● ● ● ● ●

  15. 02. Generics

  16. 02. Generics (@TODO: needs more examples) class Wrapper<Tval> { private Tval $value; public function __construct(Tval $value) { $this->value = $value; } public function setValue(Tval $value): void { $this->value = $value; } public function getValue(): Tval { return $this->value; } } // ………. $wrapper = new Wrapper(20); $x = $wrapper->getValue(); // …. function takes_wrapper_of_num(Wrapper<num> $w): void { } function takes_wrapper_of_int(Wrapper<int> $w): void { takes_wrapper_of_num($w); }

  17. 02. Generics -> Covariance and Contravariance Covariance class CovariantOnT<+T> { private T $value; // No + here // … } class ContravariantOnT<-T> { private T $value; // No - here // … } class InvariantOnT<T> { private T $value; // … } class DifferentVariances <Tinvariant, +Tcovariant, -Tcontravariant> { // … } The prefix co- means “with,” and the subtype relationship of a generic type goes with—“in the same direction as”—the subtype relationship of arguments to a covariant type parameter. Because they go together, the symbol is a plus sign. Contravariance The prefix contra- means “against,” and the subtype relationship of a generic type goes against the subtype relationship of arguments to a contravariant type parameter. Because they go in opposite directions, the symbol is a minus sign.

  18. 03. Features of Hack

  19. 03. Features of Hack ● ● ● ● ● ● ● ● ● ● Enums Enums Type Aliases Lambda Expressions Constructor Parameter Promotion Attributes Enhanced Autoloading Integer Arithmetic Overflow Nullsafe Method Call Operator Trait and Interface Requirements Silencing Typechecker Errors

  20. 03. Features of Hack -> Enums enum CardSuit : int { SPADES = 0; HEARTS = 1; CLUBS = 2; DIAMONDS = 3; } function suit_for_card_index(int $index): CardSuit { if ($index < 13) { return CardSuit::SPADES; } else if ($index < 26) { return CardSuit::HEARTS; } else if ($index < 39) { return CardSuit::CLUBS; } else { return CardSuit::DIAMONDS; } } function takes_card_suit(CardSuit $suit) { // … } function legacy_function(int $suit) { $enum_suit = CardSuit::coerce($suit); if ($enum_suit !== null) { takes_card_suit($enum_suit); } } enum CardSuit : int as int {..}

  21. 04. PHP Features Not Supported in Hack

  22. 04. PHP Features Not Supported in Hack ● The global Statement ● Top-Level Code ● Old-Style Constructors ● Case-Insensitive Name Lookup ● Variable Variables ● goto ● Arguments to break and continue

  23. 04. PHP Features Not Supported in Hack ● The global Statement ● Top-Level Code ● Old-Style Constructors ● Case-Insensitive Name Lookup ● Variable Variables ● goto (FYI) ● Mixing Method Call Syntax ● Arguments to break and continue (FYI)

  24. 04. PHP Features Not Supported in Hack -> The global Statement The global statement is forbidden in Hack, because it’s implemented with references under the hood. The statement global $x is syntactic sugar for $x =& $GLOBALS['x'] . ● In partial mode, you can read from and write to the $GLOBALS array without references, so you can use that to work around the lack of a global statement. (In strict mode, Hack will report an error, saying $GLOBALS is an undefined variable.) ●

  25. 04. PHP Features Not Supported in Hack -> Top-Level Code <?hh require_once 'lib/autoload.php'; function main() { setup_autoload(); do_initialization(); $controller = find_controller(); $controller->execute(); } main();

  26. 04. PHP Features Not Supported in Hack -> Old-Style Constructors class Thing { public function Thing() { echo 'constructor!'; } } $t = new Thing();

  27. 04. PHP Features Not Supported in Hack -> Case-Insensitive Name Lookup In PHP, function and class names are looked up case- insensitively. That is, if you define a function named compute , you can call it by writing CoMpUtE() . If you try to do that in Hack, however, the typechecker will report an error, saying the function CoMpUtE is undefined.

  28. 04. PHP Features Not Supported in Hack -> Variable Variables $name = 'x'; $x = 'well this is silly'; echo $$name;

  29. 04. PHP Features Not Supported in Hack -> Dynamic Properties <?hh class C { } function f(): void { $c = new C(); $c->prop = 'hi'; echo $c->prop; }

  30. 04. PHP Features Not Supported in Hack -> Mixing Method Call Syntax class C { public function nonstaticMethod() { } public static function staticMethod() { } } C::nonstaticMethod(); // Allowed in PHP $c = new C(); $c->staticMethod(); // Allowed in PHP

  31. 04. PHP Features Not Supported in Hack -> Mixing Method Call Syntax class C { public function nonstaticMethod() { } public static function staticMethod() { } } C::nonstaticMethod(); // Allowed in PHP $c = new C(); $c->staticMethod(); // Allowed in PHP

  32. 05. Collections

  33. 05. Collections Why Use Collections? Collections Have Reference Semantics Using Collections Type Annotations for Collections Interoperating with Arrays ● ● ● ● ●

  34. 05. Collections -> Why Use Collections? PHP arrays are extremely flexible, but in practice, applications use them in one of a small number of highly specific patterns: vectors, maps, and sets. Using the right type of collection instead makes life easier for both humans and computers. For human readers of code, seeing the names of specific collection classes makes it clearer what their purpose is. This advantage becomes much more potent when com‐ bined with Hack’s type annotations: the purpose of a collection is made clear at every abstraction boundary it passes through. This prevents mistakes and makes development faster and easier. ● ●

  35. 05. Collections -> Using Collections function get_items(): array<string> { static $cache = null; if ($cache === null) { $cache = do_expensive_fetch(); } return $cache; } function main(): void { $items = get_items(); $items[] = some_special_item(); foreach ($items as $item) { // … } }

  36. 05. Collections -> Type Annotations for Collections Most of the time, you shouldn’t use the collection class names themselves in type annotations. Hack provides a large set of interfaces that describe elements of a collection’s functionality, and you should generally use those in type annotations. For example, if you’re writing a function that takes a set of values as an argument and doesn’t modify it, you should annotate the argument as ConstSet, an interface, rather than Set , the concrete class. This increases expressiveness, which helps the typechecker catch more mistakes: if you try to modify the set within the function, there will be a type error. It also makes the function’s contract clear to callers: it wants a set, and it won’t modify it. ● ●

  37. 05. Collections -> Interoperating with Arrays $vector = Vector {'first', 'second'}; $map = Map {10 => 'int', '10' => 'string'}; $array = (array)$map; // Warning: Map::toArray() for a map // containing both int(10) and string('10') var_dump($array); // Prints: array(1) { [10]=> string(6) "string" } print_r((array) $vector); // Prints: Array( [0] => first, [1] => second ) $set = Set {10, "10"} $array = (array)$set; // Warning: Set::toArray() for a // map containing both int(10) and string('10') var_dump($array); // Prints: array(1) { [10]=> string(2) "10" } print_r($vector->toArray()); // Same

  38. 06. Async

  39. 06. Async Drupal && Typing Drupal && Typing && Async && Async ~ 1100%+ ~ 1100%+

  40. 06. Async Introductory Examples Structuring Async Code Sleeping Rescheduling Common Mistakes Async Extensions ● ● ● ● ● ●

  41. 06. Async -> Introductory Examples class C { async function hello(): Awaitable<string> { return 'hello'; } public static async function hello(): Awaitable<string> { return 'hello'; } async function hello_world(): Awaitable<string> { $hello = await hello(); return $hello . ' world'; } public async function goodbye(): Awaitable<string> { return 'goodbye'; } }

  42. 06. Async -> Structuring Async Code function load_user(int $id): <User> { // Call to memcache, database, … } function load_users(array<int> $ids): <Vector<User>> { $result = Vector {}; foreach ($ids as $id) { $result[] = load_user($id); } return $result; }

  43. 06. Async -> Structuring Async Code async function load_user(int $id): Awaitable<User> { // Call to memcache, database, … } async function load_users(array<int> $ids): Awaitable<Vector<User>> { return await HH\Asio\vm($ids, fun('load_user')); }

  44. 06. Async -> Sleeping // See HH\Asio\usleep(). async function sleepForFiveSeconds(): Awaitable<void> { echo "start\n"; await HH\Asio\usleep(5000000); // 5 million microseconds = 5 seconds echo "finish, at least five seconds later\n"; }

  45. 06. Async -> Rescheduling // See HH\Asio\later(). async function poll_for_result(PollingService $svc): Awaitable<mixed> { while (!$svc->isReady()) { await HH\Asio\later(); } return $svc->getResult(); }

  46. 06. Async -> Common Mistakes Dropping Wait Handles ● Memoizing Async Functions ●

  47. 06. Async -> Async Extensions MySQL ● MCRouter and MemCached ● cURL ● Streams ●

  48. 07. XHP

  49. 07. XHP Drupal && XHP Drupal && XHP - (minus) ~ 5-15% - (minus) ~ 5-15%

  50. 07. XHP Why Use XHP? How to Use XHP Creating Your Own XHP Classes Migrating to XHP ○ Converting Bottom-Up ● ● ● ●

More Related