For those of you who are unfamiliar, PHP is a dynamic programming language designed to run web application, PHP is developed by Zend, an Israeli based company and is
considered to be one of the most popular languages today.
The need to accelerate PHP’s runtime comes from the way dynamic languages like PHP, Python and Ruby are translated into ‘machine code‘ compared to compiled languages like C++ and Java.
In a compiled language, code is translated into machine code during the compilation process and it can then be executed as machine code endless times.
Dynamic languages are translated into machine code every time the code is executed which makes the execution process a bit slower compared to a compiled language (the machine has to translate every bit of code every time a user runs it)
but it speeds up the development process because the compilation process can be skipped.
We can safely say the dynamic language give faster development time than a compiled language while having a slower run time, and vice-versa.
Naturally dynamic languages are used to develop web application since they have low development cost which is generally more important than fast runtime.
In high-volume web sites it is common to use cache engines or partial code compilation to “spare” some of the webserver’s load, Facebook however has reached a point where using those mechanism isn’t enough.
Due to high traffic volumes and that the most viewed pages on Facebook contain dynamic data and are thus non-cacheable.
A valid solution is to translate partials from the code into a compiled language
like C++ thus creating a faster run time and a more efficient application, this solution presents a problem since using a second language requires more developers and more maintenance.
Instead the Facebook team has decided to improve this idea further, instead of compiling code partials they have written an algorithm to translate PHP code into C++ code and compile it, enter Hip-Hop.
Due to the similarity between PHP and C++ and the fact that most code is usually a block of control statements(loops, conditions and functions) it is relatively easy to translate the PHP syntax into the C++ one.
Thus gaining a huge performance boost while loosing some of PHP’s dynamic nature ,the use of ‘eval’ for example, which does not hold a static replacement in C++ static nature.
Though it sounds complicated the process itself is quite simple, write your application in PHP and use Hip-Hop to translate it into C++, run it seamlessly and gain better performance.
Facebook has announced that the Hip-Hop source code will be released in the near future.
More in Facebook’s Development Blog