All posts
Published in PHP

JIT Compiler in PHP

Profile image of Atakan Demircioğlu
By Atakan Demircioğlu
Fullstack Developer

JIT Compiler in PHP

Here are my notes about the JIT compiler in PHP.

JIT Compiler in PHP image 1

Before PHP JIT Compilation

  • each script processed in a request to a PHP web application is parsed, and then compiled into “byte codes” (op codes)
  • “op codes”, are pseudo-instructions that a virtual machine can process.
  • Zend Engine”; this is a virtual machine, similar to LLVM or the JVM.
  • A virtual machine’s job is to take those byte codes, compile them to machine code, and then execute them.
  • When the OPcache is enabled, PHP takes the byte codes it compiles to and stores them in the OPcache before passing them to the VM (Introduced in PHP 5.5). On subsequent requests, it checks to see if it has an entry for that script in the OPcache; if so, it passes those directly to the VM, thus skipping the steps of parsing and compiling to opcodes. This helps improve performance.

JIT Compiler in PHP image 2

  • OPCache is not enough because it is memory-limited. Even with an OPcache, there are bottlenecks in many applications. Also, most of modern PHP frameworks tend to be bootstrap-heavy.
  • To overcome this situation PHP 7.4 introduced opcache preloading.
  • This allows administrators to specify scripts that should be pre-compiled into the opcache during language startup.

JIT Compiler in PHP image 3

  • A pool of workers is spawned and initialized as the server starts, and these then consult the opcache preload rules, load those scripts, compile them to byte codes, and cache the byte codes. (like php-fpm)
  • Also, this is again memory-limitted and sometimes can impact negatively if you try to many things in the startup.

What is JIT?

  • In computing, just-in-time (JIT) compilation is a way of executing computer code that involves compilation at run time rather than before execution.
  • OPcache removes the parsing and compilation steps that gave us opcodes for the VM but the VM still needs to compile native machine code. JIT adds an extra layer of caching and caches of the native machine code.

In the PHP way;

  • PHP is an interpreted language so it’s not compiled like a C, Java, or Rust program. Instead, it is translated to machine code (stuff the CPU understands) at runtime.
  • “JIT” is a technique that will compile parts of the code at runtime so that the compiled version can be used instead. Think of it like a “cached version” of the interpreted code, generated at runtime.
  • For CPU-intensive tasks, having a JIT compiler in PHP boasts significant performance gains.

Function-Based JIT

  • it instead analyzes each function, attempting to optimize and compile each path through it.
  • In real-world apps like WordPress, it decreases the other performance gains. That is the problem of function-based JIT.

JIT Compiler in PHP image 4

JIT Compilation in PHP 8

  • Introduced with PHP 8.
  • Using “traces”
  • Traces includes “ what classes were in the context”, “whatt functions, methods and etc.” where called.
  • Based on the how often a trace is called, how long it is, and what operations it performs, the JIT decides if the code can benefit from compilation.

    JIT Compiler in PHP image 5

Not a real case but let's look a generating fractal generation

Here is a good article on JIT performance in real-life web applications.


Check this articles;

PHP Magic Methods Explained
PHP Magic methods explained. What are the PHP Magic methods?

PHP Loops — Which is faster?
Foreach vs for in PHP, which is the fastest loop in PHP?

How To Secure PHP Sessions?
My notes about how to secure PHP Sessions, what is session hijacking, and so