Author: Thomas Meier

What is PHP7 Vs. HHVM

Origin Story of the Rivalry Between Facebook and PHP

Back in 2010 Facebook developers announced that they had been working on a solution to the rising costs of running Facebook’s cloud servers. Due to the ever-growing resource demands being placed on Facebook, they needed to develop a solution that would not require them to make substantial changes to their source code but would still offer optimized performance.

Their solution was “HipHop for PHP” (HPHPc), which translated the PHP codebase of Facebook into heavily optimized C++ and then compiled that code with g++. The project’s success was considerable–Facebook boasted that they were able to reduce CPU usage on their web servers by around 50%, translating to a significant reduction in the overhead cost of running their servers.

Because of this success, Facebook open-sourced the project with the goal of showing the web community how easy it was to enhance website performance without having to make major changes to their codebase. Facebook developers often stated during presentations that PHP’s interpreter needed optimization and that that was one of the primary reasons behind their development of the Hip Hop Platform.

PHP’s development team may have taken this criticism by Facebook to heart when they began development of the next generation of PHP; the result of PHP7’s development (which began in 2014) has produced in significant performance upgrades when compared to PHP 5.X. They started by analyzing PHP’s performance on popular platforms such as WordPress, Drupal, and phpBB. By focusing on enhancing performance on these popular web platforms, the development team was able to make small but numerous changes to the existing source code. After over a year of finding, improving, and testing these changes, they found they were able to considerably reduce the source code of PHP, resulting in a faster and more lightweight programming language.

The Goals and Features of HHVM

After the success of HPHPc, Facebook decided to invest in developing HPHPc more to push for a more substantial increase in performance and a greater reduction of resources necessary to run one of the internet’s most high-traffic websites. Their next step was to create a virtual machine to execute the HPHPc code, a VM that came to be called the HipHop Virtual Machine (HHVM).

HHVM is a virtual machine that executes programs written in PHP or Facebook’s own Hack programming language. It works by taking PHP or Hack code and converting it into untyped bytecode and metadata, using their own Abstract Syntax Tree to convert PHP and Hack into bytecode that they call HipHopByteCode (HHBC).

HHVM then analyzes the HHBC and converts it into a typed Intermediate Representation(IR). HHVM then converts the IR into x64 machine code and executes the program off the x64 machine code.

Summary of HHVM Execution Flow:

PHP source–(parse)–> Abstract Syntax Tree–(emit)–>Bytecode–(analyze)–(intermediate representation)–(codegen)–> x64 machine code.

Why Does HHVM Use C++?

It may be confusing to some as to why you would convert PHP code to C++ code. Facebook argues that C++ gives developers a much-needed balance between performance and maintainability, noting that C++ offers many convenient features, such as:

  • virtual methods
  • multiple inheritances
  • templates
  • macros
  • reinterpretcast vs. dynamiccast
  • plain old data vs. constructors/destructors
  • raw pointers vs. references vs. smart pointers
  • stack allocation vs. malloc vs. new

One important thing to take away from these features is that you have to know what your bytecode will look like at compile time to be able to properly customize HHVM to optimize your code.

The Goals and Features of PHP 7

PHP 7’s development team sought to improve the performance of their source code with the end goal of having the ability for any website to update to PHP7 to see a substantial increase in performance without having to make any changes to the site’s current code.

They boast that, with the source code streamlining, the decreased memory usage, and the inclusion of an Abstract Syntax Tree to boost the performance of the PHP parser, users can see as much as a doubling in performance compared to PHP 5.x. The addition of a secondary file-based cache for OPcache also further augments the OPCache introduced in PHP 5.3.x.

The Major Difference Between HHVM and PHP 7

While HHVM and PHP7 have the same goal of improving the speed and performance of executed PHP code, they both have vastly different ways to accomplish these improvements. One noteworthy difference between the two is the inclusion of the “Just in Time” (JIT) compiler in HHVM. The PHP 7 development team attributed this inclusion as the reason many early benchmarks showed HHVM outperforming PHP 7. So while much of PHP7’s efforts focused on optimizing the source code of PHP, they were also able to include an Abstract Syntax Tree (AST) in the source code for the language. With the inclusion of the AST, PHP7’s development team has set up for the inclusion of a JIT compiler in a future version of PHP 7. A JIT compiler should allow PHP7 to significantly outperform HHVM in any non-Facebook application.

Who is the winner?

Despite the rivalry between PHP’s and Facebook’s development teams, both acknowledge that performance will widely vary from system to system. Because both projects are open source, they encourage community feedback to help them further develop and optimize their code. Developers with a large PHP code base now have the potential ability to increase the performance of their PHP code base with HHVM. For those who mostly handle small websites that don’t quite have the resource requirements of Facebook, the optimization and performance updates that PHP 7 brings will help enhance user experience so websites will be able to run as fast as those with any large code base do.

What is PHP7 – Performance Improvements

PHP7 Elephant created by Walker Cahall

PHP7 Elephant created by Walker Cahall 

Target Audience

This article is geared toward those familiar with PHP, or with scripting languages and how they interact with the hardware upon which they run.


Sometime in November 2015, the PHP Group will be releasing PHP 7. This release promises greater performance and some new tools to further improve responsiveness. This article takes a quick look at how PHP’s developers accomplished these improvements and what they will mean for the future of PHP.


PHP 7 Performance Tweaks

The PHP 7 development team focused on performance optimization using real world web frameworks and software as a guide. PHP inventor Rasmus Lerdorf recently discussed the development team’s efforts at frOSCon 2015. One of the team’s first tasks was to increase the speed of PHP by reducing both the amount of time and instructions needed for requests against WordPress–one of the most common and popular PHP applications online. Some of their accomplishments during this time include:

  • Much more CPU cache friendly
  • New memory allocator similar to jemalloc
  • Faster hashtable iteration API
  • Array duplication optimization
  • Fast ZPP (ZendParseParameters) implementation
  • Faster stack-allocated zvals (instead of heap)
  • Optimized VM calling
  • Global register variables with gcc 4.8+
  • zval size reduced from 24 to 16 bytes
  • Hashtable size reduced from 72 to 56 bytes
  • Hashtable bucket size reduced from 72 to 32 bytes
  • Immutable array optimization (from 43M of memory down to 6M of memory).

Much of this list taken from Lerdorf’s talk in Santa Clara on Speeding up the Web with PHP7.

Lerdorf also presented benchmarks showing significantly increased performance in Drupal 8, WordPress 4.1.1, phpBB 3.1.3, MediaWiki 1.24.1, OpenCart, Wardrobe CMS 1.2.0, Geeklog 2.1.0., and many others. Lerdorf reported performance improvements in some applications was double that of PHP 5.x in his benchmark tests–with the caveat that replicating these results depends on the system on which PHP 7 runs.


Introducing the Secondary File-Based Cache

PHP 7 introduces a major performance improvement by augmenting the OPCache extension (bundled into PHP >=5.5.0) with a secondary, persistent file-based cache. The OPCache extension works by storing the OPCodes generated by the compiling of a PHP script in shared memory. When the script executes a second time, it can load the OPcode file from shared memory into OPCache and then execute the file. This process is substantially faster than recompiling everything. In PHP 7, users will have the option to enable a secondary file-based cache in addition to the shared memory cache. If a user enables the secondary file-based cache, then the system running PHP will have a fallback alternative location on disk for already generated OPCodes; it will only consult this secondary cache if the OPCodes have been flushed from shared memory. If the OPCode file that matches the script is in this new file-based cache, it will load right back into OPCache for the script to finally execute.


How Does The New Secondary Cache Increase Performance?

This feature will be mostly beneficial to performance on high traffic websites. When these servers need to be restarted, their memory cache is cleared, so everything has to be recompiled. With the addition of the secondary OPCache on disk, PHP can grab the OPCode from the new secondary file-based cache instead of having to recompile everything. It’s important to remember that this isn’t as fast as getting the memory cache from the shared memory. To compare the speeds, consider the time it takes to read an uncompiled PHP index file as the base speed. It is 4 times as fast to run from the disk than from the index file, and 10 times faster to run from the shared memory OpCache than it is from the index file. So, while there is still a performance hit upon server restarts, the secondary cache still executes much faster than running from the index file.

Future Updates to PHP 7

Absent from PHP 7 is a JIT (Just in Time) compiler, which could greatly improve PHP 7 performance. The developers have stated that they are working on JIT to be included in a future update to PHP 7. As it stands right now, PHP 7 is written with the intent to eventually support a migration to a JIT compiler.

Another potential update we could see in PHP 7 is support for asynchronous programming. This ability will allow code to perform tasks such as accessing files, networks, timers, and databases through the use of a PHP event-loop.

Thank you for reading, and good luck as you migrate your code to PHP7. Check out our how-to’s on installing PHP7 and try it yourself (CentOS, Debian, Ubuntu). To learn more about some of the other new things to expect from PHP7, check out some of our other articles:



Altantic.Net offers many cloud hosting solutions, managed cloud hosting, one-click application cloud installations and many other hosting solutions, such as our reliable HIPAA-compliant cloud hosting solutions.


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4


London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom