Now that Facebook is up to 800 million users they are working hard to find new ways to serve all of their users in a way that keeps their quality of service at a high level. To do this, Facebook has created a Just-in-time or JIT, compiler for PHP to help handle its PHP-based web pages called HipHop Virtual Machine (HHVM).
For those of you unfamiliar with the idea of Just-in-time compilation, here is a little primer: JIT compilation is a method of compilation that allows for the improvement of runtime performance because the code is not compiled until it is run on a the machine. This interpreted code is translated during execution and can be optimized for the environment’s hardware or other specifications. This allows the code to change to fit a specific CPU or operating system. Since this translation happens on the fly at execution time, it only needs to happen once, cutting down on the amount of time the computer spends handling the code. Other benefits of JIT compiling include the ability to enforce a certain amount of security and the handling of late-bound objects and data types. JIT compilation can be found in many popular programming languages such as Java and C#.
What Facebook has done with HipHop Virtual Machine is cut down on the amount of compilation and translation that needs to happen between PHP and native machine code. According to Facebook, this process is 60 percent faster than the current PHP interpreter and uses 90 percent less memory. The reason for this is clear if you understand how PHP works. Since PHP was originally designed to be a dynamic web language which could be used to transfer data in and out of pages, the functionality is not there to run as a native application. However, PHP is not as efficient as low-level languages such as C++, which is compiled into machine language so there has to be some steps in-between to get there. PHP’s instructions need to be translated for the CPU into machine language. The traditional PHP compiler has to make these translations in steps from PHP to Abstract Syntax Trees or AST and then these AST structures can then be translated into C++, which can then translate to machine code. This looks like this:
PHP => AST => C++ => Machine Code
The benefit of HHVM is that it is essentially a virtual machine that accepts PHP and returns a new format called HipHop bytecode (HHBC). This HHBC can be translated directly, bypassing the other translation steps that used to be necessary in the process. This is the outstanding solution that makes those earlier optimization figures possible. Now the process looks like this:
PHP => HHBC
Since the HipHop Byte Code can be executed in this virtual machine like it was machine code, no further translation is needed. This system uses an interpreter and a dynamic HHBC to machine code translator to support this transition. The main goal of this project was to keep the process as simple as possible.
While Facebook hasn’t put HHVM into its production environment yet, based on what is coming out of testing, it shouldn’t be too long before we see this new method in use on Facebook’s main pages. they currently consider the project to be 90% complete. HHVM is available on GitHub for those of you that would like to begin exploring this new virtual machine system and the bytecode specification for HHBC is available for download as well. While HHVM was designed for Facebook’s own internal use, they are providing this release in hopes of this technology being able to improve other organizations and their use of PHP. There is also a lot of work that needs to be done on HHVM and they are hoping the PHP community will enjoy working on and submitting ideas to the project. This could be your opportunity to work on something that may change the way we use the PHP language for a long time. Is PHP is not in your skill bag yet? Put it there by attending one of our PHP training courses!
|GitHub site for HipHop:|
|Bytecode specification for HHBC:|
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|May 29-30||Web Development with PHP/MySQL|
|Jul 23-24||Web Development with PHP/MySQL|
|- Classroom - Online|