Blog

Performance improvements with PHP 8 JIT compiler

published

PHP 8.0.0 introduced a new “just in time” compiler (JIT) that can improve the performance of our PHP applications. But the JIT is not active by default. So how can the JIT compiler be activated and what is the performance gain?

These ini-file settings will activate the opcache and the JIT compiler with some more reasonable default settings:

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

We should now get some lines with configuration settings after running php -i | grep jit in the terminal:

auto_globals_jit => On => On
pcre.jit => 1 => 1
opcache.jit => tracing => tracing
opcache.jit_bisect_limit => 0 => 0
opcache.jit_blacklist_root_trace => 16 => 16
opcache.jit_blacklist_side_trace => 8 => 8
opcache.jit_buffer_size => 100M => 100M
opcache.jit_debug => 0 => 0
opcache.jit_hot_func => 127 => 127
opcache.jit_hot_loop => 64 => 64
opcache.jit_hot_return => 8 => 8
opcache.jit_hot_side_exit => 8 => 8
opcache.jit_max_exit_counters => 8192 => 8192
opcache.jit_max_loop_unrolls => 8 => 8
opcache.jit_max_polymorphic_calls => 2 => 2
opcache.jit_max_recursive_calls => 2 => 2
opcache.jit_max_recursive_returns => 2 => 2
opcache.jit_max_root_traces => 1024 => 1024
opcache.jit_max_side_traces => 128 => 128
opcache.jit_prof_threshold => 0.005 => 0.005

Here I want to test the performance gain with a small script, that will calculate the prime numbers up to 100 mio using an implementation of the Sieve of Eratosthenes. It will not output each prime number but rather count all the prime numbers up to the given upper limit.

<?php
$tStart = microtime(true);

$limit = 100_000_000;

$nums=array_fill(0, $limit+1, 1);
$sqrtlimit = (int)sqrt($limit);
for ($n = 2; $n <= $sqrtlimit; $n++)
  if (1 === $nums[$n])
    for ($i = $n * $n; $i <= $limit; $i += $n)
      $nums[$i] = 0;
$duration = microtime(true) - $tStart;

echo array_sum($nums)-2 . "\n"; // 0 and 1 are not primes
echo round($duration, 3) . " seconds\n";

I did a couple of test runs, with PHP 7.4 / 8.0, without opcache and with opcache, without and with JIT compiler and also with strict_mode:

PHP 7.4 and 8.0 performance

Running with PHP 8.0 and active JIT compiler

>php8.0 prime-sieve.php 
5761455
10.614 seconds

Seems like it doesn’t matter if we are running PHP 7.4 or PHP 8.0 with or without opcache. But activating the JIT compiler in PHP 8.0 gives us a 90% performance improvement! This is quite a remarkable boost, though in real life code the speed improvement will probably be much smaller, due to more I/O operations like database requests.


Tags: #php, #performance, #jit, #benchmark