25
Hack & HHVM by EWERE DIAGBOYA

Hack and HHVM

Embed Size (px)

Citation preview

Page 1: Hack and HHVM

Hack & HHVM

byEWERE DIAGBOYA

Page 2: Hack and HHVM

Table of Contents- What is Hack ?- What is HHVM?- Installing HHVM- Some Statistics on HHVM- Features of Hack- Code Comparison/Similarity- Who is using HHVM- Pros and Cons- Conclusion- Demo

Page 3: Hack and HHVM

What is HackHack is a programming language for HHVM. Hack reconciles the fast development cycle of a dynamically typed language with the discipline provided by static typing, while many features commonly found in other modern languages.

Hack was inspired by PHP, C# and Java so it brings the features of these languages together into itself to form a single language with syntax very close to PHP (developed by Facebook)

Page 4: Hack and HHVM

What is HHVMHipHop Virtual Machine (HHVM) is a process virtual machine based on just-in-time (JIT) compilation, serving as an execution engine for PHP and Hack programming languages.

By using the principle of JIT compilation, executed PHP or Hack code is first transformed into intermediate HipHop bytecode (HHBC), which is then dynamically translated into the x86-64 machine code, optimized and natively executed (developed by Facebook)

Page 5: Hack and HHVM

HHVM In Summary

Rather than directly interpret or compile PHP code directly to C++, HHVM compiles Hack and PHP into an intermediate bytecode. This bytecode is then translated into x64 machine code dynamically at runtime by a just-in-time (JIT) compiler.

This compilation process allows for all sorts of optimizations that cannot be made in a statically compiled binary, thus enabling higher performance of your Hack and PHP programs.

Page 6: Hack and HHVM

NB: It actually takes a while to compile the code on HHVM but once compiled it can then run faster and has cache for storing unchanged files to enable it run faster

Page 7: Hack and HHVM

Installing HHVMHHVM has Linux ready distros to be deployed on Ubuntu and others. It is quite easy to install. But HHVM does not work on all its own, it is configured either on Nginx or Apache Server

Page 8: Hack and HHVM

Some Statistics

Page 9: Hack and HHVM

StatisticsAccording to Facebook, HHVM has realized over a 9x increase in web request throughput and over a 5x reduction in memory consumption for Facebook compared with the Zend PHP engine + APC (which is the current way of hosting a large majority of PHP applications).

Page 10: Hack and HHVM

Source: OSCON

Page 11: Hack and HHVM

Source: OSCON

Page 12: Hack and HHVM

Some Features of Hack- Gradual Typing (Static and Dynamic Typing) *

- Collections (Vectors, Set, Map, StableMap)

- Generics *

- Async Functions *

- Lambda Expression

- Function Typing

- Shapes, Type Aliasing and more

Page 13: Hack and HHVM

Gradual Typing in HackHack has a unique typing system where the user can choose to use strict typing or decide not to use typing at all, which makes it similar to its influencer PHP.

But Facebook warns that to get the full power of HHVM strict typing is recommended

Page 14: Hack and HHVM

Gradual Typing in Hack

<?phpstring $name = “This is PHP”;

PHP Code

<?hhstring $name = “I am learning Hack”;

Hack Code

Sample Code

Data Types supported in Hack include: bool, int, float, num, string

Page 15: Hack and HHVM

GenericsGeneric programming is a style of computer programming in which algorithms are written in terms of typesto-be-specified-later that are then instantiated when needed for specific types provided as parameters.

In simple terms it allows classes and methods to be parameterized (i.e. a type associated when a class is instantiated or a method is called

Page 16: Hack and HHVM

Generics (Sample Code)

<?hhclass Box<T> { protected T $data;

public function __construct(T $data) { $this->data = $data; }

public function getData(): T { return $this->data; }}

Hack Code

Page 17: Hack and HHVM

Async FunctionsAsynchronous programming refers to a programming design pattern that allows several distinct tasks to cooperatively transfer control to one another on a given thread of execution. The tasks are isolated from one another from the perspective of dependency minimization and task interaction. Asynchronous programming is most often used in the context of input/output (I/O)

Allow multiple functions to run cooperatively

Page 18: Hack and HHVM

Async Functions (Sample Code)

<?hhasync function getPage($url) :

Awaitable<string> {sfp = fopen($url, ‘r’);return await async_get_contents($fp);}// Sends all requests in parallel,// blocks for response$pages = await (

getPage(‘http://php.net’),getpage(‘http://example.com’),getPage(‘http://hhvm.com’),

);

Hack Code

Page 19: Hack and HHVM

Comparison & Similarity

<?hhclass MyClass { public function alpha(): int { return 1; }

public function beta(): string { return 'hi test'; }}

function f(MyClass $my_inst): string { return $my_inst->beta();}

Hack Code

<?phpclass MyClass { public function alpha() { return 1 }

public function beta() { return ‘hi test’; }

function f() {return $this->beta();

}

PHP Code

Page 20: Hack and HHVM

Who is using HHVM ?

Page 21: Hack and HHVM

Advantages of Hack & HHVM- More Features in Hack than PHP

- Runs faster than PHP- Higher general perform and less resource usage- Compatible with alot of CMS and Frameworks

(Magento, CodeIgniter, CakePHP, Laravel etc)- Compiles to a more efficient native code- Only recompiles when changes are made to a file

- All HTTP verbs are instact like in PHP (POST, GET etc)

Page 22: Hack and HHVM

Disadvantages of Hack & HHVM- Compile Time might be high

- Learning a new syntax might take a while

- Incompatibility issues with PHP & HHVM

- Facebook could stop support for Hack & HHVM which could slow down its growth

- Does not allow direct HTML in its code

Page 23: Hack and HHVM

ConclusionPHP is an old language and very dear to alot of us but Hack and HHVM show us better ways to improve our code and build features that do not currently exist on PHP, and give the performance PHP is yet to give.

The final option might be to run good old PHP on HHVM and still get good performance or learn Hack and use the new tools it presents. Either way the goal is to increase performance with less resources and give the user a better experience of the application

Page 24: Hack and HHVM

DemoSimple POST in Hack

Page 25: Hack and HHVM

Thank you

Further Readingwww.hacklang.orgwww.hhvm.comwww.github.com/facebook/hhvmwww.github.com/hhvm