From 75e2ee17b0f87fcf085f982df321cc5b7a6f6579 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 27 Jun 2020 12:33:05 +0200 Subject: [PATCH] [DI] fix minor perf regression when creating non-shared services --- .../DependencyInjection/Dumper/PhpDumper.php | 26 ++++++++++++++----- .../Tests/Fixtures/php/services9_as_files.txt | 12 +++++++-- .../Tests/Fixtures/php/services9_compiled.php | 6 ++++- .../php/services9_inlined_factories.txt | 12 +++++++-- .../php/services_almost_circular_public.php | 10 ++++--- .../php/services_errored_definition.php | 6 ++++- .../Fixtures/php/services_non_shared_lazy.php | 8 ++++-- .../php/services_non_shared_lazy_as_files.txt | 6 ++++- .../php/services_service_locator_argument.php | 6 ++++- 9 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 91c0ff5c9e916..cda9a49acfb9b 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -864,13 +864,23 @@ protected function {$methodName}($lazyInitialization) } if ($this->getProxyDumper()->isProxyCandidate($definition)) { - $factoryCode = $asFile ? "\$this->load('%s', false)" : '$this->%s(false)'; - $factoryCode = $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName)); + $factoryCode = $definition->isShared() ? ($asFile ? "\$this->load('%s', false)" : '$this->%s(false)') : '$this->factories[%2$s](false)'; + $factoryCode = $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName, $this->doExport($id))); $code .= $asFile ? preg_replace('/function \(([^)]*+)\) {/', 'function (\1) use ($container) {', $factoryCode) : $factoryCode; } $code .= $this->addServiceInclude($id, $definition); - $code .= $this->addInlineService($id, $definition); + $c = $this->addInlineService($id, $definition); + + if (!$definition->isShared()) { + $c = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $c))); + $factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); + $lazyloadInitialization = $definition->isLazy() ? '$lazyLoad = true' : ''; + + $c = sprintf(" %s = function (%s) {\n%s };\n\n return %1\$s();\n", $factory, $lazyloadInitialization, $c); + } + + $code .= $c; } if ($asFile) { @@ -1880,10 +1890,14 @@ private function getServiceCall(string $id, Reference $reference = null): string $code = sprintf('$this->%s[%s] = %s', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); } $code = "($code)"; - } elseif ($this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition)) { - $code = sprintf("\$this->load('%s')", $this->generateMethodName($id)); } else { - $code = sprintf('$this->%s()', $this->generateMethodName($id)); + $code = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) ? "\$this->load('%s')" : '$this->%s()'; + $code = sprintf($code, $this->generateMethodName($id)); + + if (!$definition->isShared()) { + $factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); + $code = sprintf('(isset(%s) ? %1$s() : %s)', $factory, $code); + } } if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) { $code = sprintf('($this->%s[%s] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt index 7818c091a00d4..c7291046caf79 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt @@ -402,7 +402,11 @@ class getFooBarService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - return new \Bar\FooClass(($container->services['deprecated_service'] ?? $container->load('getDeprecatedServiceService'))); + $container->factories['foo_bar'] = function () use ($container) { + return new \Bar\FooClass(($container->services['deprecated_service'] ?? $container->load('getDeprecatedServiceService'))); + }; + + return $container->factories['foo_bar'](); } } @@ -574,7 +578,11 @@ class getNonSharedFooService extends ProjectServiceContainer { include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; - return new \Bar\FooClass(); + $container->factories['non_shared_foo'] = function () use ($container) { + return new \Bar\FooClass(); + }; + + return $container->factories['non_shared_foo'](); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 277da470b544c..671a1b11b3417 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -275,7 +275,11 @@ protected function getFoo_BazService() */ protected function getFooBarService() { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $this->factories['foo_bar'] = function () { + return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + }; + + return $this->factories['foo_bar'](); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt index 6a3af3fbb9bd0..cf0543d4eeea8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt @@ -300,7 +300,11 @@ class ProjectServiceContainer extends Container */ protected function getFooBarService() { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $this->factories['foo_bar'] = function () { + return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + }; + + return $this->factories['foo_bar'](); } /** @@ -398,7 +402,11 @@ class ProjectServiceContainer extends Container { include_once $this->targetDir.''.'/Fixtures/includes/foo.php'; - return new \Bar\FooClass(); + $this->factories['non_shared_foo'] = function () { + return new \Bar\FooClass(); + }; + + return $this->factories['non_shared_foo'](); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php index 1554588ac381e..4905ade2dd408 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php @@ -287,11 +287,15 @@ protected function getFoo2Service() */ protected function getFoo4Service() { - $instance = new \stdClass(); + $this->factories['foo4'] = function () { + $instance = new \stdClass(); - $instance->foobar = ($this->services['foobar4'] ?? $this->getFoobar4Service()); + $instance->foobar = ($this->services['foobar4'] ?? $this->getFoobar4Service()); - return $instance; + return $instance; + }; + + return $this->factories['foo4'](); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php index 9f2bb02158a92..99595f647851f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php @@ -275,7 +275,11 @@ protected function getFoo_BazService() */ protected function getFooBarService() { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $this->factories['foo_bar'] = function () { + return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + }; + + return $this->factories['foo_bar'](); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php index 4295512b087c0..048e4fdf59b59 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php @@ -57,7 +57,7 @@ protected function createProxy($class, \Closure $factory) */ protected function getBarService() { - return $this->services['bar'] = new \stdClass($this->getFooService()); + return $this->services['bar'] = new \stdClass((isset($this->factories['service_container']['foo']) ? $this->factories['service_container']['foo']() : $this->getFooService())); } /** @@ -69,7 +69,11 @@ protected function getFooService($lazyLoad = true) { // lazy factory for stdClass - return new \stdClass(); + $this->factories['service_container']['foo'] = function ($lazyLoad = true) { + return new \stdClass(); + }; + + return $this->factories['service_container']['foo'](); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt index ebdcacd1b4d28..a42d1e29db3e2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt @@ -30,7 +30,11 @@ class getNonSharedFooService extends ProjectServiceContainer { include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php'; - return new \Bar\FooLazyClass(); + $container->factories['non_shared_foo'] = function ($lazyLoad = true) { + return new \Bar\FooLazyClass(); + }; + + return $container->factories['non_shared_foo'](); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php index 14873b484c2d1..c695bb8e49e17 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php @@ -107,7 +107,11 @@ protected function getFoo2Service() */ protected function getFoo3Service() { - return new \stdClass(); + $this->factories['service_container']['foo3'] = function () { + return new \stdClass(); + }; + + return $this->factories['service_container']['foo3'](); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy