Make your PHP 8 apps twice as fast (OPCache & JIT)

Edouard Courty
3 min readJul 17, 2022

--

PHP 8 comes with a lot of performance optimizations, and a brand new feature called JIT (Just In Time) compiling.

If you don’t know anything about JIT, this PHPWatch article gives a great explanation and it can be useful to have the basic knowledge to understand what’s happening next.

While some papers present crazy benchmark graphs with more than 70% performance boost by enabling JIT, the “real-life” performance boost in web apps is closer to 40/50% faster execution time.

I’ve run benchmarks with a Symfony 6 / PHP 8.1 app running on Docker with Nginx & PHP-FPM, here is the outcome:

Symfony 6 / PHP 8.1 app

Enabling JIT can only be done if OPCache is enabled on the server.

I’ll go through the installation of OPCache and its configuration using Docker.
The first step is to install the OPCache extension on your PHP server,
here is my folder configuration:

I copy my opcache.ini and php.ini inside the container, and install the OPCache PHP extension with the built-in PHP extension manager (using the official PHP-FPM images, here php:8.1-fpm-bullseye

COPY docker/php/php.ini /usr/local/etc/php/php.ini
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

RUN docker-php-ext-install opcache

Your php.ini file should contain the following directives:

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=500000000
opcache.jit=1235

And your opcache.ini should contain the following:

[opcache]
opcache.enable
=1
; 0 means it will check on every request
; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in production
opcache.revalidate_freq
=0
opcache.validate_timestamps=1
opcache.max_accelerated_files=10000
opcache.memory_consumption=192
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1

This is a basic configuration that will work with any type of project.

I won’t go through all the values since they are pretty self-explanatory.
If you want to know more about the opcache.ini configuration, this official doc page explains everything.

You can now rebuild your Docker using docker-compose build and restart your containers to use the new image.

Using the Symfony debug bar, we see that OPCache is enabled.

Symfony Debug Bar

Note that JIT cannot be enabled if XDebug is, which is not ideal in most dev environments where XDebug is widely used.

By disabling XDebug and reading phpinfo, we see that OPCache is running and JIT compiling is enabled.

phpinfo()

Vising the homepage of my app now takes 60ms instead of 110–120ms before enabling JIT, meaning a gain of 50% performance!

Going even faster

This app is running in dev mode right now. Changing the APP_ENV to prod and rebuilding the cache makes the homepage load in 30ms!

That’s it, your Symfony app is now faster than ever.

Feel free to comment below to share your experiences with JIT and OPCache, and feel free to ask any related questions.

Have a good day!

I hope this article helped you understand the ins and outs of PHP optimization, If you like my content and want to get updates on my future articles, feel free to follow my account!

--

--

Edouard Courty

Web Developer & IT Teacher based in Paris - Back-end guru - Co-founder of @IMXrarity