10000 minor #10241 [DependencyInjection] made some perf improvements (fabpot) · symfony/symfony@181e460 · GitHub
[go: up one dir, main page]

Skip to content

Commit 181e460

Browse files
committed
minor #10241 [DependencyInjection] made some perf improvements (fabpot)
This PR was merged into the 2.6-dev branch. Discussion ---------- [DependencyInjection] made some perf improvements | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This PR optimizes the creation of dumped containers drastically (thanks @jpauli for the hint). The Container class generated by the PHP dumper does not include the `getDefaultParameters()` method anymore. It does not seem like a big deal to me as I fail to see a use case where someone would override this method. Commits ------- e1a3ef8 [DependencyInjection] made some perf improvements
2 parents ff17493 + e1a3ef8 commit 181e460

File tree

8 files changed

+57
-104
lines changed

8 files changed

+57
-104
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -800,16 +800,18 @@ class $class extends $baseClass
800800
*/
801801
private function addConstructor()
802802
{
803-
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
803+
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
804804

805805
$code = <<<EOF
806806
807+
private static \$parameters = $parameters;
808+
807809
/**
808810
* Constructor.
809811
*/
810812
public function __construct()
811813
{
812-
parent::__construct($arguments);
814+
parent::__construct(new ParameterBag(self::\$parameters));
813815
814816
EOF;
815817

@@ -837,23 +839,17 @@ public function __construct()
837839
*/
838840
private function addFrozenConstructor()
839841
{
842+
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
843+
840844
$code = <<<EOF
841845
842-
private \$parameters;
846+
private static \$parameters = $parameters;
843847
844848
/**
845849
* Constructor.
846850
*/
847851
public function __construct()
848852
{
849-
EOF;
850-
851-
if ($this->container->getParameterBag()->all()) {
852-
$code .= "\n \$this->parameters = \$this->getDefaultParameters();\n";
853-
}
854-
855-
$code .= <<<EOF
856-
857853
\$this->services =
858854
\$this->scopedServices =
859855
\$this->scopeStacks = array();
@@ -961,8 +957,6 @@ private function addDefaultParametersMethod()
961957
return '';
962958
}
963959

964-
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
965-
966960
$code = '';
967961
if ($this->container->isFrozen()) {
968962
$code .= <<<EOF
@@ -974,11 +968,11 @@ public function getParameter(\$name)
974968
{
975969
\$name = strtolower(\$name);
976970
977-
if (!(isset(\$this->parameters[\$name]) || array_key_exists(\$name, \$this->parameters))) {
971+
if (!(isset(self::\$parameters[\$name]) || array_key_exists(\$name, self::\$parameters))) {
978972
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', \$name));
979973
}
980974
981-
return \$this->parameters[\$name];
975+
return self::\$parameters[\$name];
982976
}
983977
984978
/**
@@ -988,7 +982,7 @@ public function hasParameter(\$name)
988982
{
989983
\$name = strtolower(\$name);
990984
991-
return isset(\$this->parameters[\$name]) || array_key_exists(\$name, \$this->parameters);
985+
return isset(self::\$parameters[\$name]) || array_key_exists(\$name, self::\$parameters);
992986
}
993987
994988
/**
@@ -1005,27 +999,14 @@ public function setParameter(\$name, \$value)
1005999
public function getParameterBag()
10061000
{
10071001
if (null === \$this->parameterBag) {
1008-
\$this->parameterBag = new FrozenParameterBag(\$this->parameters);
1002+
\$this->parameterBag = new FrozenParameterBag(self::\$parameters);
10091003
}
10101004
10111005
return \$this->parameterBag;
10121006
}
1013-
EOF;
1014-
}
1015-
1016-
$code .= <<<EOF
1017-
1018-
/**
1019-
* Gets the default parameters.
1020-
*
1021-
* @return array An array of the default parameters
1022-
*/
1023-
protected function getDefaultParameters()
1024-
{
1025-
return $parameters;
1026-
}
10271007
10281008
EOF;
1009+
}
10291010

10301011
return $code;
10311012
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
*/
1818
class Container extends AbstractContainer
1919
{
20+
private static $parameters = array(
21+
22+
);
23+
2024
/**
2125
* Constructor.
2226
*/
2327
public function __construct()
2428
{
25-
parent::__construct();
29+
parent::__construct(new ParameterBag(self::$parameters));
2630
}
2731
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private static $parameters = array(
20+
21+
);
22+
1923
/**
2024
* Constructor.
2125
*/
2226
public function __construct()
2327
{
24-
parent::__construct();
28+
parent::__construct(new ParameterBag(self::$parameters));
2529
}
2630
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19-
private $parameters;
19+
private static $parameters = array(
20+
'empty_value' => '',
21+
'some_string' => '-',
22+
);
2023

2124
/**
2225
* Constructor.
2326
*/
2427
public function __construct()
2528
{
26-
$this->parameters = $this->getDefaultParameters();
27-
2829
$this->services =
2930
$this->scopedServices =
3031
$this->scopeStacks = array();
@@ -68,11 +69,11 @@ public function getParameter($name)
6869
{
6970
$name = strtolower($name);
7071

71-
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
72+
if (!(isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters))) {
7273
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
7374
}
7475

75-
return $this->parameters[$name];
76+
return self::$parameters[$name];
7677
}
7778

7879
/**
@@ -82,7 +83,7 @@ public function hasParameter($name)
8283
{
8384
$name = strtolower($name);
8485

85-
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
86+
return isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters);
8687
}
8788

8889
/**
@@ -99,21 +100,9 @@ public function setParameter($name, $value)
99100
public function getParameterBag()
100101
{
101102
if (null === $this->parameterBag) {
102-
$this->parameterBag = new FrozenParameterBag($this->parameters);
103+
$this->parameterBag = new FrozenParameterBag(self::$parameters);
103104
}
104105

105106
return $this->parameterBag;
106107
}
107-
/**
108-
* Gets the default parameters.
109-
*
110-
* @return array An array of the default parameters
111-
*/
112-
protected function getDefaultParameters()
113-
{
114-
return array(
115-
'empty_value' => '',
116-
'some_string' => '-',
117-
);
118-
}
119108
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19-
private $parameters;
19+
private static $parameters = array(
20+
21+
);
2022

2123
/**
2224
* Constructor.
< 10000 div class="pt-3">

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,7 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19-
/**
20-
* Constructor.
21-
*/
22-
public function __construct()
23-
{
24-
parent::__construct(new ParameterBag($this->getDefaultParameters()));
25-
}
26-
27-
/**
28-
* Gets the default parameters.
29-
*
30-
* @return array An array of the default parameters
31-
*/
32-
protected function getDefaultParameters()
33-
{
34-
return array(
19+
private static $parameters = array(
3520
'foo' => '%baz%',
3621
'baz' => 'bar',
3722
'bar' => 'foo is %%foo bar',
@@ -47,5 +32,12 @@ protected function getDefaultParameters()
4732
7 => 'null',
4833
),
4934
);
35+
36+
/**
37+
* Constructor.
38+
*/
39+
public function __construct()
40+
{
41+
parent::__construct(new ParameterBag(self::$parameters));
5042
}
5143
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private static $parameters = array(
20+
'baz_class' => 'BazClass',
21+
'foo_class' => 'Bar\\FooClass',
22+
'foo' => 'bar',
23+
);
24+
1925
/**
2026
* Constructor.
2127
*/
2228
public function __construct()
2329
{
24-
parent::__construct(new ParameterBag($this->getDefaultParameters()));
30+
parent::__construct(new ParameterBag(self::$parameters));
2531
$this->methodMap = array(
2632
'bar' => 'getBarService',
2733
'baz' => 'getBazService',
@@ -370,18 +376,4 @@ protected function getNewFactoryService()
370376

371377
return $instance;
372378
}
373-
374-
/**
375-
* Gets the default parameters.
376-
*
377-
* @return array An array of the default parameters
378-
*/
379-
protected function getDefaultParameters()
380-
10000 {
381-
return array(
382-
'baz_class' => 'BazClass',
383-
'foo_class' => 'Bar\\FooClass',
384-
'foo' => 'bar',
385-
);
386-
}
387379
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19-
private $parameters;
19+
private static $parameters = array(
20+
'baz_class' => 'BazClass',
21+
'foo_class' => 'Bar\\FooClass',
22+
'foo' => 'bar',
23+
);
2024

2125
/**
2226
* Constructor.
2327
*/
2428
public function __construct()
2529
{
26-
$this->parameters = $this->getDefaultParameters();
27-
2830
$this->services =
2931
$this->scopedServices =
3032
$this->scopeStacks = array();
@@ -320,11 +322,11 @@ public function getParameter($name)
320322
{
321323
$name = strtolower($name);
322324

323-
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
325+
if (!(isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters))) {
324326
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
325327
}
326328

327-
return $this->parameters[$name];
329+
return self::$parameters[$name];
328330
}
329331

330332
/**
@@ -334,7 +336,7 @@ public function hasParameter($name)
334336
{
335337
$name = strtolower($name);
336338

337-
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
339+
return isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters);
338340
}
339341

340342
/**
@@ -351,22 +353,9 @@ public function setParameter($name, $value)
351353
public function getParameterBag()
352354
{
353355
if (null === $this->parameterBag) {
354-
$this->parameterBag = new FrozenParameterBag($this->parameters);
356+
$this->parameterBag = new FrozenParameterBag(self::$parameters);
355357
}
356358

357359
return $this->parameterBag;
358360
}
359-
/**
360-
* Gets the default parameters.
361-
*
362-
* @return array An array of the default parameters
363-
*/
364-
protected function getDefaultParameters()
365-
{
366-
return array(
367-
'baz_class' => 'BazClass',
368-
'foo_class' => 'Bar\\FooClass',
369-
'foo' => 'bar',
370-
);
371-
}
372361
}

0 commit comments

Comments
 (0)
0