Skip to content

Commit 86670b0

Browse files
committed
minor #21192 [DI] Dont call get() when not required - µ-optim (nicolas-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 ------- 7a42199115 [DI] Dont call get() when not required - µ-optim
2 parents 2bbcf6c + dee05d2 commit 86670b0

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

Dumper/PhpDumper.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,19 +1539,24 @@ private function getServiceCall($id, Reference $reference = null)
15391539
}
15401540

15411541
if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1542-
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0
1543-
1544-
return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
1545-
}
1546-
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
1547-
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
1542+
$code = sprintf('$this->%s()', $this->generateMethodName($id));
1543+
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
1544+
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
15481545
} else {
15491546
if ($this->container->hasAlias($id)) {
15501547
$id = (string) $this->container->getAlias($id);
15511548
}
15521549

1553-
return sprintf('$this->get(\'%s\')', $id);
1550+
$code = sprintf('$this->get(\'%s\')', $id);
15541551
}
1552+
1553+
if ($this->container->hasDefinition($id) && (!$this->container->getDefinition($id)->isPublic() || $this->container->getDefinition($id)->isShared())) {
1554+
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? $code)" on PHP>=7.0
1555+
1556+
$code = "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : $code) && false ?: '_'}";
1557+
}
1558+
1559+
return $code;
15551560
}
15561561

15571562
/**

Tests/Fixtures/php/services31.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ protected function getBarService()
6262
$this->services['bar'] = $instance = new \stdClass();
6363

6464
$instance->foo = array(0 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withNoArgs */ function () {
65-
return $this->get('foo')->withNoArgs();
65+
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withNoArgs();
6666
}, 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)) {
67-
return $this->get('foo')->withArgs($a, $b, $c);
67+
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withArgs($a, $b, $c);
6868
}, 2 => /** @closure-proxy Symfony\Component\DependencyInjection\Tests\Fixtures\Container31\Foo::withRefs */ function &(&$a = NULL, &$b) {
69-
return $this->get('foo')->withRefs($a, $b);
69+
return ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->get('foo')) && false ?: '_'}->withRefs($a, $b);
7070
});
7171

7272
return $instance;

Tests/Fixtures/php/services32.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ protected function getBarService()
6262
$this->services['bar'] = $instance = new \stdClass();
6363

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

7272
return $instance;

Tests/Fixtures/php/services9.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct()
7676
*/
7777
protected function getBarService()
7878
{
79-
$a = $this->get('foo.baz');
79+
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
8080

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

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

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

102102
return $instance;
103103
}
@@ -113,7 +113,7 @@ protected function getBazService()
113113
protected function getClosureProxyService()
114114
{
115115
return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () {
116-
return $this->get('closure_proxy')->getBaz();
116+
return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz();
117117
});
118118
}
119119

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

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

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

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

@@ -315,7 +315,7 @@ protected function getLazyContextService()
315315
{
316316
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() {
317317
yield 0 => 'foo';
318-
yield 1 => $this->get('foo.baz');
318+
yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
319319
yield 2 => array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
320320
yield 3 => true;
321321
yield 4 => $this;
@@ -333,7 +333,7 @@ protected function getLazyContextService()
333333
protected function getLazyContextIgnoreInvalidRefService()
334334
{
335335
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() {
336-
yield 0 => $this->get('foo.baz');
336+
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
337337
if ($this->has('invalid')) {
338338
yield 1 => $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE);
339339
}
@@ -354,7 +354,7 @@ protected function getMethodCall1Service()
354354

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

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

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

431431
return $instance;
432432
}
@@ -482,7 +482,7 @@ protected function getInlinedService()
482482
$this->services['inlined'] = $instance = new \Bar();
483483

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

487487
return $instance;
488488
}

Tests/Fixtures/php/services9_compiled.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function isFrozen()
8282
*/
8383
protected function getBarService()
8484
{
85-
$a = $this->get('foo.baz');
85+
$a = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
8686

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

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

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

108108
return $instance;
109109
}
@@ -119,7 +119,7 @@ protected function getBazService()
119119
protected function getClosureProxyService()
120120
{
121121
return $this->services['closure_proxy'] = new \BarClass(/** @closure-proxy BarClass::getBaz */ function () {
122-
return $this->get('closure_proxy')->getBaz();
122+
return ${($_ = isset($this->services['closure_proxy']) ? $this->services['closure_proxy'] : $this->get('closure_proxy')) && false ?: '_'}->getBaz();
123123
});
124124
}
125125

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

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

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

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

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

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

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

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

300300
$instance->setBar($a);
301301

@@ -314,7 +314,7 @@ protected function getLazyContextService()
314314
{
315315
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function() {
316316
yield 0 => 'foo';
317-
yield 1 => $this->get('foo.baz');
317+
yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
318318
yield 2 => array('bar' => 'foo is '.'bar'.'', 'foobar' => 'bar');
319319
yield 3 => true;
320320
yield 4 => $this;
@@ -332,7 +332,7 @@ protected function getLazyContextService()
332332
protected function getLazyContextIgnoreInvalidRefService()
333333
{
334334
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function() {
335-
yield 0 => $this->get('foo.baz');
335+
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
336336
}));
337337
}
338338

@@ -350,7 +350,7 @@ protected function getMethodCall1Service()
350350

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

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

0 commit comments

Comments
 (0)