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():
https://github.com/symfony/symfony/pull/12784#issuecomment-65619179

Commits
-------

375f83e Revert "[DependencyInjection] backport perf optim"
fcd8ff9 [DependencyInjection] perf optim: call dirname() at most 5x
c11535b [DependencyInjection] backport perf optim
This commit is contained in:
Fabien Potencier 2014-12-11 19:39:10 +01:00
commit 6043544a37
10 changed files with 59 additions and 10 deletions

View File

@ -104,17 +104,19 @@ class PhpDumper extends Dumper
), $options);
if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
// Build a regexp where the first two root dirs are mandatory,
// Build a regexp where the first root dirs are mandatory,
// but every other sub-dir is optional up to the full path in $dir
// Mandate at least 2 root dirs and not more that 5 optional dirs.
$dir = explode(DIRECTORY_SEPARATOR, realpath($dir));
$i = count($dir);
if (3 <= $i) {
$regex = '';
$this->targetDirMaxMatches = $i - 3;
$lastOptionalDir = $i > 8 ? $i - 5 : 3;
$this->targetDirMaxMatches = $i - $lastOptionalDir;
while (2 < --$i) {
while (--$i >= $lastOptionalDir) {
$regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex);
}
@ -764,6 +766,9 @@ $bagClass
*/
class $class extends $baseClass
{
private \$parameters;
private \$targetDirs = array();
EOF;
}
@ -774,6 +779,7 @@ EOF;
*/
private function addConstructor()
{
$targetDirs = $this->exportTargetDirs();
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
$code = <<<EOF
@ -782,7 +788,7 @@ EOF;
* Constructor.
*/
public function __construct()
{
{{$targetDirs}
parent::__construct($arguments);
EOF;
@ -811,13 +817,15 @@ EOF;
*/
private function addFrozenConstructor()
{
$targetDirs = $this->exportTargetDirs();
$code = <<<EOF
/**
* Constructor.
*/
public function __construct()
{
{{$targetDirs}
EOF;
if ($this->container->getParameterBag()->all()) {
@ -1351,6 +1359,17 @@ EOF;
}
}
private function exportTargetDirs()
{
return null === $this->targetDirRegex ? '' : <<<EOF
\$dir = __DIR__;
for (\$i = 1; \$i <= {$this->targetDirMaxMatches}; ++\$i) {
\$this->targetDirs[\$i] = \$dir = dirname(\$dir);
}
EOF;
}
private function export($value)
{
if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) {
@ -1359,8 +1378,8 @@ EOF;
$suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : '';
$dirname = '__DIR__';
for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) {
$dirname = sprintf('dirname(%s)', $dirname);
if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) {
$dirname = sprintf('$this->targetDirs[%d]', $offset);
}
if ($prefix || $suffix) {

View File

@ -85,13 +85,14 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$definition = new Definition();
$definition->setClass('stdClass');
$definition->addArgument('%foo%');
$definition->addArgument(array('%foo%' => '%foo%'));
$definition->addArgument(array('%foo%' => '%buz%/'));
$container = new ContainerBuilder();
$container->setDefinition('test', $definition);
$container->setParameter('foo', 'wiz'.dirname(dirname(__FILE__)));
$container->setParameter('bar', dirname(__FILE__));
$container->setParameter('baz', '%bar%/PhpDumperTest.php');
$container->setParameter('buz', dirname(dirname(__DIR__)));
$container->compile();
$dumper = new PhpDumper($container);

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class Container extends AbstractContainer
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,11 +16,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/
public function __construct()
{
$dir = __DIR__;
for ($i = 1; $i <= 5; ++$i) {
$this->targetDirs[$i] = $dir = dirname($dir);
}
$this->parameters = $this->getDefaultParameters();
$this->services =
@ -48,7 +55,7 @@ class ProjectServiceContainer extends Container
*/
protected function getTestService()
{
return $this->services['test'] = new \stdClass(('wiz'.dirname(__DIR__)), array(('wiz'.dirname(__DIR__)) => ('wiz'.dirname(__DIR__))));
return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), array(('wiz'.$this->targetDirs[1]) => ($this->targetDirs[2].'/')));
}
/**
@ -102,9 +109,10 @@ class ProjectServiceContainer extends Container
protected function getDefaultParameters()
{
return array(
'foo' => ('wiz'.dirname(__DIR__)),
'foo' => ('wiz'.$this->targetDirs[1]),
'bar' => __DIR__,
'baz' => (__DIR__.'/PhpDumperTest.php'),
'buz' => $this->targetDirs[2],
);
}
}

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/

View File

@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/