8000 [DependencyInjection] perf optim: call dirname() at most 5x · symfony/symfony@fcd8ff9 · GitHub
[go: up one dir, main page]

Skip to content

Commit fcd8ff9

Browse files
[DependencyInjection] perf optim: call dirname() at most 5x
1 parent c11535b commit fcd8ff9

File tree

9 files changed

+41
-11
lines changed

9 files changed

+41
-11
lines changed

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

Lines changed: 25 additions & 8 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

@@ -765,6 +767,7 @@ private function startClass($class, $baseClass)
765767
class $class extends $baseClass
766768
{
767769
private \$parameters;
770+
private \$targetDirs;
768771
769772
EOF;
770773
}
@@ -777,14 +780,15 @@ class $class extends $baseClass
777780
private function addConstructor()
778781
{
779782
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
783+
$targetDirs = $this->exportTargetDirs();
780784

781785
$code = <<<EOF
782786
783787
/**
784788
* Constructor.
785789
*/
786790
public function __construct()
787-
{
791+
{{$targetDirs}
788792
\$this->parameters = $parameters;
789793
790794
parent::__construct(new ParameterBag(\$this->parameters));
@@ -816,14 +820,15 @@ public function __construct()
816820
private function addFrozenConstructor()
817821
{
818822
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
823+
$targetDirs = $this->exportTargetDirs();
819824

820825
$code = <<<EOF
821826
822827
/**
823828
* Constructor.
824829
*/
825830
public function __construct()
826-
{
831+
{{$targetDirs}
827832
\$this->services =
828833
\$this->scopedServices =
829834
\$this->scopeStacks = array();
@@ -1335,16 +1340,28 @@ private function getNextVariableName()
13351340
}
13361341
}
13371342

1343+
private function exportTargetDirs()
1344+
{
1345+
return null === $this->targetDirRegex ? '' : <<<EOF
1346+
1347+
\$dir = __DIR__;
1348+
for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
1349+
\$this->targetDirs[\$i] = \$dir = dirname(\$dir);
1350+
}
1351+
EOF;
1352+
}
1353+
13381354
private function export($value)
13391355
{
13401356
if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
13411357
$prefix = $matches[0][1] ? var_export(substr($value, 0, $matches[0][1]), true).'.' : '';
13421358
$suffix = $matches[0][1] + strlen($matches[0][0]);
13431359
$suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : '';
1344-
$dirname = '__DIR__';
13451360

1346-
for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) {
1347-
$dirname = sprintf('dirname(%s)', $dirname);
1361+
if (0 < $dirname = 1 + $this->targetDirMaxMatches - count($matches)) {
1362+
$dirname = sprintf('$this->targetDirs[%d]', $dirname);
1363+
} else {
1364+
$dirname = '__DIR__';
13481365
}
13491366

13501367
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
@@ -70,13 +70,14 @@ public function testDumpRelativeDir()
7070
$definition = new Definition();
7171
$definition->setClass('stdClass');
7272
$definition->addArgument('%foo%');
73-
$definition->addArgument(array('%foo%' => '%foo%'));
73+
$definition->addArgument(array('%foo%' => '%buz%/'));
7474

7575
$container = new ContainerBuilder();
7676
$container->setDefinition('test', $definition);
7777
$container->setParameter('foo', 'wiz'.dirname(dirname(__FILE__)));
7878
$container->setParameter('bar', dirname(__FILE__));
7979
$container->setParameter('baz', '%bar%/PhpDumperTest.php');
80+
$container->setParameter('buz', dirname(dirname(__DIR__)));
8081
$container->compile();
8182

8283
$dumper = new PhpDumper($container);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class Container extends AbstractContainer
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.
2324
*/
2425
public function __construct()
2526
{
27+
$dir = __DIR__;
28+
for ($i = 1; $i <= 5; ++$i) {
29+
$this->targetDirs[$i] = $dir = dirname($dir);
30+
}
2631
$this->services =
2732
$this->scopedServices =
2833
$this->scopeStacks = array();
2934
$this->parameters = array(
30-
'foo' => ('wiz'.dirname(__DIR__)),
35+
'foo' => ('wiz'.$this->targetDirs[1]),
3136
'bar' => __DIR__,
3237
'baz' => (__DIR__.'/PhpDumperTest.php'),
38+
'buz' => $this->targetDirs[2],
3339
);
3440

3541
$this->set('service_container', $this);
@@ -53,7 +59,7 @@ public function __construct()
5359
*/
5460
protected function getTestService()
5561
{
56-
return $this->services['test'] = new \stdClass(('wiz'.dirname(__DIR__)), array(('wiz'.dirname(__DIR__)) => ('wiz'.dirname(__DIR__))));
62+
return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), array(('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')));
5763
}
5864

5965
/**

src/Symfony/Component/DependencyInjection/Tests/F 102A0 ixtures/php/services8.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
20+
private $targetDirs;
2021

2122
/**
2223
* Constructor.

0 commit comments

Comments
 (0)
0