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.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • 1
    Not sure how much https://www.reddit.com/r/PHP/comments/e8pgzi/weve_found_some_fundamental_design_problems_in/ will affect any plans for using it. – Nigel Ren Dec 12 '19 at 08:01
  • 2
    [Symfony on preloading](https://symfony.com/blog/new-in-symfony-4-4-preloading-symfony-applications-in-php-7-4) – jibsteroos Dec 12 '19 at 08:03
  • 1
    Check [this article](https://stitcher.io/blog/preloading-in-php-74). Also read that bug Nigel posted, as it seems quite critical for the time being. – Jeto Dec 12 '19 at 08:04

2 Answers2

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.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
  • 2
    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. – vishal Jun 18 '20 at 06:40
  • @vishal i've got the same issue. Did you eventually find the reason ? – rugolinifr Sep 14 '20 at 10:58
  • Sure, upgrade PHP to the latest version. – Mike Doe Sep 14 '20 at 11:17
  • @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. – SeparateReality Oct 08 '20 at 10:26
  • One day for sure ;) – Mike Doe Oct 08 '20 at 10:35
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.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46