Social Media is all about moving fast, as the volume is very high and user experience is of utmost importance. There is more complexity to websites like Facebook, where every page has some customisation for specific user/users.
Facebook had large code developed in PHP and to make it execute faster was the intention behind HipHop project. Facebook keeps on moving to other technologies and faster ways to improve the performance of their social engine, but HipHop is still there, and new development is going on with this open source platform. If you are a PHP developer or a firm in web development, you can quickly find freelancers with HipHop knowledge to improve the performance of your websites.
What does HipHop do?
HipHop for PHP (HPHPc) is a source to source Compiler. It was created to transfer PHP code into C++, then compile into binary. The open source code is under HP licence, which is in C, C++, and PHP.
It was in 2010 when Facebook started working on this project and realised that it could substantially increase the speed of any PHP applications. The figures you can find on the internet claim that the increase in speed is up to six times over the Zend PHP.
HipHop Virtual Machine (HHVM) is an open source machine designed to execute programs in Hack and PHP. It uses a Just-In-Time compilation process (JIT) to achieve the desired performance. It supports PHP5, PHP7 major features, and Hack.
The JIT Compiler
HHVM first compiles Hack and PHP code into an intermediate bytecode. The bytecode is then converted into x64 machine code at runtime by JIT. The process is an excellent way to optimise the code and to improve the performance of your code.
Technical experts at Facebook mentioned these programs as source code transformer rather than the compiler. The main objective is to transform the PHP code to C++ and then compile it. It includes a transformer, improved PHP runtime and re-write of many PHP extensions.
How HipHop Works
PHP and C++ have many differences, and it was a challenge to convert the code from one scripting language to a structured compiled language. Having static binding for functions and variables was a major step towards PHP code performance improvement. The type interface also helped the technical team at Facebook to pick up specific type for variables saving on memory usage.
The HipHop transformation includes three main steps:
- Analysis of existing code to find out declarations and dependencies
- Choosing specific type between C++ variable types like scalars, array, string, classes, etc.
- Actual code generation part of PHP statements to C++ statements
An experimental interpreter named as HPHPi was developed for the purpose of development of this project. The interpreter helped to debug the HipHop programs as there was no need to compile PHP programs before passing through HipHop compiler.
The project progressed and allowed the best of both worlds. The PHP freelance web developers could use their PHP skills and the compiler optimised the speed. The large code developed under this project is available on GitHub repository. HHVM bytecode interpreter is approximately 1.6X faster than HPHPi.
PHP- HHVM together
The PHP language and HHVM HipHop Virtual Machine are now able to run side-by-side on the same server. It increases the speed and stability for websites in production environments. This project demonstrated a great output. Some websites and applications with dynamic content built on HHVM could see an average response time increase of whooping 560%!
The WP Engine, a SaaS content management system for websites and applications built on WordPress, launched Mercury. It is developed by WP Engine Labs and built on HHVM. Mercury is 5.6X faster than the company’s existing platform with high load.
Advantage of HHVM
The improved speed and performance by HHVM in production environments is an obvious advantage for the PHP developers. The Facebook engineers and the WP Engine Labs team did an excellent job in the HHVM project. PHP is the backbone of Facebook as well as more than 20% of the internet sites and improving the PHP code performance is improving the performance of 20% websites if they adapt this process. With the WP Engine Mercury platform, PHP kicks in instantly to handle the high load when HHVM requires rebooting or resetting. The fall back PHP stack is WP Engine WordPress stack.
WordPress backend id PHP, and by allowing HHVM to run on WordPress speeding it up by 560%, this breakthrough will come as a huge boost to not just the WordPress community, but developers everywhere. The WP Engine Labs are the first one to enable HHVM and PHP to run side-by-side on the same server in a geo-redundant environment. With the launch of Mercury, large enterprises and businesses can rely on WordPress to host their dynamic content in an even faster environment.
HHVM 3.18 LTS was released last month (Feb’17). The release version is the last one to support GCC 4.8 and Ubuntu 14.04 Trusty. Packages are avaaailable, and you can download and start using the same. Installation instructions are available on the HHVM website.
Key changes include Options to enable automatic garbage collection, Support for Ubuntu 16.10. Performance improvements as some Bug fixes as usual in any new release.
Profile Guided Optimisation (PGO)
The latest developments in HHVM are related to PGO. They are a way of optimization performed by compilers to a program based on profile data collected while observing the program’s execution. It is a complex process and gathering profile data and recompiling code is not easy. The JIT compilers can remove the drawback of static compilers and integrate seamlessly to convert the code.
There are many PHP developers in India and other countries also, and they have enhanced their skills for a long time. Having a compiler available to convert their best PHP code into bytecode with the help of a converter is a boon for developers taking freelance work also. Whether Facebook continues to use the same engine is not important but getting six times improvement in performance for your code is what is important for the development community.