Skip to content

Commit

Permalink
minor #21192 [DI] Dont call get() when not required - µ-optim (nicola…
Browse files Browse the repository at this point in the history
…s-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Dont call get() when not required - µ-optim

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

(could be applied on 3.2, please advise)
Helps a bit on https://rawgit.com/kocsismate/php-di-container-benchmarks/master/var/benchmark.html

note: if we want more performance, we should consider deprecating case insensitivity of service identifiers.

Commits
-------

7a42199 [DI] Dont call get() when not required - µ-optim
  • Loading branch information
fabpot committed Jan 7, 2017
2 parents e317a12 + 7a42199 commit 96c9e20
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
19 changes: 12 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -1539,19 +1539,24 @@ private function getServiceCall($id, Reference $reference = null)
}

if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0

return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
}
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
$code = sprintf('$this->%s()', $this->generateMethodName($id));
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
} else {
if ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}

return sprintf('$this->get(\'%s\')', $id);
$code = sprintf('$this->get(\'%s\')', $id);
}

if ($this->container->hasDefinition($id) && (!$this->container->getDefinition($id)->isPublic() || $this->container->getDefinition($id)->isShared())) {
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? $code)" on PHP>=7.0

$code = "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : $code) && false ?: '_'}";
}

return $code;
}

/**
Expand Down
Expand Up @@ -62,11 +62,11 @@ protected function getBarService()
$this->services['bar'] = $instance = new \stdClass();

$instance->foo = array(0 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withNoArgs */ function () {
return $this->get('foo')->withNoArgs();
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withNoArgs();
}, 1 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withArgs */ function ($a, \Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo $b = NULL, $c = array(0 => 123)) {
return $this->get('foo')->withArgs($a, $b, $c);
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withArgs($a, $b, $c);
}, 2 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withRefs */ function &(&$a = NULL, &$b) {
return $this->get('foo')->withRefs($a, $b);
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withRefs($a, $b);
});

return $instance;
Expand Down
Expand Up @@ -62,11 +62,11 @@ protected function getBarService()
$this->services['bar'] = $instance = new \stdClass();

$instance->foo = array(0 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withVariadic */ function ($a, &...$c) {
return $this->get('foo')->withVariadic($a, ...$c);
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withVariadic($a, ...$c);
}, 1 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withNullable */ function (?int $a) {
return $this->get('foo')->withNullable($a);
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withNullable($a);
}, 2 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container32\Foo::withReturnType */ function () {
return $this->get('foo')->withReturnType();
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withReturnType();
});

return $instance;
Expand Down
Expand Up @@ -76,7 +76,7 @@ public function __construct()
*/
protected function getBarService()
{
$a = $this->get('foo.baz');
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};

$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));

Expand All @@ -97,7 +97,7 @@ protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'});

return $instance;
}
Expand All @@ -113,7 +113,7 @@ protected function getBazService()
protected function getClosureProxyService()
{
return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () {
return $this->get('closure_proxy')->getBaz();
return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz();
});
}

Expand Down Expand Up @@ -217,7 +217,7 @@ protected function getDeprecatedServiceService()
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance();
}

/**
Expand All @@ -243,14 +243,14 @@ protected function getFactoryServiceSimpleService()
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};

$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);

$instance->foo = 'bar';
$instance->moo = $a;
$instance->qux = array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
$instance->setBar($this->get('bar'));
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'});
$instance->initialize();
sc_configure($instance);

Expand Down Expand Up @@ -315,7 +315,7 @@ protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() {
yield 0 => 'foo';
yield 1 => $this->get('foo.baz');
yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 2 => array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
yield 3 => true;
yield 4 => $this;
Expand All @@ -333,7 +333,7 @@ protected function getLazyContextService()
protected function getLazyContextIgnoreInvalidRefService()
{
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() {
yield 0 => $this->get('foo.baz');
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
if ($this->has('invalid')) {
yield 1 => $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE);
}
Expand All @@ -354,7 +354,7 @@ protected function getMethodCall1Service()

$this->services['method_call1'] = $instance = new \Bar\FooClass();

$instance->setBar($this->get('foo'));
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'});
$instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE));
if ($this->has('foo3')) {
$instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE));
Expand Down Expand Up @@ -426,7 +426,7 @@ protected function getConfiguratorServiceService()
{
$this->services['configurator_service'] = $instance = new \ConfClass();

$instance->setFoo($this->get('baz'));
$instance->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});

return $instance;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ protected function getInlinedService()
$this->services['inlined'] = $instance = new \Bar();

$instance->pub = 'pub';
$instance->setBaz($this->get('baz'));
$instance->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});

return $instance;
}
Expand Down
Expand Up @@ -82,7 +82,7 @@ public function isFrozen()
*/
protected function getBarService()
{
$a = $this->get('foo.baz');
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};

$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));

Expand All @@ -103,7 +103,7 @@ protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));
$instance->setFoo(${($_ = isset($this->services['foo_with_inline']) ? $this->services['foo_with_inline'] : $this->get('foo_with_inline')) && false ?: '_'});

return $instance;
}
Expand All @@ -119,7 +119,7 @@ protected function getBazService()
protected function getClosureProxyService()
{
return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () {
return $this->get('closure_proxy')->getBaz();
return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz();
});
}

Expand All @@ -134,7 +134,7 @@ protected function getClosureProxyService()
protected function getConfiguredServiceService()
{
$a = new \ConfClass();
$a->setFoo($this->get('baz'));
$a->setFoo(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});

$this->services['configured_service'] = $instance = new \stdClass();

Expand Down Expand Up @@ -213,7 +213,7 @@ protected function getDeprecatedServiceService()
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
return $this->services['factory_service'] = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}->getInstance();
}

/**
Expand All @@ -239,14 +239,14 @@ protected function getFactoryServiceSimpleService()
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};

$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);

$instance->foo = 'bar';
$instance->moo = $a;
$instance->qux = array('bar' => 'foo is bar', 'foobar' => 'bar');
$instance->setBar($this->get('bar'));
$instance->setBar(${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'});
$instance->initialize();
sc_configure($instance);

Expand Down Expand Up @@ -295,7 +295,7 @@ protected function getFooWithInlineService()
$this->services['foo_with_inline'] = $instance = new \Foo();

$a->pub = 'pub';
$a->setBaz($this->get('baz'));
$a->setBaz(${($_ = isset($this->services['baz']) ? $this->services['baz'] : $this->get('baz')) && false ?: '_'});

$instance->setBar($a);

Expand All @@ -314,7 +314,7 @@ protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() {
yield 0 => 'foo';
yield 1 => $this->get('foo.baz');
yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 2 => array('bar' => 'foo is '.'bar'.'', 'foobar' => 'bar');
yield 3 => true;
yield 4 => $this;
Expand All @@ -332,7 +332,7 @@ protected function getLazyContextService()
protected function getLazyContextIgnoreInvalidRefService()
{
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() {
yield 0 => $this->get('foo.baz');
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
}));
}

Expand All @@ -350,7 +350,7 @@ protected function getMethodCall1Service()

$this->services['method_call1'] = $instance = new \Bar\FooClass();

$instance->setBar($this->get('foo'));
$instance->setBar(${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'});
$instance->setBar(NULL);
$instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));

Expand Down

0 comments on commit 96c9e20

Please sign in to comment.