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

HHVM/Hackで得る問題解決力 / hhvm-hack-problem-solving

yuuki takezawa
September 04, 2018

HHVM/Hackで得る問題解決力 / hhvm-hack-problem-solving

builderscon 2018 tokyo の資料です

yuuki takezawa

September 04, 2018
Tweet

More Decks by yuuki takezawa

Other Decks in Technology

Transcript

  1. HHVM/HackͰಘΔ໰୊ղܾྗ
    yuuki takezawa
    builderscon 2018

    View full-size slide

  2. Profile
    • ஛ᖒ ༗و / ytake
    • גࣜձࣾΞΠελΠϧ CTO
    • PHP, Hack, Go, Scala
    • Apache Hadoop, Apache Spark, Apache Kafka


    View full-size slide

  3. ͱΓ͋͛Δ͜ͱ
    • ༷ʑͳ໰୊ղܾ
    • HackʹΑΔ໰୊ղܾ
    • ΞϓϦέʔγϣϯʹ͓͚Δ໰୊ղܾ

    View full-size slide

  4. ໰୊ղܾͱ͸
    • ໰୊Λղܾ͢Δɺ

    ͢ͳΘͪղΛൃݟ͢Δ͜ͱͰ͋Γɺ

    ࢥߟͷҰ෦෼Ͱ͋Δ
    • νʔϜʹ͓͚ΔࢥߟͰ͋ͬͨΓ

    ΦϒδΣΫτࢦ޲Ͱ͋ͬͨΓ

    View full-size slide

  5. ͳͥHackΛ࢖͏ͷ͔

    View full-size slide

  6. എܠ
    • શମతʹ͸PHPϝΠϯͷٕज़ελοΫ
    • WebΞϓϦέʔγϣϯ͕ϝΠϯ
    • Go੡ͷΞϓϦέʔγϣϯ΋͍͔ͭ͘
    • ෼ੳॲཧ͸Scala੡͕͍͔ͭ͘

    View full-size slide

  7. ݒ೦
    • ͦΕ XX Ͱ΋Ͱ͖ΔΑ

    • ಛఆͷݴޠ͔͠஌Βͳ͍
    • ։ൃͱ͍͏ߦҝʹΫϦΤΠςΟϒ͕͞ײ͡ΒΕͳ͍

    View full-size slide

  8. બ୒ͷύϥυοΫε
    https://www.ted.com/talks/barry_schwartz_on_the_paradox_of_choice?language=ja

    View full-size slide

  9. ΞϓϦέʔγϣϯ͸
    ϦϦʔε͢Δ·ͰͰ͸ͳ͘ɺ
    ϦϦʔε͔ͯ͠Β͕ຊ൪

    View full-size slide

  10. ։ൃऀ͸
    ΞϓϦέʔγϣϯͱͱ΋ʹ
    ੒௕͢Δ

    View full-size slide

  11. ։ൃͷଊ͑ํ
    • ͍ͭ΋ͷࢹ఺ͱগ͠ҧ͏ࢹ఺Λ௥Ճͯ͠ΈΔ
    • ҧ͏ࢹ఺ͷ͋ͱɺ͍ͭ΋ͷݴޠ͔Β
    • ૒ํ޲ʹಇ͘ݟ͑ํΛཆ͏͜ͱ͸ѱ͍͜ͱʁ
    • ͍ͭ΋ͷࢹ఺͚ͩɺ͸ѱ͍͜ͱʁ

    View full-size slide

  12. ࢹ఺Λม͑Δํ๏ͷҰͭͱͯ͠
    HackΛར༻

    View full-size slide

  13. PHP͕
    ͪΐͬͱૣ͘ͳͬͨ΍ͭͰ͠ΐʁ

    View full-size slide

  14. HHVM/Hack
    • ݴޠͷϕʔεʹ͋Δͷ͸PHPͷվળ
    • I/O͕ൃੜ͢Δ΋ͷʹରͯ͠ͷAsync/Await

    શͯʹରͯ͠࡞༻͢ΔΘ͚Ͱ͸ͳ͍ͷͰɺෑډ͕௿͍

    • ݫ֨ͳܕνΣοΫͱίϨΫγϣϯɾ੬ऑੑରࡦ

    View full-size slide

  15. for Developer
    • Atom + Nuclide
    • Visual Studio Code + Hack plugin
    • Docker (hhvm/hhvm)

    View full-size slide

  16. .hhconfig ͷجຊ
    • HackͰ࣮ߦ؀ڥʹઃஔ͢ΔϑΝΠϧ
    • ༷ʑͳઃఆΛهड़Ͱ͖Δ
    • PHPར༻Λ૝ఆ͠ͳ͍(PHPࠞࡏෆՄ) 

    assume_php = false(default: true)

    • Type Checker Ұ෦ແࢹ

    ignored_paths = [ "vendor/hhvm/hhast/.+" ]

    View full-size slide

  17. .hhconfig ͋Ε͜Ε
    • λΠϓνΣοΧʔͷϝϞϦૢ࡞

    • ࢀর౉͠࢖͏ͳϞʔυ΋௥Ճ(3.28)

    disallow_return_by_ref

    disallow_array_cell_pass_by_ref

    View full-size slide

  18. ۤ࿑
    • IDEʹ͓͚Δิ׬ػೳͷಈ͖

    • Τϥʔ಺༰͕Θ͔Βͳ͍
    • PHPʹݟ͍͑ͯͯผ෺ͱ͍͏ೝࣝΛ΋ͭ·Ͱ
    • ৘ใऩूྗ

    View full-size slide

  19. Type Checker

    View full-size slide

  20. Type Checker
    • ίϯύΠϥϥΠΫʹಈ͘ܕνΣοΫπʔϧ
    • Ϟʔυ͸3ͭ Partial / Strict / Decl 

    • σϑΥϧτͰ͸Partial

    View full-size slide

  21. Type Checker: Partial
    • PHPͷܕએݴ strictͱಉఔ౓
    • ඞཁҎ্ʹܕνΣοΫ͸͠ͳ͍
    • ओʹPHPͷίʔυΛ Hackͱ࣮ͯ͠ߦ͢Δ

    ίʔυҠ২தͷϑΝΠϧ౳Ͱར༻
    • ࢀর౉͠ ར༻Մೳ

    View full-size slide

  22. Type Checker: Decl
    • • ܕνΣοΫ͸͠ͳ͍
    • ଞͷίʔυνΣοΫ࣌ʹ͸ࢀর͞ΕΔ
    • New Hack code should never be written 

    in decl mode

    View full-size slide

  23. Type Checker: Strict
    • • PHPґଘ͕ͳ͘ɺ100% HackͰ࣮૷͢Δ৔߹ʹબ୒
    • ݫ֨ͳܕνΣοΫΛߦ͏ϞʔυͷͨΊɺ

    ίʔυϨϏϡʔ࣌ͷܕએݴʹ͍ͭͯͷٞ࿦͸ඞཁ࠷খݶ
    • ૝ఆ֎ͷܕม׵ͳͲ͸ߦΘΕͳ͍ͨΊɺ

    ϨϏϡʔ͸ΫϥεઃܭɾΞʔΩςΫνϟͳͲʹ

    ϑΥʔΧεͰ͖Δ

    View full-size slide

  24. for Example
    • ίϯετϥΫλͰॳظԽ͍ͯ͠ͳ͍ίʔυ͸ܯࠂ
    • isset͡Όͳͯ͘ɺarray_key_existsΛ࢖͍ͳ͍͞
    • ͨͩͷarray͡ΌΘ͔Βͳ͍ Vector͔Map࢖͍ͳ͍͞

    View full-size slide

  25. ߟ͑ํΛม͑Δ
    • ͱΓ͋͑ͣarray

    ͦͷarray͸ͲΜͳ໾ׂ͕͋ΓɺͲ͏͍͏΋ͷͳͷ͔
    • arrayͰ͋Δඞཁ͕͋Δͷ͔Ͳ͏͔

    View full-size slide

  26. Hackͷarray͸ࡾछྨ
    • array

    ௨ৗͷ഑ྻ
    • varray

    ஋ͷΈͰߏ੒͞ΕΔ഑ྻ
    • darray

    σΟΫγϣφϦͷ഑ྻ

    View full-size slide

  27. class Sample {
    protected varray $varray = varray[
    'php',
    'hack'
    ];
    protected darray $darray = darray[
    'testing' => 'testing',
    1 => 'testing'
    ];
    public function failedVArray(): varray {
    return $this->varray;
    }
    public function getDArray(): darray {
    return $this->darray;
    }
    }
    ܕҧ͍
    ໭Γܕҧ͍
    ໭Γܕҧ͍

    View full-size slide

  28. ΞϓϦέʔγϣϯʹ͓͚Δ
    ໰୊ղܾ

    View full-size slide

  29. ܧঝΛ੍ޚ͢Δ
    • Sealed Class
    • Sealed Interface
    • Hack 3.27 Ҏ߱

    View full-size slide

  30. <<__Sealed(Hoge::class)>>
    class SealedClass {
    }
    <<__Sealed(Sample::class)>>
    interface SealedInterface {
    }
    ࢦఆͨ͠ΫϥεҎ֎
    ܧঝෆՄ
    ࢦఆͨ͠ΫϥεҎ֎
    ܧঝෆՄɾ࣮૷ෆՄ

    View full-size slide

  31. ܧঝʹ͍ͭͯͷߟ͑ํ
    • final classͷΈͰ੍໿(ڐՄ)Λ͔͚Δ͔

    • GenericsͰ஋දݱͷΈڐՄ͢Δͷ͔

    • ͦͷޙͷΞϓϦέʔγϣϯͷ੒௕ͱɺ

    ֦ுੑͰେ͖͘Ξϓϩʔν͕ҟͳΔ

    View full-size slide

  32. public function sum(): int
    {
    return 10 + "5e2";
    }

    View full-size slide

  33. public function sum(): int
    {
    return 10 + "5e2";
    }
    Typing error
    This is a num (int/float) because
    this is used in an arithmetic
    operation.
    It is incompatible with a string.

    View full-size slide

  34. ܕม׵: mixed

    View full-size slide

  35. public function get($id): mixed
    {
    return //Կ͔Λฦ٫͢Δ;
    }
    $container = new Container();
    $container->get('something');

    View full-size slide

  36. private function invariantLoggerInterface(
    Container $container,
    ): LoggerInterface {
    $logger = $container
    ->get(LoggerInterface::class);
    invariant(
    $logger instanceof LoggerInterface,
    "Interface '\Psr\Log\LoggerInterface' is not
    implemented by this class",
    );
    return $logger;
    }
    mixedͷ৔߹͸Կ͕ฦ٫͞ΕΔ͔Θ͔Βͳ͍
    ظ଴஋ͷ΋ͷ͕ฦ٫͞ΕΔ͔Ͳ͏͔
    ඞͣهड़ͯ͠ɺTypeChecker޲͚ʹهड़

    View full-size slide

  37. final class Util {
    public function something(mixed $any): mixed {
    if($any is int) {
    //
    }
    if($any is string) {
    //
    }
    }
    }

    View full-size slide

  38. Type Constants

    View full-size slide

  39. interface TypeInterface {
    abstract const type T;
    public function getNative(): this::T;
    }

    View full-size slide

  40. class UserType implements TypeInterface {
    const type T = Vector;
    public function getNative(): this::T {
    return new Vector([1,2]);
    }
    }

    View full-size slide

  41. class Sample
    {
    }

    View full-size slide

  42. final class Factory
    {
    protected array $array = [
    'Sample' => Sample::class
    ];
    public function get(string $id)
    {
    $key = \ucfirst(\strtolower($id));
    if(\array_key_exists($key, $this->array)) {
    $class = $this->array[$key];
    return new $class();
    }
    }
    }
    class_existsͳͲ΋

    View full-size slide

  43. <<__ConsistentConstruct>>
    class Sample {
    }
    constructor੍ޚ

    View full-size slide

  44. final class Factory {
    protected Map> $map = Map{
    'Sample' => Sample::class
    };
    public function get(string $id): Sample {
    $class = $this->map->get(\ucfirst(\strtolower($id)));
    if (!\is_null($class)) {
    return new $class();
    }
    throw new \RuntimeException();
    }
    }
    classจࣈྻࢦఆ
    ࣮֬ͳΠϯελϯεੜ੒

    View full-size slide

  45. ࣝผͷҧ͍

    View full-size slide

  46. final class BookId
    {
    private $id;
    public function __construct(string $id)
    {
    $this->id = $id;
    }
    public function getValue(): string
    {
    return $this->id;
    }
    }

    View full-size slide

  47. final class BookId {
    public function __construct(
    private string $id
    ) {}
    public function getValue(): string {
    return $this->id;
    }
    }

    View full-size slide

  48. ҧ͏දݱํ๏

    View full-size slide

  49. abstract class Identifier {
    public function __construct(
    protected T $id
    ) {}
    public function getValue(): T {
    return $this->id;
    }
    }
    final class BookId extends Identifier {
    }
    Generics

    View full-size slide

  50. ॻ੶දݱํ๏

    View full-size slide

  51. class Book
    {
    private $id;
    private $title;
    private $price;
    public function __construct(
    BookId $id,
    BookTitle $title,
    Price $price
    ) {
    $this->id = $id;
    $this->title = $title;
    $this->price = $price;
    }
    public function getId(): BookId {
    return $this->id;
    }
    // লུ
    }

    View full-size slide

  52. class Book {
    public function __construct(
    private BookId $id,
    private BookTitle $title,
    private Price $price
    ) {}
    public function getId(): BookId {
    return $this->id;
    }
    // লུ
    }
    ݎ੍͍໿
    ݎ੍͍໿

    View full-size slide

  53. object(Book)#2 (3) {
    ["id":"Book":private]=>
    object(BookId)#3 (1) {
    ["id":protected]=>
    string(36) "6ad4bb95-262c-4a5b-a6c7-ac9b5cadf707"
    }
    ["title":"Book":private]=>
    string(18) "HHVM/Hack Practice"
    ["price":"Book":private]=>
    int(2999)
    }

    View full-size slide

  54. ॻ੶ίϨΫγϣϯදݱํ๏

    View full-size slide

  55. class BookCollection
    {
    protected $books = [];
    public function __construct(array $books = [])
    {
    $this->books = $books;
    }
    public function toArray(): array {
    $books = [];
    foreach($this->books as $book) {
    $books[] = new Book(
    new BookId($book['id']),
    new BookTitle($book['title']),
    new Price($book['price'])
    );
    }
    return $books;
    }
    }
    ϑΟʔϧυ੍໿ͳ͠

    View full-size slide

  56. type BookShape = shape(
    'book_id' => string,
    'title' => string,
    'price' => int
    );
    ϑΟʔϧυ੍໿

    View full-size slide

  57. class BookCollection {
    protected Vector $v = Vector{ };
    public function __construct(
    protected varray $array
    ) {
    $this->v = $this->vec();
    }
    protected function vec(): Vector {
    $v = Vector{ };
    foreach($this->array as $row) {
    $v->add(new Book(
    new BookId($row['book_id']),
    new BookTitle($row['title']),
    new Price($row['price'])
    ));
    }
    return $v;
    }
    public function toArray(): varray {
    return $this->v->toVArray();
    }
    }
    shapeΛvarrayͰ
    to Vector

    View full-size slide

  58. $v = new Vector([
    new Book('1234', 'testing', 2999),
    new Book('1235', 'testing', 2999),
    ]);
    $v = $v->filter(
    ($t) ==> $t->getId() === '1234'
    )->immutable();
    collection filter
    Πϛϡʔλϒϧʹ

    View full-size slide

  59. ෳ਺ҙຯΛ࣋ͨͤͳ͍ͨΊͷ੍ޚ
    • ౎߹ͷ͍͍ΫϥεΛ࡞Βͳ͍

    • ू໿ΛΑΓߟ͑Δ

    • ͳΜͰ΋Ͱ͖ΔΫϥεͰ͸ͳ͍

    ͜Ε͔͠Ͱ͖ͳ͍Ϋϥε

    View full-size slide

  60. ݫ֨ͳܕΛPHPΞϓϦέʔγϣϯʹద༻͢Δҙຯ
    • ͱΓ͋͑ͣಈ͘ঢ়ଶʹͰ͖Ε͹͍͍
    • ࢓༷มߋ͕͋Ε͹ܕ͕ͳ͍ํ͕ଟ༷ੑ͋ΔΜ͡Όͳ͍ʁ
    • ͦ΋ͦ΋ଞͷݴޠͰΑ͘ͳ͍ʁ

    View full-size slide

  61. ͱΓ͋͑ͣಈ͘ঢ়ଶ͔Β

    ៉ྷʹ͢Δ࣌ؒ͸ͳ͔ͳ͔དྷͳ͍

    View full-size slide

  62. બ୒ࢶͱࢹ໺Λ޿͛Δ

    View full-size slide

  63. ໰୊Λղܾ͢Δɺ

    ͢ͳΘͪղΛൃݟ͢Δ͜ͱͰ͋Γɺ

    ࢥߟͷҰ෦෼

    View full-size slide

  64. ղܾ͢ΔͨΊͷྗͱ
    ࢹ໺

    View full-size slide