Skip to content

Commit 6043544

Browse files
committed
bug #12855 [DependencyInjection] Perf php dumper (nicolas-grekas)
This PR was merged into the 2.3 branch. Discussion ---------- [DependencyInjection] Perf php dumper | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR came up after this comment to reduce the number of calls to dirname(): #12784 (comment) Commits ------- 375f83e Revert "[DependencyInjection] backport perf optim" fcd8ff9 [DependencyInjection] perf optim: call dirname() at most 5x c11535b [DependencyInjection] backport perf optim
2 parents 6989d0a + 375f83e commit 6043544

File tree

10 files changed

+59
-10
lines changed

10 files changed

+59
-10
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,19 @@ public function dump(array $options = array())
104104
), $options);
105105

106106
if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
107-
// Build a regexp where the first two root dirs are mandatory,
107+
// Build a regexp where the first root dirs are mandatory,
108108
// but every other sub-dir is optional up to the full path in $dir
109+
// Mandate at least 2 root dirs and not more that 5 optional dirs.
109110

110111
$dir = explode(DIRECTORY_SEPARATOR, realpath($dir));
111112
$i = count($dir);
112113

113114
if (3 <= $i) {
114115
$regex = '';
115-
$this->targetDirMaxMatches = $i - 3;
116+
$lastOptionalDir = $i > 8 ? $i - 5 : 3;
117+
$this->targetDirMaxMatches = $i - $lastOptionalDir;
116118

117-
while (2 < --$i) {
119+
while (--$i >= $lastOptionalDir) {
118120
$regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex);
119121
}
120122

@@ -764,6 +766,9 @@ private function startClass($class, $baseClass)
764766
*/
765767
class $class extends $baseClass
766768
{
769+
private \$parameters;
770+
private \$targetDirs = array();
771+
767772
EOF;
768773
}
769774

@@ -774,6 +779,7 @@ class $class extends $baseClass
774779
*/
775780
private function addConstructor()
776781
{
782+
$targetDirs = $this->exportTargetDirs();
777783
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
778784

779785
$code = <<<EOF
@@ -782,7 +788,7 @@ private function addConstructor()
782788
* Constructor.
783789
*/
784790
public function __construct()
785-
{
791+
{{$targetDirs}
786792
parent::__construct($arguments);
787793
788794
EOF;
@@ -811,13 +817,15 @@ public function __construct()
811817
*/
812818
private function addFrozenConstructor()
813819
{
820+
$targetDirs = $this->exportTargetDirs();
821+
814822
$code = <<<EOF
815823
816824
/**
817825
* Constructor.
818826
*/
819827
public function __construct()
820-
{
828+
{{$targetDirs}
821829
EOF;
822830

823831
if ($this->container->getParameterBag()->all()) {
@@ -1351,6 +1359,17 @@ private function getNextVariableName()
13511359
}
13521360
}
13531361

1362+
private function exportTargetDirs()
1363+
{
1364+
return null === $this->targetDirRegex ? '' : <<<EOF
1365+
1366+
\$dir = __DIR__;
1367+
for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
1368+
\$this->targetDirs[\$i] = \$dir = dirname(\$dir);
1369+
}
1370+
EOF;
1371+
}
1372+
13541373
private function export($value)
13551374
{
13561375
if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
@@ -1359,8 +1378,8 @@ private function export($value)
13591378
$suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : '';
13601379
$dirname = '__DIR__';
13611380

1362-
for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) {
1363-
$dirname = sprintf('dirname(%s)', $dirname);
1381+
if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) {
1382+
$dirname = sprintf('$this->targetDirs[%d]', $offset);
13641383
}
13651384

13661385
if ($prefix || $suffix) {

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ public function testDumpRelativeDir()
8585
$definition = new Definition();
8686
$definition->setClass('stdClass');
8787
$definition->addArgument('%foo%');
88-
$definition->addArgument(array('%foo%' => '%foo%'));
88+
$definition->addArgument(array('%foo%' => '%buz%/'));
8989

9090
$container = new ContainerBuilder();
9191
$container->setDefinition('test', $definition);
9292
$container->setParameter('foo', 'wiz'.dirname(dirname(__FILE__)));
9393
$container->setParameter('bar', dirname(__FILE__));
9494
$container->setParameter('baz', '%bar%/PhpDumperTest.php');
95+
$container->setParameter('buz', dirname(dirname(__DIR__)));
9596
$container->compile();
9697

9798
$dumper = new PhpDumper($container);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class Container extends AbstractContainer
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/
2225
public function __construct()
2326
{
27+
$dir = __DIR__;
28+
for ($i = 1; $i <= 5; ++$i) {
29+
$this->targetDirs[$i] = $dir = dirname($dir);
30+
}
2431
$this->parameters = $this->getDefaultParameters();
2532

2633
$this->services =
@@ -48,7 +55,7 @@ public function __construct()
4855
*/
4956
protected function getTestService()
5057
{
51-
return $this->services['test'] = new \stdClass(('wiz'.dirname(__DIR__)), array(('wiz'.dirname(__DIR__)) => ('wiz'.dirname(__DIR__))));
58+
return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), array(('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')));
5259
}
5360

5461
/**
@@ -102,9 +109,10 @@ public function getParameterBag()
102109
protected function getDefaultParameters()
103110
{
104111
return array(
105-
'foo' => ('wiz'.dirname(__DIR__)),
112+
'foo' => ('wiz'.$this->targetDirs[1]),
106113
'bar' => __DIR__,
107114
'baz' => (__DIR__.'/PhpDumperTest.php'),
115+
'buz' => $this->targetDirs[2],
108116
);
109117
}
110118
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
1922
/**
2023
* Constructor.
2124
*/

0 commit comments

Comments
 (0)