Php Performance Tips

PHP 8.0 brings support for Just-In-Time Compilation (JIT). Pure interpreted programming languages has no compilation step, and directly executes the code in a virtual machine. Most of the interpreted languages including PHP, in fact, has a light-weight compilation step to improve its performance.Just-In-Time compilation is a hybrid model of interpreter and Ahead-of-Time compilation, that some or all of the code is compiled, often at run-time, without requiring the developer to manually compile it.1

First of all, the JIT will only work if opcache is enabled, this is the default for most PHP installations, but you should make sure that opcache.enable is set to 1 in your php.ini file. Enabling the JIT itself is done by specifying opcache.jit_buffer_size in php.ini.

Testing JIT

The difference between opcache.enable and opcache.enable_cli is that the first one should be used if you're running, for example, the built-in PHP server. If you're actually running a CLI script, you'll need opcache.enable_cli.

Before continuing, let's ensure the JIT actually works, create a PHP script that's accessible via the browser or the CLI (depending on where you're testing the JIT), and look at the output of opcache_get_status():2

var_dump(opcache_get_status()['jit']);

/*
array:7 [
  "enabled" => true
  "on" => true
  "kind" => 5
  "opt_level" => 4
  "opt_flags" => 6
  "buffer_size" => 4080
  "buffer_free" => 0
]
*/

If enabled and on are true, you're good to go!

Next, there's several ways to configure the JIT (and this is where we'll get into the configuration mess). You can configure when the JIT should run, how much it should try to optimise, etc. All of these options are configured using a single (!) config entry: opcache.jit.

Now, what does that number mean? The RFC lists the meaning of each of them. Mind you: this is not a bit mask, each number simply represents another configuration option. The RFC lists the following options:2

O — Optimization level

Level Description
0 don't JIT
1 minimal JIT (call standard VM handlers)
2 selective VM handler inlining
3 optimized JIT based on static type inference of individual function
4 optimized JIT based on static type inference and call tree
5 optimized JIT based on static type inference and inner procedure analyses

Configuring JIT

In PHP 8.0, JIT is enabled by default, but turned off. Enabling JIT is easy, and only requires minimal INI configuration similar to this:

opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=256M

Important

opcache.enable=1 directive does not enable Opcache for CLI, and requires opcache.enable_cli=1. It is possible to turn off Opcache (and this JIT) for other SAPIs (such as FPM), and enable Opcache only for CLI with opcache.enable_cli directive.

Set Buffer Size

The effective toggle for JIT is opcache.jit_buffer_size. It accepts how much memory JIT is allowed to use for its buffer. The default value 0, which effectively disables JIT. To enable and set a buffer size, set a positive value in bytes, or with standard PHP data size suffixes (M, G, etc.)

opcache.jit_buffer_size=256M

Full Configuration

Finally your opcache configuration should contain the following:

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=256M
opcache.jit=1255
opcache.revalidate_freq=0
opcache.validate_timestamps=1
opcache.max_accelerated_files=10000
opcache.memory_consumption=256
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=16

Don't forget to restart your web server after making the php.ini changes.

service apache2 restart

Dev Tools F12 Key

You can easily check your page speed with dev tools by pressing the f12 key.

See The Results with F12 Key

Google PageSpeed

You can measure your multidevice page performance with Google Page Score. Visit to Google pagespeed.dev url.