Issue 21 - Today Software Magazine (english)

Page 35

programming

TODAY SOFTWARE MAGAZINE

The HipHop Virtual Machine

T

hese days, when it comes to huge popular websites, performance issues come up often along with the question: „How can we support such a huge userbase at a reasonable price ?”. Facebook, one of the largest websites on the planet, seems to have a pretty good answer to this: the HipHop virtual machine. What is this, how was it born, how does it work and why is it better than the alternatives ? I will try to answer all these questions in the following pages. The information presented here is scattered among many documents, interviews, blogs, wiki pages etc. In this article, I try to provide a big picture view over this product. Keep in mind that some features and functionalities will only be touched upon, not covered in depth, and that others will be omitted entirely. If you want detailed information about a feature of HHVM, please read the project’s documentation or its dedicated blog.

1. What is the HipHop Virtual Machine ? HipHop Virtual Machine (or HHVM) is a PHP execution engine. It was created by Facebook in the (successful) attempt to reduce the load on their webservers, which received more and more traffic as the number of users increased.

2. Istorie

The history of HHVM starts in the summer of 2007 when Facebook started developing HPHPc. HPHPc worked in the following way: • it built an abstract syntax tree, which represented the logical structure of the PHP code • based on that tree, the PHP code was translated to C++ code • using g++, the C++ code was compiled to a binary file • that executable was then uploaded to the webservers where it was executed

In its glory days, HPHPc managed to overperform Zend PHP (the regular PHP platform) by up to 500 %. These impressive results convinced Facebook’s engineers that HPHPc was worth keeping around. They decided to give it a brother: HPHPi (HipHop interpreted). HPHPi is the developer-friendly version of HPHPc and, besides eliminating the compilation step, it offers various tools for the programmers, among which is a code debugger called HPHPd, static code analysis, performance profiling and many more. The two products (HPHPc and HPHPi) were developed and maintained in parallel in order to keep them compatible. The development of HPHPc took 2 years and, at the end of 2009, it ran on about 90 % of Facebook’s production servers. Performance was excellent, the load on the server dropped significantly (50 % in some cases) and everybody was happy. So, in February 2010, HPHPc’s source code was open-sourced and published on GitHub under the PHP License. But Facebook’s engineers realized that superior performance wasn’t going to guarantee HPHPc’s success in the long run. Here’s why: • static compilation took a long time and was cumbersome • the resulting binary had well over 1GB, which made deployment more difficult (new code had to be pushed to production daily) • developing both HPHPc and HPHPi

and keeping them compatible was getting more and more difficult. This was especially true because the syntax trees they used were different and keeping them compatible was no easy task. So, at the beginning of 2010 (right after HPHPc became open-source), Facebook put together a team that had to come up with a viable alternative to HPHPc, one that could be maintained for a long time. A stack–based virtual machine with a JIT compiler seemed to be the answer; the incarnation of this solution gave birth to HipHop Virtual Machine (HHVM). At first, HHVM replaced only HPHPi and was used only for development while HPHPc remained on the production servers. But, at the end of 2012, HHVM exceeded the performance of HPHPc and therefore, in February 2013, all Facebook’s production servers were switched to HHVM.

II. Architecture

HHVM’s general architecture is made up of two webservers, a translator, a JIT compiler and a garbage collector. HHVM doesn’t run on any operating system. More specifically: • a lot of Linux flavours are supported, especially Ubuntu and CentOS • on Mac OS X, HHVM can only run in interpreted mode (no JIT) • there is no support for Windows

www.todaysoftmag.ro | nr. 21/Martie, 2014

35


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.