DEV Community

Cover image for PHP 8.0 is here! How to turn on JIT compiling
James Sansbury for Tugboat

Posted on • Updated on

PHP 8.0 is here! How to turn on JIT compiling

As of Nov 26, 2020 PHP 8.0 will be “generally available.” (I learned today that that’s what the GA means on that page.)

Folks have been benchmarking and found that an upgrade to PHP 8.0 will likely mean a performance boost for your website. Faster loading pages is important both for SEO purposes, and of course for the actual users of your sites.

PHP 8.0 now ships with a JIT compiler that can increase that performance boost even further. JIT stands for “Just in time,” and means that PHP can compile its code directly into machine code (code that the CPU understands) without needing the help of an interpretation layer. For CPU-intensive tasks, having a JIT compiler in PHP boasts significant performance gains.

PHP developers everywhere are missing out on this:

Compiling comic from XKCD: https://xkcd.com/303/

If you’re interested in learning more about the new stuff in PHP 8.0, here’s a few articles to check out:

  1. https://torquemag.io/2020/11/php-8/
  2. https://stitcher.io/blog/php-jit
  3. https://thephp.website/en/issue/php-8-jit/

🔦 How to turn on JIT compiling

If you’ve been using PHP 7.x, you’re probably familiar with the OPcache extension. (And if you’re not, go make sure it’s turned on now. It’s a huge performance boost.)

For PHP 8.0, the JIT compiling is enabled via this same OPcache extension. So step one in getting JIT compiling turned on for your project is making sure the OPcache extension is installed and enabled.

The process for installing the OPcache extension will depend a lot on the platform you’re running PHP on. Check the docs for your flavor of Linux. If you’re using the official PHP Docker images, installing it is pretty easy:

$ docker-php-ext-install opcache
Enter fullscreen mode Exit fullscreen mode

To check if you’ve got it installed, you can run php -i on the command-line to print out phpinfo().

$ php -i | grep 'opcache\.enable '
opcache.enable => On => On
Enter fullscreen mode Exit fullscreen mode

🖊️ Tweak the Opcache settings to enable JIT compiling

Now that you have the Opcache extension installed, that doesn’t mean JIT is turned on. We’ve got to override a few default ini settings to actually enable JIT compiling. To do this, you can see if you can find the existing opcache.ini file. Here’s how I usually do that (again, with phpinfo()).

php -i | grep opcache\.ini
/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
Enter fullscreen mode Exit fullscreen mode

As you can see, this is a Docker image I’m using, so any changes I make here are liable to get wiped out, but I’m okay with that for now. I’m just tinkering at this point. I’m going to edit that file and add the following settings:

# Turn on the OPcache for command-line PHP, like drush or 
# wp-cli, etc.
opcache.enable_cli=1

# The amount of shared memory to reserve for compiled JIT
# code. A zero value disables the JIT.
opcache.jit_buffer_size=50M

# JIT control options. Either accepts a string or a 4 digit 
# int for advanced controls. See 
# https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.jit
opcache.jit=tracing
Enter fullscreen mode Exit fullscreen mode

Note: if you wanted a more permanent solution for Docker, create your own ini file that gets copied or mounted into the $PHP_INI_DIR/conf.d/ directory in your container.

For more details on these settings, check out the OPcache Runtime Configuration or the PHP RFC: JIT. You read that right: three three-letter acronyms. 🤦‍♂️

OMGWTFBBQ

You can double check that your settings worked by using php -i again:

$ php -i | grep -E '^opcache\.(enable_cli|jit|jit_buffer_size) '
opcache.enable_cli => On => On
opcache.jit => tracing => tracing
opcache.jit_buffer_size => 50M => 50M
Enter fullscreen mode Exit fullscreen mode

That’s it! You’re now taking advantage of JIT compiling in PHP 8.0. (And if you’re using the Apache PHP module, don’t forget to apache2ctl graceful). Go forth and test. 🧪

Top comments (1)

Collapse
 
vinceamstoutz profile image
Vincent Amstoutz

Thanks for your help !

If I am not mistaken your link is broken https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.jit
opcache.jit=tracing
and sould be just : https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.jit