Facebook rebuilds PHP with HACK e HHVM

HHVMFacebook is probably the largest website running on the PHP platform. This “old” language was created by Rasmus Lerdof as a scripting language to facilitate and to speed website realization in the “far” 1995. Main features were dynamic allocation of variable’s type, and the absence of compilation. To edit file and run the web page is enough.

Clearly, these features, if on one hand make developing more easy and more affordable for a larger community, on the other hand heavily affect performance. The Zend engine (the standard php runner) must detect in every variable usage, its type, and convert it in the right type, depending on the context. As a language can simulate the type absence, at the execution layers it’s not possible.

These features though have an important impact  (as many others of php) on performance, they have never been a big issue in general. Suffice it to say that in 1995, people still had 56k lines, then the execution time of the pages were negligible and in any case the sites had a much simpler architecture.

Today, the performance issues of PHP, are not so critical, in fact that is the most widely used web development platform in the world. It’s partly improved the situation by introducing the opcode caching (In practice php interprets the code only the first time, the result is stored in RAM, so you don’t have to repeat this onerous task on subsequent requests) and the caching of pages through  Varnish and Nginx.

These solutions are sure fine for 90% of websites, which at most run on 5-6 server, but definitely not for Facebook. In fact on large infrastructure composed of thousands of servers, the execution of code in half time allows to halve the number of servers. A big costs saving of servers, energy and staff that deals with the management.

HipHopFacebook immediately after achieving fame, found itself facing a crossroads, to change platform by switching to a more powerful language, or to rewrite PHP. Although can you appear obvious the first choice, we must also take into account there are many PHP developers and therefore they are more cheaply. Thus was developed HipHop for PHP, and was also released to public with an open source license. Basically it was a converter, which converted the PHP code in C + + and then compiled it into an executable. Clearly converting PHP code into machine code, drastically improves performance. Facebook, claimed to have reached 600% in performance compared to standard PHP.

Although Facebook had announced HipHop as a great promise for the future of PHP, a few months after the project was abandoned. The creation of an executable that you distribute on the server (even 1GB) was not practice, in addition debugging system was more complex, requiring the development and maintaining of a special hiphop version. Moreover, given the nature of HipHop, functions such as eval() or create_function() could not be implemented, because they clearly are not portable in C + +, being functions that allow the execution of strings as code, and clearly in a platform where the code is already compiled this isn’t possible. Also with regard to the performance enhancements they couldn’t improve anymore, so the team decided to deprecate  HipHop.

And here HHVM came (HipHop Virtual Machine),  it collected the “shards” of the HipHop project, radically changing philosophy. Rather than to convert the code into C + +, they created a JIT engine, a bit like what used by Java and ASP.NET.
Essentially HHVM converts PHP scripts internally in a kind of pseudo code, very close to the machine language. This is then supplied to the JIT compiler, which analyzes and optimizes it for the CPU on which it runs. This new system has simplified the debugging system and has enabled us to achieve even higher performance than before. The major result is that allows to implement all PHP functions, and therefore it will be fully compatible very soon.

Practically there is no more difference using PHP or Zend PHP HHVM. You upload the website on the server, you install HHVM, and then it works as common FastCGI Module. Compatibility with PHP, at present it is not 100% yet, although the Facebook Team is doing its utmost to make HHVM fully compatible passing the unit tests of the major frameworks and PHP applicationsSlim, Laravel, WordPress, Codeigniter and others are already fully compatible.

Reached this optimization, the Facebook team decided to go further. PHP has structural limitations that affect its own syntax. Infacts it is not  possible to specify the type of the variable (which enable to increase performance and to reduce bugs), async functions are missing as many other features present in other languages. So they created HACK, which is nothing more than an extended PHP. This language was already used by Facebook internally for several months, and what little we knew was obtained by studying the HHVM code. Now HHVM is instead released publicly with all the necessary documentation.
Clearly it has support for static types, lambda expressions, asynchronous functions and much more. The documentation is on the official lang website.

The next steps for the team are to achieve the”parity”, the 100% full support of PHP, in particular of its main framework. You can contribute to this goal, being HHVM an open source project, sending your changes and corrections on GitHub.

Certainly HHVM is the beginning, and its features do presuppose and hope that will soon replace PHP in many cases. The founder of PHP thinks that in the future the two projects can converge and merge.

With the stability and compatibility reached (version 3.0 should be released in a few days) I hope to be able to implement the platform in the coming weeks on this same site, in order to test in production the miraculous results.

Leave a Reply