Skip to content

Commit b44c847

Browse files
feature #46279 [DependencyInjection] Optimize autowiring logic by telling it about excluded symbols (nicolas-grekas)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Optimize autowiring logic by telling it about excluded symbols | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Replaces symfony/symfony#46275 While inspecting the dumped container of the demo app, I noticed that it contains so-called "errored" services. They come e.g from entities that are used as type-hints on controllers. To compute those services, the autowiring logic currently loads all services looking for possible type-compatible candidates. But here, we should know that those won't be found: they're excluded from being loaded. Instead of completely ignoring excluded classes, this PR still registers them, but as abstract and with a special `container.excluded` tag. This allows the rest of the compilation logic to skip+remove them, while allowing `AutowiringPass` to compute a better error message and way faster. Here is the relevant part of a https://blackfire.io comparison: ![image](https://user-images.githubusercontent.com/243674/167118702-383a3cd5-a176-4e9f-9218-de48a84fcab3.png) Commits ------- 739789f384 [DependencyInjection] Optimize autowiring logic by telling it about excluded symbols
2 parents 2738f16 + 686b3c2 commit b44c847

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class UnusedTagsPass implements CompilerPassInterface
3434
'container.do_not_inline',
3535
'container.env_var_loader',
3636
'container.env_var_processor',
37+
'container.excluded',
3738
'container.hot_path',
3839
'container.no_preload',
3940
'container.preload',

Resources/config/services.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
1515
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
1617
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1718
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
1819
use Symfony\Component\Console\ConsoleEvents;
@@ -26,7 +27,10 @@
2627
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcherInterfaceComponentAlias;
2728
use Symfony\Component\Filesystem\Filesystem;
2829
use Symfony\Component\Form\FormEvents;
30+
use Symfony\Component\HttpFoundation\Request;
2931
use Symfony\Component\HttpFoundation\RequestStack;
32+
use Symfony\Component\HttpFoundation\Response;
33+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3034
use Symfony\Component\HttpFoundation\UrlHelper;
3135
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
3236
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
@@ -218,5 +222,11 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
218222
->set('config_builder.warmer', ConfigBuilderCacheWarmer::class)
219223
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
220224
->tag('kernel.cache_warmer')
225+
226+
// register as abstract and excluded, aka not-autowirable types
227+
->set(LoaderInterface::class)->abstract()->tag('container.excluded')
228+
->set(Request::class)->abstract()->tag('container.excluded')
229+
->set(Response::class)->abstract()->tag('container.excluded')
230+
->set(SessionInterface::class)->abstract()->tag('container.excluded')
221231
;
222232
};

0 commit comments

Comments
 (0)