Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] fix minor perf regression when creating non-shared services #37435

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
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}
}

Expand Down Expand Up @@ -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']();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

/**
Expand Down Expand Up @@ -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']();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/**
Expand All @@ -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']();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}

/**
Expand Down