JIT - The main novelty of PHP 8. How to enable it in the new version of the language?

Ronaldo B.July 27, 20224 min read

In this article

PHP 8 brought many new features, but one of the most talked about was JIT. We discussed what this acronym means and how it impacts the performance of a programming language in our article What is the difference between compiled, interpreted, and JIT (Just in Time) languages?.

However, since JIT is a new feature in PHP version 8, in this article, we will show how to enable this feature in PHP and also give our opinion on how JIT may impact the use of this language.

We showed on our Blog how to install PHP 8 on both Windows and Linux. You can access this content at the links below:

PHP 8 - Installation and Configuration on Ubuntu 20.04 and Windows

PHP 8 on Windows 10 with Nginx server

Enabling OPcache in PHP

OPcache is an extension that allows the storage of precompiled PHP code in cache, aiming for better language performance. JIT was implemented in PHP through this extension. Therefore, the first thing we need to do is enable it.

After installing PHP 8, we will access the php.ini file and enable the zend_extension configuration line, as shown below:

zend_extension=opcache

Note that to enable a configuration in the php.ini file, simply remove the semicolon (;) at the beginning of the line.

In addition to this configuration, we will enable the opcache.enable and opcache.enable_cli directives, as shown below:

opcache.enable=1
opcache.enable_cli=1

When running the phpinfo() function in a file, we should see the following section of information about PHP:

OPcache section in PHP information

Now OPcache is enabled. The next step is to enable JIT through this extension.

Enabling JIT in PHP 8

We need to insert three directives to enable JIT. You may not find them in the php.ini file. If that’s the case, simply add these directives to the file instead of editing them.

The first is opcache.jit_buffer_size. It defines the amount of memory buffer that will be reserved for native code generation. By default, this directive is 0, which disables JIT. Thus, if we want to enable it, we just need to specify an amount of memory for that purpose. We will do this in php.ini using the following code:

opcache.jit_buffer_size=100M

The second configuration is opcache.jit. It defines a set of configurations for JIT in PHP. This directive consists of four digits. Each one represents a specific configuration. The recommended set is the number 1235, but we could use a different number if we wanted. This directive will look like this:

opcache.jit=1235

Finally, but no less important, we have the directive opcache.jit_debug. It controls how the code will be debugged. This configuration will be as follows:

opcache.jit_debug=1

Now, when we restart the server and access the PHP information, we will see the following line in the OPcache block:

JIT enabled in PHP

Note that now JIT is enabled in our PHP ?.

How will JIT help the PHP language?

The PHP website provides us with a very interesting graph that shows some performance improvements when running some code tests with JIT:

Speed comparison chart of PHP 8

We can note that the speed difference is truly impressive. However, it has been found that the power of JIT is even more noticeable when we need to execute code that requires a high CPU consumption or code that demands high data processing.

Perhaps we are not so familiar with this concept in PHP since this is a language very much focused on the Web. With that in mind, the introduction of JIT can bring a new horizon of possibilities for PHP, now outside of the Web. Think, for example, of Artificial Intelligence, Machine Learning, or even Game Development. All of these are environments that require a lot of data processing and where PHP has not ventured so far.

Conclusion

The inclusion of JIT in PHP can be a turning point for the language. This means we may be witnessing a historic moment, the start of a possible expansion of PHP, which could increase its area of operation, moving out of the unique context of the Web and beginning to "take new flights".

All of these are the possibilities that JIT can bring to PHP. But only time will tell which of these assumptions will become reality. What remains for us now is to wait and continue studying ?.

Until the next article :).

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.