8000 [DependencyInjection] Optimize PHP preloading by lyrixx · Pull Request #42855 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Optimize PHP preloading #42855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2021

Conversation

lyrixx
Copy link
Member
@lyrixx lyrixx commented Sep 2, 2021
Q A
Branch? 5.4
Bug fix?
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

I'm trying to solve a warning when my app is booting:

[02-Sep-2021 10:02:59] NOTICE: PHP message: PHP Warning:  Can't preload unlinked class Symfony\Component\Security\Core\Authorization\ExpressionLanguageProvider: Unknown interface Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface in /app/vendor/symfony/security-core/Authorization/ExpressionLanguageProvider.php on line 22

It looks like it's not fixable since PHP parse all the file,
even if the class declaration is wrapped in an if (false) statement.

But anyway, I found some optimisations

  • reuse the $preloaded var, to avoid parsing the same list many times
    (Preloader::preload()) is called many times in var/cache/prod/App_KernelProdContainer.preload.php
  • call class_exist before using the reflection to avoid a useless throw / catch.

@lyrixx lyrixx force-pushed the dic-preload branch 2 times, most recently from e320c71 to 9b68844 Compare September 2, 2021 14:48
@@ -342,7 +342,7 @@ class %s extends {$options['class']}

$code[$options['class'].'.preload.php'] .= <<<'EOF'

Preloader::preload($classes);
$preloaded = Preloader::preload($classes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this variable used anywhere ?

Copy link
Member Author
@lyrixx lyrixx Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, when you run bin/console cache:clear --env=prod --no-debug you end with this file var/cache/prod/App_KernelProdContainer.preload.php: (real file from a real project)

<?php

// This file has been auto-generated by the Symfony Dependency Injection Component
// You can reference it in the "opcache.preload" php.ini setting on PHP >= 7.4 when preloading is desired

use Symfony\Component\DependencyInjection\Dumper\Preloader;

if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) {
    return;
}

require dirname(__DIR__, 3).'/vendor/autoload.php';
require __DIR__.'/ContainerWzBLRqh/App_KernelProdContainer.php';
// ...

$classes = [];
$classes[] = 'Symfony\Bundle\FrameworkBundle\FrameworkBundle';
// ...

$preloaded = Preloader::preload($classes);
require_once __DIR__.'/twig/ed/ed5588e4aa2c633d6e4d1ab3a4513d9ba8365c3d12ebf428cc741d0da77555cd.php';
require_once __DIR__.'/twig/49/499cbd92134d953c76afb5025f1db3a2ab80405e5971f6477a4893d6928ad6b1.php';
require_once __DIR__.'/twig/70/704863ffbc575094d43b51f64894f32131dfa045a8849be92596d5a3ef3bb124.php';
require_once __DIR__.'/twig/31/31ea17e321effcd4f6b72864a9ba4eee5a3849c2c2436548fbebd21f2a3dc823.php';
require_once __DIR__.'/twig/d0/d0b8370d51aa15d688c5fb120a8b4aa0bad0e8d0d83b82f0844d231fc5b55dab.php';
require_once __DIR__.'/twig/4b/4bdeb26a0aab0c783ca61a2abee99bf90b433575fa5a94018934045559ee92c4.php';
require_once __DIR__.'/twig/75/757a58fcce449e67afd245d7b534220bfa904be6bb609fee1d3697f190e643b5.php';
require_once __DIR__.'/twig/a0/a0e10cd2137ef92e0c2830af70a0f2dcb1735c3df7a8b75a5301452b9c0ff318.php';
require_once __DIR__.'/twig/5f/5f2b87645662d27d37e09186986a410fff2c2832049772faf3e5cd525990fc33.php';
require_once __DIR__.'/twig/a1/a10f955a1b640aaaac06ac7f68d3212517ed58bcef5fc6662ef960a1696999fe.php';
require_once __DIR__.'/twig/b8/b8361b189000a17b07c5a71e1d332cee5b494b7536c74ffdaf5d5bca005d7a62.php';
require_once __DIR__.'/twig/d9/d9c6b24a80943c5e6a8cf92f3cee97848164b499eb8d4ac8a927db166c05017a.php';
require_once __DIR__.'/twig/69/697075e5aee5dcff27ae63ee1760644f903f8f339acb93e3c94ae934d87c1ff7.php';
require_once __DIR__.'/twig/95/95405ba95db8b50746810a48a9717b91f03d37902660a21b8cd42544ca2ad2bb.php';
require_once __DIR__.'/twig/63/63757e44a1693f2e575b4d97e6d48061d850b4487005a36eee679beec51d5868.php';
require_once __DIR__.'/twig/d5/d5ff445375b9931f663b70a3ca0322fe9c4abfb814ec746b782e09b4f79cd9ef.php';
require_once __DIR__.'/twig/11/112b39f1c877dd6764f788b7576d3bf6aa84c28f6884d9d554b652f3036bb36b.php';
require_once __DIR__.'/twig/8a/8a16aca553faf2413588e3b4949150fb831140a8238bdfbc32fad4b921dddd99.php';
require_once __DIR__.'/twig/33/33cdddcb0de0b87241956866a55e8cb7d53b2f3fd5e827984f53123805c8ae26.php';
require_once __DIR__.'/twig/4b/4b95e8c57f4f0d295bb47af900ba46f6f55da1cbe14b43e0457076bff217b583.php';
require_once __DIR__.'/twig/87/8731269ece3a9764a7f799d4c4b0bcb87e712f11071daaf2b821365836d18ae3.php';

$classes = [];
$classes[] = 'Symfony\\Component\\Validator\\Mapping\\ClassMetadata';
$classes[] = 'Symfony\\Component\\Form\\Extension\\Validator\\Constraints\\Form';
$classes[] = 'Symfony\\Component\\Routing\\Generator\\CompiledUrlGenerator';
$classes[] = 'Symfony\\Bundle\\FrameworkBundle\\Routing\\RedirectableCompiledUrlMatcher';
$preloaded = Preloader::preload($classes, $preloaded);

So the preloaded is given to the next call to Preloader::preload, and so we avoid reploading again all files
And in my case, I divide by two the number of warning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this $preloaded = Preloader::preload($classes, $preloaded); coming from ? I don't see it in your diff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derrabus derrabus added this to the 5.4 milestone Sep 3, 2021
Copy link
Member
@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question and good to me.

@nicolas-grekas
Copy link
Member

Thank you @lyrixx.

@nicolas-grekas nicolas-grekas merged commit 8042736 into symfony:5.4 Sep 8, 2021
@lyrixx lyrixx deleted the dic-preload branch September 8, 2021 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0