PHP is an interpreted programming language, which has long struggled to improve its performance, which was particularly bleak in comparison to other competing programming languages used in web development.
Among what is new in PHP 8, the JIT compiler deserves special attention, as it was officially included in the latest version. Will this step be an actual breakthrough, or nothing but a blip, an interesting addition to the language? Let’s see what real benefits it can bring and whether it will bring any real-world improvement of the performance of your application.
Some readers might not remember that but starting with PHP 5.5 OPcache ran by Zend VM became an integral part of the language. It was a breakthrough in terms of improving application performance, and thus in the context of PHP web development services delivery. The later PHP 7 release brought even better performance. Even at that stage, there were proposals to implement and add a JIT compiler to the language; however, the community made every attempt to maximise efficiency using the available mechanisms.
What is JIT?
In simple words, JIT (just-in-time compilation) is a technique that enables compiling a program into machine code immediately before it is executed. In reality, this approach results in significantly faster code execution compared to a traditional interpreter. Sometimes JIT is referred to as a golden mean between flexibility in developing the application code and its runtime performance.
Working principle
As I mentioned earlier, to date better PHP performance was based on OPCache using the so-called OPCode, which is a pre-compiled snippet of code in the form of commands given to the processor for execution, created after the script is run for the first time. The cached code can be run almost immediately by a virtual machine; however, it is still not native machine code.
JIT offers actual machine code that has been implemented in such a way as to work together with OPCache. When the script is first launched, if it’s been already cached in OPCache, it is immediately returned and compiled (if it was not already). However, in the case the script has not yet been cached, it first goes through the entire OPCode generation process, like previously. The whole process is perfectly illustrated in the diagram below.
Source: https://thephp.website
Impact on web application performance
Every good developer should know that the most important factor that affects the application’s performance is the quality of their code. Another equally important factor is the choice of the technologies that make up its entire stack and how they are developed. In the case of PHP, OPCache was a revolution; however, JIT — despite the huge media hype — does not seem to be the same milestone, especially for web applications. Why?
JIT was introduced with the aim of compiling batches of code that are not subject to significant fluctuations. It detects snippets of code that are executed more than once and compiles them accordingly. As you may probably already guess, executing code while handling a single query/request of a web application depends on too many variables and in reality, it turns out that these identical snippets of code are few and far in between. What’s more, in some cases there may be so few of them that instead of really speeding up the application, JIT will actually slow it down due to the additional burden of compiling the code.
In one of the publicly available tests of the project based on the Laravel framework and described in the medium.com article we can find out that running it in PHP 8 with JIT gives only a slight performance boost:
PHP 7.3: 131.37 req/s PHP 8.0 + JIT: 133.57 req/s
One can clearly see that in web applications the added performance will be barely noticeable.
What’s its use then?
The introduction of a JIT compiler in PHP is actually a step towards opening the language to new possibilities. JIT significantly improves code execution performance in CPU-intensive applications. This is about using PHP for completely new purposes, such as machine learning, complex mathematical calculations, or 2D/3D image processing/modelling.
A proof of concept video created by one of the leading PHP developers (Zeev Suraski) has been circulating on the web for quite a while now, showing performance improvements thanks to JIT — in his case, it was about generating fractals in real-time.
According to various benchmarks available on the web (for example https://www.phoronix.com/scan.php?page=article&item=php8-jit-june&num=2), depending on the application, JIT offers an actual, significant performance boost, ranging from single-digit improvements to nearly a hundred per cent.
Pros and cons
The implementation of the JIT compiler is a step towards opening the language to new possibilities and making, for example, PHP perfect for startups. The ability to directly compile PHP code will lower the dependence on C, which will make it easier and actually possible to develop without any particular performance implications. Undeniably, JIT improves application performance to a greater or lesser extent.
On the other hand, in its current shape, JIT is unable to significantly improve the performance of web applications, and in some specific scenarios, it might even result in worse performance than before. The way it works can also affect the development process, making it difficult to debug code, as there are some known issues with the xDebug tool. JIT will also require additional configuration know-how from developers.
Conclusions
At Droptica, where we provide Drupal development services, Drupal support and our team includes many PHP programmers, we know that although JIT in the world is not entirely new (Facebook and their HHVM were the forerunners), it will soon become native part of the language with the release of PHP 8. JIT is going to improve code execution performance and expand the possibilities of using the language itself. I think that in the coming years we should keep an eye on its development, which can bring even better results, especially in the context of web applications.
Originally published at https://www.droptica.com.