Skip to content

Commit

Permalink
bug #18048 [HttpKernel] Fix mem usage when stripping the prod contain…
Browse files Browse the repository at this point in the history
…er (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpKernel] Fix mem usage when stripping the prod container

| Q             | A
| ------------- | ---
| Branch        | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18007
| License       | MIT
| Doc PR        | -

I propose to just replace doc comments by regular comments, so that the parser removes them and opcache doesn't have to keep them in memory, which is the target.

Commits
-------

4fa5844 [HttpKernel] Fix mem usage when stripping the prod container
  • Loading branch information
fabpot committed Mar 9, 2016
2 parents 15ccef7 + 4fa5844 commit fd4edff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
use Symfony\Component\HttpKernel\Kernel;

/**
* PhpDumper dumps a service container as a PHP class.
Expand Down Expand Up @@ -53,6 +54,7 @@ class PhpDumper extends Dumper
private $reservedVariables = array('instance', 'class');
private $targetDirRegex;
private $targetDirMaxMatches;
private $docStar;

/**
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
Expand Down Expand Up @@ -97,7 +99,9 @@ public function dump(array $options = array())
$options = array_merge(array(
'class' => 'ProjectServiceContainer',
'base_class' => 'Container',
'debug' => true,
), $options);
$this->docStar = $options['debug'] ? '*' : '';

if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
// Build a regexp where the first root dirs are mandatory,
Expand Down Expand Up @@ -219,9 +223,15 @@ private function addProxyClasses()
array($this->getProxyDumper(), 'isProxyCandidate')
);
$code = '';
$strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');

foreach ($definitions as $definition) {
$code .= "\n".$this->getProxyDumper()->getProxyCode($definition);
$proxyCode = "\n".$this->getProxyDumper()->getProxyCode($definition);
if ($strip) {
$proxyCode = "<?php\n".$proxyCode;
$proxyCode = substr(Kernel::stripComments($proxyCode), 5);
}
$code .= $proxyCode;
}

return $code;
Expand Down Expand Up @@ -589,7 +599,7 @@ private function addService($id, $definition)
$visibility = $isProxyCandidate ? 'public' : 'protected';
$code = <<<EOF
/**
/*{$this->docStar}
* Gets the '$id' service.$doc
*$lazyInitializationDoc
* $return
Expand Down Expand Up @@ -699,7 +709,7 @@ private function addServiceSynchronizer($id, Definition $definition)

return <<<EOF
/**
/*{$this->docStar}
* Updates the '$id' service.
*/
protected function synchronize{$this->camelize($id)}Service()
Expand Down Expand Up @@ -760,7 +770,7 @@ private function startClass($class, $baseClass)
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
$bagClass
/**
/*{$this->docStar}
* $class.
*
* This class has been auto-generated
Expand All @@ -786,7 +796,7 @@ private function addConstructor()

$code = <<<EOF
/**
/*{$this->docStar}
* Constructor.
*/
public function __construct()
Expand Down Expand Up @@ -823,7 +833,7 @@ private function addFrozenConstructor()

$code = <<<EOF
/**
/*{$this->docStar}
* Constructor.
*/
public function __construct()
Expand Down Expand Up @@ -970,11 +980,14 @@ public function getParameterBag()
return $this->parameterBag;
}
EOF;
if ('' === $this->docStar) {
$code = str_replace('/**', '/*', $code);
}
}

$code .= <<<EOF
/**
/*{$this->docStar}
* Gets the default parameters.
*
* @return array An array of the default parameters
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -650,10 +650,7 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
$dumper->setProxyDumper(new ProxyDumper(md5((string) $cache)));
}

$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache));
if (!$this->debug) {
$content = static::stripComments($content);
}
$content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass, 'file' => (string) $cache, 'debug' => $this->debug));

$cache->write($content, $container->getResources());
}
Expand Down

0 comments on commit fd4edff

Please sign in to comment.