Skip to content

Commit 47eb90b

Browse files
committed
minor #24872 [DI] Add "container.hot_path" tag to flag the hot path and inline related services (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Add "container.hot_path" tag to flag the hot path and inline related services | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR is the result of my quest to squeeze some performance out of 3.4/4.0. It builds on two ideas: - a new `container.inline` tag that identifies the services that are *always* needed. This tag is only applied to a very short list of bootstrapping services (`router`, `event_dispatcher`, `http_kernel` and `request_stack` only). Then, it is propagated to all dependencies of these services, with a special case for event listeners, where only listed events are propagated to their related listeners. - replacing the PHP autoloader by plain inlined `require_once` in generated service factories, with the benefit of completely bypassing the autoloader for services and their class hierarchy. The end result is significant, even on a simple Hello World. Here is the Blackfire profile, results are consistent with `ab` benchmarks: https://blackfire.io/profiles/compare/b5fa5ef0-755c-4967-b990-572305f8f381/graph ![capture du 2017-11-08 16-54-28](https://user-images.githubusercontent.com/243674/32558666-a3f439b2-c4a5-11e7-83a3-db588c3e21e5.png) Commits ------- f7cb559a06 [DI] Add "container.hot_path" tag to flag the hot path and inline related services
2 parents 1bee96d + 5889003 commit 47eb90b

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class UnusedTagsPass implements CompilerPassInterface
2424
private $whitelist = array(
2525
'cache.pool.clearer',
2626
'console.command',
27+
'container.hot_path',
2728
'container.service_locator',
2829
'container.service_subscriber',
2930
'controller.service_arguments',

FrameworkBundle.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Symfony\Component\Form\DependencyInjection\FormPass;
4444
use Symfony\Component\HttpFoundation\Request;
4545
use Symfony\Component\HttpKernel\Bundle\Bundle;
46+
use Symfony\Component\HttpKernel\KernelEvents;
4647
use Symfony\Component\Config\Resource\ClassExistenceResource;
4748
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
4849
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
@@ -83,14 +84,22 @@ public function build(ContainerBuilder $container)
8384
{
8485
parent::build($container);
8586

87+
$hotPathEvents = array(
88+
KernelEvents::REQUEST,
89+
KernelEvents::CONTROLLER,
90+
KernelEvents::CONTROLLER_ARGUMENTS,
91+
KernelEvents::RESPONSE,
92+
KernelEvents::FINISH_REQUEST,
93+
);
94+
8695
$container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
8796
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
8897
$container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
8998
$container->addCompilerPass(new RoutingResolverPass());
9099
$container->addCompilerPass(new ProfilerPass());
91100
// must be registered before removing private services as some might be listeners/subscribers
92101
// but as late as possible to get resolved parameters
93-
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
102+
$container->addCompilerPass((new RegisterListenersPass())->setHotPathEvents($hotPathEvents), PassConfig::TYPE_BEFORE_REMOVING);
94103
$container->addCompilerPass(new TemplatingPass());
95104
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class, PassConfig::TYPE_BEFORE_REMOVING);
96105
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);

Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher" public="true">
1111
<argument type="service" id="service_container" />
12+
<tag name="container.hot_path" />
1213
</service>
1314
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />
1415

@@ -17,6 +18,7 @@
1718
<argument type="service" id="controller_resolver" />
1819
<argument type="service" id="request_stack" />
1920
<argument type="service" id="argument_resolver" />
21+
<tag name="container.hot_path" />
2022
</service>
2123
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" />
2224

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/class-loader": "~3.2",
2323
"symfony/dependency-injection": "~3.4|~4.0",
2424
"symfony/config": "~3.4|~4.0",
25-
"symfony/event-dispatcher": "^3.3.1|~4.0",
25+
"symfony/event-dispatcher": "^3.4-beta4|~4.0-beta4",
2626
"symfony/http-foundation": "^3.3.11|~4.0",
2727
"symfony/http-kernel": "~3.4|~4.0",
2828
"symfony/polyfill-mbstring": "~1.0",

0 commit comments

Comments
 (0)