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.
Facebook Creates “HipHop for PHP”
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.
The Development of PHP7
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.
.
.