HHVM and Hack – Part 1

Posted by Unknown on Wednesday, February 12, 2014

This entry is part 1 of 1 in the series HHVM and Hack

Facebook started to work on HipHop for PHP in 2008. Their goal was to speed up the PHP execution speed and the first version of the project was composed of the tandem HPHPc/HPHPi. HPHPc is a PHP to C++ transpiler which was used to deploy the code to the production servers while HPHPi was an interpreter used during development and debug stages.


HPHPc did a good job on improving performance but it was not without issues: maintaining both HPHPc and HPHPi in sync proved to be cumbersome plus some differences still existed between the transpiled code vs the interpreted one. That's why back in 2010 Facebook decided to go for another approach and created HHVM – a new virtual machine designed to replace the Zend Engine used by PHP. By the end of 2012 HHVM achieved performance parity with the former HPHPc and soon surpassed it.


HHVM is intended to achieve both parity with the Zend Engine features and best possible performances. Facebook claims a 3x to 10x speed boost and 1/2 memory footprint by switching from PHP+APC to HHVM. Of course this is really application dependent (10x being for the FB code base). This article will not focus on parity nor performances as plenty of resources are already available, check the HHVM blog or google for "hhvm benchmark". To find out more about HipHop and HHVM in general, read the previous SitePoint articles about it.


Instead this article will focus on HACK which is an evolution of the PHP language designed to be safer, to enable better performance and to improve developer efficiency. Note that both HACK and PHP are equally supported by the HHVM. Despite the fact that HACK is in use at Facebook on all the production servers, only little info has leaked for now. In a nutshell, HACK is Facebook's PHP6 – it proposes to fix most of what's wrong with PHP today, while adding some new features like static typing along the way.


Hello HACK


Not all the tools and almost no documentation for HACK have been released as of today. However the latest available HHVM source code already supports HACK. You can install a Vagrant virtual machine to start experimenting with HACK and run the code snippets from this article:



# Vagrant should be installed on your machine
$ git clone git@github.com:vicb/hhvm-vagrant.git
$ cd hhvm-vagrant
$ vagrant up


You are now ready to write your first HACK program:



<?hh
require "/vagrant/www/xhp/php-lib/init.php";

$hello = "Hello HACK!";

echo <html>
<head>
<title>{$hello}!</title>
</head>
<body>
<h1>{$hello}</h1>
</body>
</html>;


This sample is available in your clone of the vagrant VM (located at www/hello/index.php). You can see the result by pointing your browser to http://localhost:8080/hello/.


A HACK application starts with the <?hh tag located at the very start of your source file – note that there is no closing tag in HACK. This sample is pretty self-explanatory, the only noteworthy point is that is uses XHP, an XML markup language which is also available as a stock PHP extension. More about XHP later in this article.


More interesting HACK goodness will be introduced the following sections.


Constructor argument promotion


Not only is HACK more efficient than stock PHP when it comes to execution speed but it also focuses on improving developer efficiency.


"Constructor argument promotion" is one of the features helping reducing boilerplate code. Indeed, a common pattern when creating a class in PHP is first to declare the properties and then assign all of them straight from constructor arguments. With HACK, adding the visibility as a constructor argument modifier is enough for both declaring the property and initializing it. It makes the code more concise:



<?hh
class PHPClass {
public $pub;
protected $pro;
private $pri;

public function __construct($pub, $pro, $pri) {
$this->pub = $pub;
$this->pro = $pro;
$this->pri = $pri;
}
}

// object(PHPC


Truncated by Planet PHP, read more at the original (another 7544 bytes)




more

{ 0 comments... » HHVM and Hack – Part 1 read them below or add one }

Post a Comment

Popular Posts