JIT Compiler in PHP

Atakan Demircioğlu
Developers Keep Learning
4 min readNov 27, 2022

--

Here are my notes about the JIT compiler in PHP.

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.
  • 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.
  • 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 cpmpile native machine code. JIT adds an extra layer of caching and caches of the nativa 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 perfomance gains. That is the problem of function-based JIT.

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.

Not a real case but lets look a generating fractal generation

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

References

If you like to get more updates from me, 
please follow me on Medium and subscribe to email alert.

Recommended Articles;

--

--

Atakan Demircioğlu
Developers Keep Learning

Passionate about blogging and sharing insights on tech, web development, and beyond. Join me on this digital journey! 🚀