A Chat With Drew Paroski, Co-Creator of HHVM and Hack

The HipHop Virtual Machine (HHVM) and the Hack programming language haven’t really gotten the coverage they deserve. The biggest news in DevOps culture over the past few years had to do with the performance of PHP 7 and the Zend Engine 3. The quieter developments in HHVM may end up being a more influential trend in the long run. I wanted to find out more about what drove these new technologies and where they’re headed, so I sat down with Drew Paroski, co-creator of HHVM and Hack. Here are some highlights from our wide-ranging discussion.

What Are Hack and HHVM?

You really can’t define either one without talking about the other. They were developed around the same time to improve application performance and developer productivity. HHVM is a virtual machine (now open source) designed to execute Facebook’s upgraded version of PHP, which became known as the Hack programming language. The power of HHVM derives from an original just-in-time (JIT) compiler that was more flexible and delivered better performance than existing PHP.

How HHVM Began at Facebook

Paroski is now a senior architect and engineering manager at MemSQL, but in 2009 Facebook hired him away from Microsoft to help solve a massive problem. That was the year that Facebook’s membership doubled from January to September, rising up to 300 million. As their dependence on PHP expanded, they needed more servers running more code and it was clear where that was going. Bugs, code maintenance and infrastructure expenses were tracking right alongside growth forecasts. A level shift had to happen to improve the speed and the performance of PHP.

It all started as HipHop for PHP, which was open sourced in 2010. Paroski explained, “HipHop for PHP was an ahead-of-time compiler, and the centerpiece of its optimization strategy was to leverage type inference to generate more efficient type-specialized code. Unfortunately, it’s often impossible to statically infer the type of a given PHP variable with certainty. That was arguably the biggest technical challenge that inspired the creation of HHVM.” Paroski continued, “HHVM had the major advantage of a being a just-in-time (JIT) compiler which doesn’t have to generate all the native code for your application up front. It could do it in pieces and observe details about how your program behaves as it actually runs. We were able to capitalize on those details to perform type inference and generate type-specialized code on the fly while the program was running.”

Metrics and Wider Applications

HHVM turned out to be a big win for performance after launching to production in early 2013. “Each new release of HHVM delivers improvements in terms of memory usage and CPU utilization per request. Improving these metrics ultimately translates to being able to serve more requests per second with a single machine, meaning that you don’t need as many web servers to serve the same amount of traffic”, said Paroski.

Up to that point, Paroski and his fellow engineers were just tuning HHVM’s performance exclusively for Facebook’s PHP codebase. “We realized that it would be useful to people outside of Facebook after it launched to production. It was no longer this toy of ours but something that real large websites would want. We wanted it to be something that the community could contribute to and other people could use. That’s important for the long term success and viability of a language execution engine.”

The Impact of PHP 7

Just as HHVM was making a name for itself in the DevOps community, PHP finally addressed many of its performance issues with the new PHP 7 in 2014 and the first stable release a year later. “PHP 7 made great strides in terms of performance over various PHP 5 releases,” Paroski said. “Even going from PHP 5.3 to 5.4, there were noticeable improvements. In PHP 7, they definitely put the development focus and energy on improving performance. I like to think that this was motivated in part by some healthy competition from HHVM, but I don’t mean to take focus away from the hard work of all those who contributed to building PHP 7.”

What PHP 7 didn’t bring though, was a JIT compiler. Like many other dynamic language execution engines, Zend Engine 3 still uses an interpreter-based approach to execute PHP applications. Paroski pointed out that, “They have a very advanced and highly tuned interpreter, but the point is that they are not actually compiling PHP to native code. That makes the performance improvements of PHP7 even more impressive.”

For developers who are devoted to the finer points of performance optimization, what PHP 7 developers actually did is a fascinating lesson. Paroski explained, “A lot of the performance improvements in PHP 7 came from reducing memory usage and improving memory access and allocation patterns. This includes a lot of the classic things that people who are tuning for performance pay attention to – things like making common data structures smaller, reducing the number of indirections and allocations, etc. Reducing memory usage is particularly important for larger applications, because it reduces the rate of cache misses which improves application speed.”

Details on Performance Optimization

We covered some of the most important details on PHP 7 improvements in an earlier blog, but I wanted to know what Paroski found most valuable. He responded, “One important detail, for example, has to do with how the engine represents a variable. They have this data structure called a ‘zval’, and before PHP 7 every variable slot was a pointer to a zval which had to be dynamically allocated. The zval in turn would contain a type tag and often a pointer or handle to some other block of memory such as an object or an array.”

“So every time the engine needed to get at the contents of some object, it had to dereference the pointer that’s in the variable slot to get to the zval, and then it had to dereference the pointer or handle inside the zval to get to the contents of the object. In PHP 7, they got rid of this two hop scheme. This improvement by itself likely had a huge impact on reducing the number of memory allocations, reducing memory usage, and improving memory access patterns.”

Wikipedia’s HHVM Conversion

While PHP was addressing issues like these, companies with large-scale PHP applications turned to HHVM including Box and Wikimedia. I was especially interested in what convinced these large scale operations to take the plunge and adopt something that was essentially a brand new technology.

Paroski recalled, “We started talking with some of the folks at the Wikimedia foundation, which hosts wikipedia.org. Naturally, there were around how much effort the transition would take. A site like that couldn’t afford to go down for any amount of time. HHVM at the time wasn’t as mature as it is now, so it was almost certain they were going to run into a few problems in implementation. They knew that up front, and they were going to have to push through those problems to make switching to HHVM a success.”

Facebook ended up virtually loaning a developer to help with converting Wikipedia to HHVM. “One of the engineers on the HHVM team spent a great deal of time with Wikimedia, helping them work through their problems. Longer term, they wanted to be sure that HHVM remained a healthy project and isn’t something that stops being supported down the road. That’s a fear that everyone has when they consider a technology that’s relatively young. I think the HHVM team has done an excellent job demonstrating that they genuinely care about the success of all the different companies out there using HHVM. For most companies, compilers and runtimes are not a central focus of their business. They’re trying to do something else. How they execute PHP is very secondary to their mission.”

Handling PHP Extensions

That led to me to question why people would jump to HHVM when there have been so many extensions created for PHP over the years. I knew that Wikipedia had been using many of those extensions and asked how they addressed it. Paroski agreed that the team had spent a great deal of time thinking about that and working on solutions. He said, “HHVM inherited a lot of its extension implementations from HipHop for PHP, which was developed to solve problems at Facebook. HipHop for PHP had its own extension framework, and custom versions of each extension were coded up manually as needed. As HHVM went open-source and people expressed interest, support for extensions was one of the key areas of concern.”

They ended up addressing it in two ways. “One was to make writing extensions for HHVM as painless as possible,” he said. “You are able to write most of your extension implementation in PHP and only the parts that need to be native, you can write in C++. The second route is an experimental compatibility layer. In 2013, we thought, ‘Wouldn’t it be cool if we could just take the source code of an extension written for the Zend engine and get it to talk to HHVM?’ The idea was that you would recompile the original extension from source in a special way that would bind it to HHVM. A few developers on the team ran with that idea. It ended up being used a fair amount to help accelerate Wikimedia’s transition to HHVM.”

Now, HHVM and PHP are both seen as viable options for any website.

The Next Tuning Challenge

Where HHVM goes next depends on the open-source community and the next generation of DevOps expectations. Paroski summed up saying, “In the past, HHVM was only being performance-tuned for one application, so there’s a lot more tuning that needs to happen in both directions.” The companies and organizations that take up the HHVM challenge over the next few years will certainly have a substantial impact how the HHVM community and the technology evolves from here.

Comparing PHP 7 vs. HHVM

PHP is one of the most popular scripting languages used for web development. The latest version of PHP, PHP 7 is a new version of the language that is been optimized for fast performance. However, PHP has a rival in HHVM (Hip Hop Virtual Machine) — a virtual tool that executes PHP code. The competition between these two options is heating up, so let’s take a look at the performance that each can offer.

What is HHVM?

In 2008, Facebook started working on a tool to convert PHP script into C++ so it could be compiled and executed on web servers. The aim was to conserve server resources, an important goal, as Facebook’s user base was growing rapidly. In this sense, the project was a success; it allowed the server to accommodate between five and six times more traffic than it had managed before.

Fast-forward a couple years to 2010. Facebook’s server needs had grown even more, placing it in a position to require another innovation to allow it to operate more efficiently. In response to this demand, Facebook developed the Hip Hop Virtual Machine (HHVM).

HHVM uses Just-In-Time (JIT) compilation to convert PHP code into a type of bytecode. It then converts this bytecode into machine code and optimizes it so that it runs as quickly as possible.

What is PHP 7?

PHP 7 is the PHP community’s response to HHVM. Early announcements of the launch of PHP 7 claimed that it would offer better than 100 percent performance improvements over the previous version of the language, PHP 5.

You might be wondering why PHP skipped version 6. The answer is that development on PHP 6 began in 2005, but it went on so long and ran into so many problems that PHP 6 had developed a bad reputation long before it was ready for release. As a result, the PHP community decided to skip the name PHP 6 and go straight to PHP 7 for the new working version of the language.

The real question is not how PHP 7 compares to PHP 5, as it is pretty clear that PHP 7 offers speedier performance. Instead, the consideration is how PHP 7 compares to HHVM. Many experts have conducted tests on the two ways of handling PHP code, which have revealed some interesting results.

PHP 7 vs. HHVM: Similarities and Differences

Before answering the “which is better” question, let’s take a look at the key differences between PHP 7 and HHVM, as well as the ways in which they are similar.

Code Interpretation

The fundamental difference between PHP 7 and HHVM is the way in which each one interprets PHP code. PHP 7 uses the standard PHP interpreter, free software that is available for anyone to use, to directly interpret and execute PHP code on the server. This generates HTML code, which is then sent to the client. The client then displays the desired content to the web user.

In contrast, the Hip Hop Virtual Machine first converts PHP code into Hip Hop bytecode. This code is then translated into machine code and executed. Some optimization takes place during this translation, ironing out inefficiencies in PHP code with the aim of delivering faster performance.

Writing Code

Both the PHP interpreter and HHVM take PHP code and execute it. Therefore, the process of writing the code is pretty much the same in each case. However, if you want to use HHVM, you need to install it on your server and then call it using the hhvm command on the command line.

Benchmark Testing

HHVM has offered much faster performance than previous versions of PHP. However, recent benchmark tests suggest that PHP 7 is slightly faster than HHVM, at least in some situations. Let’s take a look at the results of some benchmark testing conducted by Kinsta.

  • WordPress: Running on WordPress 4.1.1, PHP 7 allows more than twice as many requests to be executed per second as PHP 5.6. However, it still doesn’t process quite as many as HHVM 3.6.1, which executed 624 requests per second in the test compared to just 604 executed each second by PHP 7.

  • Drupal: PHP 7 offers a distinct advantage over HHVM for Drupal users. PHP 7 can handle 37 percent more server requests per second compared to HHVM on Drupal 8.

Which Companies Use HHVM?

In addition to Facebook, which developed HHVM, many other businesses have adopted this solution to running PHP applications on their own servers. These include Wikimedia and the e-commerce site Etsy.

  • Wikimedia: Wikimedia hosts a huge range of educational content, including the famous Wikipedia online encyclopedia. Attracting nearly half a billion Internet users each month, Wikimedia needs to optimize server performance to cope with its high level of demand. HHVM poses a significant advantage over PHP in that it can load multiple SPU cores simultaneously whereas PHP is a single-threaded language that can’t be parallelized. According to Wikimedia, deploying HHVM shrank CPU load from 50 to just 10 percent, halved the mean time taken to respond to users submitting edits and reduced the average page load time from 1.3 seconds to just 0.9 seconds.

  • Etsy: With 54 million users, Etsy’s servers also face significant demands. Etsy engineers compared HHVM to PHP 5.4 and found that HHVM could cope with up to 280 server requests per second whereas the response time of PHP 5.4 started to dramatically increase once the number of requests grew beyond 190 per second.

What Does the Future Hold for PHP 7 and HHVM?

PHP 7 is due for stable release in November 2015. Therefore, companies are not yet using the new language, but promising benchmark test results of the performance of the beta version of PHP 7 could tempt more companies to adopt the new version of the language.

The future looks bright for PHP 7, but what about HHVM? It is likely that it is far from dead. Many businesses are already using HHVM to increase performance on their sites. The transition between PHP and HHVM is not instantaneous. It took Etsy more than six months to complete the transition. With the speed benefits of PHP 7 compared to HHVM being only very slight, it is unlikely that businesses will rush to switch back to PHP.

Facebook is continuing to develop HHVM. It has recently announced support for Mac OS X, making the technology accessible for developers who prefer to work in the Apple development environment. HHVM developers are convinced that HHVM is still faster than PHP 7 in many situations, including with WordPress.

Why Does the HHVM vs. PHP 7 Competition Matter to PHP Shop Owners?

As an online store owner, you need to make your decision on whether to use PHP 7 or HHVM based on the platform that hosts your shop. For example, if your site is built using WordPress, take a look at benchmark tests for HHVM and PHP 7 to find out how the latest release of each one performs. You want to choose the solution that can offer the biggest reduction in page load times, server response times and CPU usage.

Reasons to Choose HHVM

  • HHVM uses dynamic translation to deliver faster performance in many situations, including on WordPress.

  • HHVM uses less memory to process each request in cases where it faces a very large number of requests.

  • HHVM developers are steadily increasing the number of PHP code bases that the engine can run. It can already run the latest version of WordPress, along with many other common PHP frameworks and applications.

  • HHVM is open source. Even though HHVM has been developed by Facebook, it is open source, which means the source code is available to anyone who wants to use or alter it.

Reasons to Choose PHP 7

  • PHP 7 performs faster than HHVM in some situations, including when running on Drupal 8.

  • Using PHP 7 doesn’t require you to install or setup HHVM.

  • Code written in PHP 5 should work as expected after a transition to PHP 7, although some features of PHP 4 code are no longer supported in the new release. In practice, this means that any code created in the last decade is probably ready for the transition to PHP 7.

  • PHP 7 is developed by the PHP community, a group with a long-standing reputation for creating stable and reliable PHP releases.

HHVM vs. PHP 7: Make Your Choice

Don’t agonize for too long over the decision. Kinsta recommends that online businesses choose quickly between PHP 7 and HHVM. The sooner you make your decision, the sooner you can begin to implement the solution, allowing you to optimize your website performance. A poorly performing website can cause your reputation to suffer, which can be difficult to reverse.

Both HHVM and PHP 7 offer significant benefits compared to older versions of PHP. Make your choice and start the process of switching your site to the new system as soon as possible.