Lesson 6 of 9 · 20m
OPcache and JIT: Performance Without Superstition
What OPcache definitely gives you, what JIT does and does not accelerate, and the production settings worth understanding.
Most PHP performance folklore comes from benchmarks measuring the wrong thing. In production, OPcache is non-negotiable because reparsing every file on every request is waste. JIT is more nuanced: it can accelerate CPU-heavy numeric PHP, but most web applications spend their lives waiting on databases, Redis, HTTP clients, and serialization. The correct posture is simple — always understand OPcache, measure before crediting JIT.
OPcache is the baseline
A typical PHP-FPM pool benefits from explicit settings rather than distro defaults:
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=50000
opcache.validate_timestamps=0
opcache.preload=/var/www/app/config/preload.php
opcache.jit=tracing
opcache.jit_buffer_size=64MWith validate_timestamps=0, PHP stops checking whether files changed. That is excellent for performance and catastrophic if deployment does not reset OPcache. Atomic deploys plus php-fpm reload, or a carefully controlled opcache_reset(), are part of the release pipeline — not an afterthought after the first stale-code incident.
Preloading moves work to boot
Preloading compiles selected framework and application files when FPM starts. It reduces per-request work and can expose classes as if they were internal. It also makes dependency ordering real: preload the wrong file before its parent class or interface and startup fails. Treat preload scripts as build artifacts and test them in the exact production PHP minor version.
JIT compiles hot PHP code, not your database
Tracing JIT watches hot execution paths and emits machine code. CPU-bound algorithms — parsers, numerical work, tight transformation loops — can benefit. ORM-heavy CRUD usually does not because the time is outside PHP userland. In some workloads JIT memory pressure or extra bookkeeping costs more than it saves.
Measure with production-shaped traffic:
php -v
php -i | grep -E 'opcache\.jit|opcache\.enable'
ab -n 2000 -c 50 https://staging.example.internal/api/invoicesThen compare latency percentiles, not just mean throughput. A runtime change that improves p50 but worsens p99 can make checkout feel worse.
Memory is an operational contract
OPcache memory is shared by the FPM master and workers. If it fills, PHP may evict or fail to cache files depending on configuration, creating confusing performance cliffs. Monitor hit rate, wasted memory, restarts, and interned-string usage. Frameworks with tens of thousands of files need max_accelerated_files above the real file count, not a neat round number copied from a blog post.
Field notes
- Never enable OPcache timestamp validation in production as a substitute for a broken deploy pipeline.
- Xdebug and OPcache both alter execution; benchmark with Xdebug disabled. Production should not run Xdebug at all unless you enjoy pain.
- JIT is unrelated to type correctness. Adding types may help analysis and sometimes engine optimization, but do not promise a JIT speedup from types alone.
- CLI workers usually need OPcache disabled or deliberately configured; a long-running worker with stale assumptions behaves differently from request-scoped FPM.
- Container images make PHP version, ini files, preload output, and deployment order reproducible. Snowflake servers make OPcache incidents archaeological expeditions.
Was this lesson helpful?