PHP JIT and Web APIs: What I Measured on Real Projects

The PHP 8 JIT showed no measurable gain on the web APIs I tried it on, because most request time went to I/O. The numbers and where it might still help.

· 2 min read · Erik Kubica

When PHP 8.0 introduced the Just-In-Time (JIT) compiler, a lot of benchmark graphs appeared. Blog posts showed Mandelbrot sets rendering three times faster and Fibonacci recursion getting close to native C performance.

Many of us hoped the JIT compiler would cut hosting bills noticeably.

I turned on the JIT compiler across three high-traffic client APIs in 2021. I configured OPcache, allocated 128 MB of dedicated JIT buffer memory, and watched my Datadog latency charts.

The improvement I could see? None.

Why Synthetic Benchmarks Mislead

Why did an optimization that looked great in micro-benchmarks make no measurable difference in a live web service?

This is how you configure the tracing JIT in php.ini:

; /etc/php/8.3/fpm/conf.d/10-opcache.ini
opcache.enable = 1
opcache.enable_cli = 1
opcache.jit = tracing
opcache.jit_buffer_size = 128M

The JIT compiler analyzes frequently executed bytecode loops (hot traces) and compiles them into native x86 machine instructions.

If your code runs nested mathematical loops, such as matrix multiplications or fractal coordinates, the JIT can give large speedups. The CPU keeps its work in native machine registers without going through the Zend VM interpreter loop.

But what does an actual HTTP API do?

// An ordinary web endpoint
public function show(int $id): JsonResponse
{
    // 1. Network read from TCP socket
    // 2. Network query to PostgreSQL (12ms)
    $order = $this->repository->findWithItems($id);

    // 3. Hydrate PHP objects (1.5ms)
    // 4. Serialize to JSON (0.8ms)
    // 5. Network write to Nginx socket
    return new JsonResponse($order);
}

In a typical request on those APIs, lasting 18 milliseconds:

  • 14.5 ms (80%): Waiting on PostgreSQL over a Unix socket or loopback TCP.
  • 2.1 ms (12%): Parsing incoming HTTP headers and waiting on network I/O.
  • 1.4 ms (8%): Executing Zend VM bytecode (object allocation, array hashing, JSON encoding).

Even if the JIT compiler cut that 1.4 ms of PHP bytecode execution down to zero, request latency would drop from 18.0 ms to 16.6 ms. That is about 7% off the wall-clock time, in exchange for an extra 128 MB of RAM for the JIT buffer.

// Benchmarking real JSON API: JIT OFF vs JIT ON
$ wrk -t4 -c50 -d20s http://127.0.0.1/api/v1/orders/42
// JIT Disabled: Avg latency = 18.42ms, Req/Sec = 2712.10
// JIT Enabled:  Avg latency = 18.15ms, Req/Sec = 2748.40

That is about a 1.4% throughput gain, which is within the margin of error for my setup.

A Compiled Comparison

Go does not depend on a runtime picking out hot loops. The compiler generates machine code before the application reaches a server. For comparison, here is the same endpoint rewritten in Go:

$ wrk -t4 -c50 -d20s http://127.0.0.1:8080/v1/orders/42
Running 20s test @ http://127.0.0.1:8080/v1/orders/42
  4 threads and 50 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.82ms    0.64ms  12.14ms   88.10%
    Req/Sec     6.84k   412.10     7.42k    74.50%

Same database, same queries, same JSON structure. Go finished in 1.82 ms instead of 18.15 ms on my machine, and my guesses for why are:

  1. Less interpreter overhead across the code.
  2. Contiguous memory structs instead of nested hash tables (zval).
  3. Goroutines multiplexing database connections without process context switches.

I would not read too much into one comparison, and a rewrite has its own costs. The more useful takeaway for me is that the JIT does not speed up a database query or reduce network latency.

PHP's JIT is a good piece of engineering and it can help with numerical work or long-running CPU-bound scripts. For the web APIs I work on, it was not what moved the needle on latency or server bills, and I would look at queries, caching and I/O first.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.