why choose hack/hhvm over php7

Post on 15-Apr-2017

1.828 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Why choose Hack/HHVM

over PHP7

Intelligence, Ltd.Yuji Otani

1

Presented on Feb 24, 2016

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:

3

5years

→ →

My main experiences with programming languages:

7years1year

4

Question 1

Have you used

Hack/HHVM?

5

Question 2

PHP7 has been released.

Are you uncertain about

what to use next?

6

Goal of this presentation:

Convey the qualities of Hack/HHVM

and make you consider it

7

We have adopted Hack/HHVM in many

projects here at Intelligence since 2015.

The Framework of our choice is FuelPHP.

8

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

9

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

10

• a programming language developed by Facebook

• compatible with PHP

• runs on HHVM (virtual machine)

• based on PHP5.6

What is Hack?

• 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/

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:

13

At the end of last year, the big

happening

14

Dec 3, 2015PHP7 was released!

15

Huge performance improvements!!

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

16

• Null coalesce operator

• EngineException (catchable fatal errors)

• anonymous classes

• scalar type hinting

• return type declaration

New language specifications

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

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

19

Currently PHP faces a big turning point.

That's precisely the reason why I want you to

know the qualities of Hack/HHVM.

20

Point 1

Powerful type hinting

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

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)

23

There are major differences in type

hinting between Hack and 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");

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

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

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

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

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

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

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.

32

Point 2

Collections

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

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

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

36

Map

Stores key-value pairs

B

A

D

EC

37

Vector

Pure stack-like ordered array

21 4 53

38

Set

Ordered collection of unique values

BA D EC

39

Pair

Immutable collection of exactly 2 values

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.

41

Point 3

Original specifications

42

Original features:

• Lambdas

• Generics

• Enum

• Tuples

• Shapes

43

Enum (Enumeration of value)

<?hh

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

}

Enumerate values with a specific type

44

Tuples

<?hh

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

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

return tuple(“OK”, 1);}

Return multiple values from a function

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

46

Point 4

Parallel execution

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.

48

Parallel execution

49

Point 5

Static analysis tool

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.

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.

52

Point 6

Gradually adopting features from 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

54

Point 7

Great adoption record

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

56

Although Hack/HHVM is very nice,

there are also unfavorable qualities

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)

LTS are supported for about 1 year

Hack/HHVM support

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

60

Hard to find resources

Problem

61

PHP7 support in HHVM

62

PHP7 support in HHVM

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

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

64

Issues related to PHP7 can be tracked on GitHub

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

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

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

67

It is great to have options in the future of

an excellent language like PHP!

68

PHP7 is out, but it is still worth to

choose Hack/HHVM!

69

thank you

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

top related