why choose hack/hhvm over php7

70
Why choose Hack/HHVM over PHP7 Intelligence, Ltd. Yuji Otani 1 Presented on Feb 24, 2016

Upload: yuji-otani

Post on 15-Apr-2017

1.828 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Why choose Hack/HHVM over PHP7

Why choose Hack/HHVM

over PHP7

Intelligence, Ltd.Yuji Otani

1

Presented on Feb 24, 2016

Page 2: Why choose Hack/HHVM over PHP7

2

• Born in Shimonoseki, Yamaguchi Prefecture

• Software Engineer at Intelligence, Ltd

• I love cars and programming

• Be my friend on Facebook! https://www.facebook.com/yuji.otani.16

About me:

Page 3: Why choose Hack/HHVM over PHP7

3

5years

→ →

My main experiences with programming languages:

7years1year

Page 4: Why choose Hack/HHVM over PHP7

4

Question 1

Have you used

Hack/HHVM?

Page 5: Why choose Hack/HHVM over PHP7

5

Question 2

PHP7 has been released.

Are you uncertain about

what to use next?

Page 6: Why choose Hack/HHVM over PHP7

6

Goal of this presentation:

Convey the qualities of Hack/HHVM

and make you consider it

Page 7: Why choose Hack/HHVM over PHP7

7

We have adopted Hack/HHVM in many

projects here at Intelligence since 2015.

The Framework of our choice is FuelPHP.

Page 8: Why choose Hack/HHVM over PHP7

8

Example: MIIDAS (job hunting service)https://miidas.jp

Page 9: Why choose Hack/HHVM over PHP7

9

Example: MyRefer (employee referral service)https://i-myrefer.jp/

Page 10: Why choose Hack/HHVM over PHP7

10

• a programming language developed by Facebook

• compatible with PHP

• runs on HHVM (virtual machine)

• based on PHP5.6

What is Hack?

Page 11: Why choose Hack/HHVM over PHP7

• bug-free and fast coding

• enjoyable coding experience

• fast runtime

• fit for large scale systems

The goals of Hack:

http://growthhackjapan.com/2014-03-23-facebook-hack-released-to-the-public/

Page 12: Why choose Hack/HHVM over PHP7

12

• we wanted to challenge something new while

leveraging the experience with PHP

• we wanted to deliver a performant service

• we wanted to keep the ease of readability and

maintainability even when the system grew bigger

• PHP7 was not available yet

Why we chose Hack:

Page 13: Why choose Hack/HHVM over PHP7

13

At the end of last year, the big

happening

Page 14: Why choose Hack/HHVM over PHP7

14

Dec 3, 2015PHP7 was released!

Page 15: Why choose Hack/HHVM over PHP7

15

Huge performance improvements!!

http://talks.php.net/fluent15#/wpbench

Page 16: Why choose Hack/HHVM over PHP7

16

• Null coalesce operator

• EngineException (catchable fatal errors)

• anonymous classes

• scalar type hinting

• return type declaration

New language specifications

Page 17: Why choose Hack/HHVM over PHP7

17

• optimized for 64-bit CPUs

• efficient use of CPU cache

• in-memory arrays became sequences (in PHP5 they were all

associative arrays)

• the memory usage by arrays has improved dramatically. * PHP5 (72bytes) → PHP7 (32bytes)

New data structure

Page 18: Why choose Hack/HHVM over PHP7

18

PHP7 incorporated many features from Hack/HHVM

• scalar type hinting

• return value declaration

• great performance

• lower consumption of memory for arrays

• AST (abstract syntax tree) compiling

Page 19: Why choose Hack/HHVM over PHP7

19

Currently PHP faces a big turning point.

That's precisely the reason why I want you to

know the qualities of Hack/HHVM.

Page 20: Why choose Hack/HHVM over PHP7

20

Point 1

Powerful type hinting

Page 21: Why choose Hack/HHVM over PHP7

21

What is type hinting?

You can specify the type of arguments and return values of functions.

Class Sample {public static function sampleFunc(int $a):

string{return "OK";

}}

//OKSample::sampleFunc (1);

//Fatal ErrorSample::sampleFunc ("a");

Page 22: Why choose Hack/HHVM over PHP7

22

Type hinting in PHP

Types that could be used in type hinting were gradually added.

PHP5.0: Class

PHP5.1: array

PHP5.4: Closure and function

PHP7 : Scalar Type (int, float, string, bool)

Page 23: Why choose Hack/HHVM over PHP7

23

There are major differences in type

hinting between Hack and PHP7

Page 24: Why choose Hack/HHVM over PHP7

24

PHP7 type hinting

There are 2 modes:

• Weakly typed = automatic type casting

• Strongly typed = strict type checking

<?php

Class Sample {public static function sampleFunc(int $a): string{

return "OK";}

}

// OK in both modesSample::sampleFunc(1);

// OK only in weakly typed modeSample::sampleFunc("1");

Page 25: Why choose Hack/HHVM over PHP7

25

PHP7 type hinting

• The default mode is “weakly typed”

• To switch to “strongly typed” mode, it is necessary to

declare it in the beginning of the source files

• It’s only possible to declare “strongly typed” mode per file,

i.e. it’s not possible to declare “strongly typed” mode in

configuration files<?php// "Strongly typed" mode is enabled in this file onlydeclare(strict_types=1);

Page 26: Why choose Hack/HHVM over PHP7

26

PHP7 type hinting

• It is not possible to declare nullable or mixed types.

• Passing Null to a function with type-hinted parameters will

trigger error.<?php

Class Sample {public static function sampleFunc(int $a):

string{return "OK";

}}

//Fatal ErrorSample::sampleFunc(null);

Page 27: Why choose Hack/HHVM over PHP7

27

Hack type hinting

• Only “strongly typed” mode

• Permits any type (including null) by using the “mixed”

keyword<?hh

Class Sample {public static function sampleFunc(mixed $a): string{

return "OK";}

}

//OKSample::sampleFunc(null);Sample::sampleFunc(1);Sample::sampleFunc(”a");

Page 28: Why choose Hack/HHVM over PHP7

28

Hack Type Hinting

• Accepts Null by adding a “?” before the type hint

<?hh

Class Sample {public static function sampleFunc(?int $a): string{

return "OK";}

}

//OKSample::sampleFunc(null);Sample::sampleFunc(1);

Page 29: Why choose Hack/HHVM over PHP7

29

Hack Type Hinting

The types of keys and values of arrays can be specified.However, it is only for static analysis, not runtime.

<?hh

Class Sample {public static function sampleFunc(array<int, string> $a):

string{return "OK";

}}

//OKSample::sampleFunc(array(1 => "a", 2 => "b"));Sample::sampleFunc(array(1 => 1, 2 => null));

Page 30: Why choose Hack/HHVM over PHP7

30

Hack Type Hinting

It is possible to specify types in Enum.<?hh

enum Size: int {MEDIUM = 1;LARGE = 2;

}Class Sample {

public static function sampleFunc(Size $size): string{return "OK";

}}//OKSample::sampleFunc(Size::LARGE);Sample::sampleFunc(2);Sample::sampleFunc(4); // This works too: it checks the type, not the value.

//ErrorSample::sampleFunc(”a");

Page 31: Why choose Hack/HHVM over PHP7

31

Type Hinting

• “Strongly typed” only

• Nullable and mixed types

The specifications put emphasis on

type-sensitive, large scale systems.

• “Weakly typed” by default

• No nullable nor mixed types

The specifications put emphasis on

the speed of type-agnostic

development.

Page 32: Why choose Hack/HHVM over PHP7

32

Point 2

Collections

Page 33: Why choose Hack/HHVM over PHP7

33

PHP5 arrays:

• Arrays and associative arrays can be used in the same way

• Arrays accept values of any type

• Keys can be integer or string

• Values are retrieved in order of insertion, regardless of keys

<?php// the code below prints “ab”$arr = array(1 => ”a", 0 => ”b");foreach($arr as $value ) { print($value);}

Page 34: Why choose Hack/HHVM over PHP7

34

Hack Collections:

• Has original collections: Vector, Map, Set and Pair

• It is possible to specify the types of keys and values, though

they’re not checked during runtime<?hh

$a = new Map<string, string>;

//OK$a->set("key1", "value1");

//OK$a->set("key2", 2);

//OK$a->set(3, array());

Page 35: Why choose Hack/HHVM over PHP7

35

Hack Collection

It’s not necessary to check if the key exists prior to fetching a

value, and it won’t trigger a “Notice” error when the key doesn’t

exist.<?hh

$a = new Map<string, string>;

$a->set("key1", "value1");

// $val1 becomes “value1”$val1 = $a->get("key1");

// $val2 becomes null, no “Notice” error fired$val2 = $a->get("key2");

Page 36: Why choose Hack/HHVM over PHP7

36

Map

Stores key-value pairs

B

A

D

EC

Page 37: Why choose Hack/HHVM over PHP7

37

Vector

Pure stack-like ordered array

21 4 53

Page 38: Why choose Hack/HHVM over PHP7

38

Set

Ordered collection of unique values

BA D EC

Page 39: Why choose Hack/HHVM over PHP7

39

Pair

Immutable collection of exactly 2 values

Page 40: Why choose Hack/HHVM over PHP7

40

Collection

• Four original collections

• It is possible to specify the types

of values (keys as well for Maps)

The specifications put emphasis on

type-sensitive, large scale systems.

• Only arrays that accept any type

• No concern over the types of

arrays and their values

The specifications put emphasis on

the speed of “anything-goes”

associative arrays.

Page 41: Why choose Hack/HHVM over PHP7

41

Point 3

Original specifications

Page 42: Why choose Hack/HHVM over PHP7

42

Original features:

• Lambdas

• Generics

• Enum

• Tuples

• Shapes

Page 43: Why choose Hack/HHVM over PHP7

43

Enum (Enumeration of value)

<?hh

enum Size: int {SMALL = 0;MEDIUM = 1;LARGE = 2;X_LARGE = 3;

}

Enumerate values with a specific type

Page 44: Why choose Hack/HHVM over PHP7

44

Tuples

<?hh

list($a, $b) = testFunc();

public function testFunc() : (string, int) {

return tuple(“OK”, 1);}

Return multiple values from a function

Page 45: Why choose Hack/HHVM over PHP7

45

Original language specification

Hack/HHVM has added many original language specifications.

By using Hack in your service, you can achieve:

• quick development with fewer bugs

• developers actually enjoy coding

Page 46: Why choose Hack/HHVM over PHP7

46

Point 4

Parallel execution

Page 47: Why choose Hack/HHVM over PHP7

47

Parallel execution

It is possible to run statements in parallel by using

original functions such as “Async” and “Await.”

Parallel execution is possible out-of-the-box,

reaching even higher performance speeds.

Page 48: Why choose Hack/HHVM over PHP7

48

Parallel execution

Page 49: Why choose Hack/HHVM over PHP7

49

Point 5

Static analysis tool

Page 50: Why choose Hack/HHVM over PHP7

50

Static analysis tool

The static analysis of the code is done by hh_client. It

checks for syntax errors and inconsistencies, allowing

developers to fix them before runtime.

Code with fewer bugs like this is only possible due to the

strict type restrictions in Hack.

Page 51: Why choose Hack/HHVM over PHP7

51

You can check the program before execution.

• Check for compilation errors

• Type-check the arguments and return values

• Check for discouraged syntax

• Check for inappropriate type casting

Static analysis tool

* configure automatic static analysis with hhvm.hack.lang.auto_typecheck in config files.

Page 52: Why choose Hack/HHVM over PHP7

52

Point 6

Gradually adopting features from PHP7

Page 53: Why choose Hack/HHVM over PHP7

53

Continued support for both PHP5 and 7

• HHVM3.11 implements the new features in PHP7

• The direction is to support both PHP5 and PHP7

• Also possible to break backward compatibility by setting

“hhvm.php7.all = 1” in the settings

• Features from PHP7 can be individually enabled

https://docs.hhvm.com/hhvm/configuration/INI-settings#php-7-settings

Page 54: Why choose Hack/HHVM over PHP7

54

Point 7

Great adoption record

Page 55: Why choose Hack/HHVM over PHP7

55

Great adoption record

The adoption continues to grow, especially among large scale

services. Wikipedia and Baidu have many commits into the

source code to Hack/HHVM.

https://github.com/facebook/hhvm/wiki/Users

Page 56: Why choose Hack/HHVM over PHP7

56

Although Hack/HHVM is very nice,

there are also unfavorable qualities

Page 57: Why choose Hack/HHVM over PHP7

57

Release cycle

Hack (HHVM)

• Released every 8 weeks

• Every 3rd version is turned into the LTS version, which is

supported for one year

PHP

• Released every 1 year

• Life cycle of 3 years (bug fixes 2 years, security 1 year)

Page 58: Why choose Hack/HHVM over PHP7

LTS are supported for about 1 year

Hack/HHVM support

Page 59: Why choose Hack/HHVM over PHP7

59

• HHVM crash: needed to monitor to restart

automatically

• pecl not supported: tried replacing some with

golang, but currently venturing into HNI

• Had to upgrade CentOS6 to CentOS7 due to

sudden end of support

• Most of IDEs don’t support it

Some issues we faced in production

Page 60: Why choose Hack/HHVM over PHP7

60

Hard to find resources

Problem

Page 61: Why choose Hack/HHVM over PHP7

61

PHP7 support in HHVM

Page 62: Why choose Hack/HHVM over PHP7

62

PHP7 support in HHVM

http://hhvm.com/blog/10859/php-7-support

Page 63: Why choose Hack/HHVM over PHP7

63

TLDR;

• The release of PHP7 is also fortunate to HHVM

• New features in PHP7 will be available from HHVM3.11

• HHVM will continue supporting both PHP5 and PHP7

• Possible to break backward compatibility by setting

“hhvm.php7.all = 1” in the settings

https://docs.hhvm.com/hhvm/configuration/INI-settings#php-7-settings

Page 64: Why choose Hack/HHVM over PHP7

64

Issues related to PHP7 can be tracked on GitHub

https://github.com/facebook/hhvm/labels/php7%20incompatibility

Page 65: Why choose Hack/HHVM over PHP7

65

Wrap-up

• PHP7 and Hack are similar, but with different

features

• Hack seems to more suited to large scale systems

• Hack didn’t split from PHP, it synchronizes and

evolves together

Page 66: Why choose Hack/HHVM over PHP7

66

PHP specifications are community-driven, while Hack is

mainly developed by Facebook.

I think PHP values the “loose PHP-ness,” while Hack

values the “correct practical use.”

Final thoughts

Page 67: Why choose Hack/HHVM over PHP7

67

It is great to have options in the future of

an excellent language like PHP!

Page 68: Why choose Hack/HHVM over PHP7

68

PHP7 is out, but it is still worth to

choose Hack/HHVM!

Page 69: Why choose Hack/HHVM over PHP7

69

thank you

Page 70: Why choose Hack/HHVM over PHP7

70

This document was created with accuracy in mind, but

the author does not guarantee its veracity nor its

usefulness. In addition, this material was created solely

by the author, and it does not represent the views,

values, etc. of any organization.

Disclaimer