Knowledge

Enable JIT in PHP 8.x OPcache

#PHP

Using OPcache can greatly improve [PHP performance](/php-performance). By enabling Just In Time (JIT) compiling in OPcache you can improve it even more.

Published by Mark van Eijk on November 21, 2024
Updated on November 21, 2024 · 2 minute read

  1. What is JIT (Just In Time) compiling?
  2. First: OPcache should be installed
  3. How to enable JIT compiling
  4. Optimization level
  5. Enable in the CLI

What is JIT (Just In Time) compiling?

JIT is a compiler optimization feature introduced in PHP 8 and can enable faster execution times for CPU intensive tasks. It enhances performance by translating frequently executed PHP code into machine code at runtime, allowing it to be executed directly by the CPU instead of being interpreted line-by-line. This results in significant speed improvements!

This is one of those extensions that's a bit confusing to setup, but it's not very dificult if you know how to.

First: OPcache should be installed

To begin with, we expect to have OPcache installed on your server. This is a PHP extension and on a Ubuntu server you can install it using (change to your current PHP version accordingly):

sudo apt install php8.4-opcache

How to enable JIT compiling

By default JIT is disabled in OPcache, so you need to enable it manually. Also note that the enabling of OPcache is separate for the PHP (FPM) process and using PHP on the CLI.

JIT can be enabled by setting opcache.jit_buffer_size to a value and in general 128M is a pretty decent value that's enough for most PHP applications. Add this line:

opcache.jit_buffer_size=128M

To your php.ini config file or in /etc/php/8.4/mods-available/opcache.ini:

opcache.enabled=1
opcache.jit_buffer_size=128M

Now JIT is enabled!

Optimization level

Next you need to decide a proper opcache.jit value that determines what optimizations the JIT compiler will perform. This can be precisely set as a bitmasker using a 4-digit integer "CRTO". More on this in the PHP docs.

But for typical usage, it's easier to use the following presets that cover most use cases. The following options:

# Completely disabled, cannot be enabled at runtime.
opcache.jit=disable
	
# Disabled, but can be enabled at runtime.
opcache.jit=off
	
# Use tracing JIT. Enabled by default and recommended for most users.
opcache.jit=on # or opcache.jit=tracing

# Use function JIT.
opcache.jit=function

For general usage you can choose on which is almost the highest setting (1254) to enable almost all optimization levels. This makes the config now:

opcache.enable=1
opcache.jit=on
opcache.jit_buffer_size=128M

Enable in the CLI

If you also want to optimize CLI usage, you can enable OPcache (and JIT) by adding:

opcache.enable_cli=1

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

How to run PHP files

Using OPcache can greatly improve [PHP performance](/php-performance). By enabling Just In Time (JIT) compiling in OPcache you can improve it even more.

Read more →