How can I enable opcache preloading in PHP 7.4?
Asked Answered
S

2

14

I'd like to enable opcache preloading (RFC) on my production servers in PHP 7.4.

I'm using Symfony 4 if it changes anything.

Sn answered 12/12, 2019 at 7:58 Comment(3)
Not sure how much reddit.com/r/PHP/comments/e8pgzi/… will affect any plans for using it.Jovi
Symfony on preloadingTracheid
Check this article. Also read that bug Nigel posted, as it seems quite critical for the time being.Piecedyed
S
10

According to the blog post this appears to be trivial. Apparently Symfony since 4.4 generates a preload script which has to be set in the php.ini:

opcache.preload=/path/to/project/var/cache/prod/App_KernelProdContainer.preload.php

I did some tests in my local Docker environment and this is how it went:

PHP 7.3 without OPcache (current)

Requests per second:    8.75 [#/sec] (mean)
Time per request:       114.402 [ms] (mean)

PHP 7.4 without OPcache

Requests per second:    11.44 [#/sec] (mean)
Time per request:       87.417 [ms] (mean)

PHP 7.4 with OPcache, without preloading (Apache + modphp)

Requests per second:    30.25 [#/sec] (mean)
Time per request:       33.053 [ms]

PHP 7.4 with OPcache, without preloading (nginx + php fpm)

Requests per second:    40.00 [#/sec] (mean)

Unfortunately I was not able to enable the preloading :( I encountered following errors (in both Apache+Mod and Nginx+FPM):

double free or corruption (!prev)
child pid 17 exit signal Aborted (6), possible coredump (…)

This feature looks like a WIP though. I'm going to revalidate this answer when I'm able to use this preloading thing. In overall I'm quite impressed, +30% performance just by upgrading from PHP 7.3 to 7.4.

Sn answered 14/12, 2019 at 20:37 Comment(5)
I am trying preload on Symfony with fairly large size application, I am getting errors like Cannot declare class because the name is already in use. I am using require_once in preload file to be able to load dependencies as well which probably is conflicting with loaded classes.Teage
@Teage i've got the same issue. Did you eventually find the reason ?Measly
Sure, upgrade PHP to the latest version.Sn
@emix You might want to update your answer since its really great and I almost obmitted it because of the written conclusion that it does not work. Fortunately I saw your comment in time.Commerce
One day for sure ;)Sn
J
8

First of all you should add:

opcache.preload=/path/to/project/preload.php

to your php.ini

Then in your PHP script you should pass in opcache_compile_file($file); each file you want to preloaded.

Jolynjolynn answered 12/12, 2019 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.