Skip to main content

OpCache and Just-In-Time (JIT) Compilation

1. How does OpCache work?​

OpCache is a caching engine for PHP that improves performance by storing precompiled script bytecode in shared memory.

The Lifecycle without OpCache:​

  1. Reading: The PHP interpreter reads the .php file from disk.
  2. Lexing/Tokenizing: The code is converted into tokens.
  3. Parsing: Tokens are converted into an Abstract Syntax Tree (AST).
  4. Compilation: The AST is compiled into Opcodes (Operation Codes).
  5. Execution: The Zend Engine executes the Opcodes.
  6. Destruction: All resources are freed at the end of the request.

The Lifecycle with OpCache:​

OpCache eliminates steps 1-4 for subsequent requests. After the first compilation, the Opcodes are stored in shared memory (RAM). When the same script is requested again, PHP immediately executes the cached Opcodes, significantly reducing CPU and Disk I/O overhead.


2. How does OpCache work since 8.0 and the new JIT?​

Since PHP 8.0, JIT is implemented as an almost independent part of OpCache. It can be enabled or disabled at PHP compile-time and at run-time.

PHP JIT Chart

In PHP 8.0+, the execution flow changes if JIT is enabled:

  1. OpCache Check: If Opcodes are already in shared memory, they are retrieved.
  2. JIT Analysis: The JIT engine monitors "hot" code (code that is executed frequently).
  3. Machine Code Generation: Instead of just executing Opcodes via the Zend VM, the JIT compiler translates "hot" Opcodes directly into native machine code (x86 or ARM).
  4. Direct Execution: The CPU executes the machine code directly, bypassing the Zend VM interpreter loop for those specific sections.

3. What do you know about JIT?​

JIT stands for Just-In-Time compilation. It is a technique that compiles parts of the code during runtime rather than before execution.

Key Aspects:​

  • Two Engines: PHP 8.0 introduced two JIT modes:
    • Function JIT: Compiles whole functions. It's simpler but less effective.
    • Tracing JIT: (Default/Recommended) It identifies frequently used execution paths (traces) across functions and compiles them.
  • Performance Impact:
    • For Web Applications (I/O bound): JIT often provides negligible gains because the bottleneck is usually the database, network, or filesystem.
    • For CPU-intensive tasks: JIT can provide massive performance boosts (e.g., mathematical calculations, image processing, or data analysis).
  • Control: Managed via opcache.jit and opcache.jit_buffer_size in php.ini.
  • Why it matters: It opens doors for PHP to be used in areas beyond web development, such as machine learning or long-running heavy-computation daemons.