8000 bug #18048 [HttpKernel] Fix mem usage when stripping the prod contain… · symfony/symfony@fd4edff · GitHub
[go: up one dir, main page]

Skip to content

Commit fd4edff

Browse files
committed
bug #18048 [HttpKernel] Fix mem usage when stripping the prod container (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
2 parents 15ccef7 + 4fa5844 commit fd4edff

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2424
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
2525
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
26+
use Symfony\Component\HttpKernel\Kernel;
2627

2728
/**
2829
* PhpDumper dumps a service container as a PHP class.
@@ -53,6 +54,7 @@ class PhpDumper extends Dumper
5354
private $reservedVariables = array('instance', 'class');
5455
private $targetDirRegex;
5556
private $targetDirMaxMatches;
57+
private $docStar;
5658

5759
/**
5860
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -97,7 +99,9 @@ public function dump(array $options = array())
9799
$options = array_merge(array(
98100
'class' => 'ProjectServiceContainer',
99101
'base_class' => 'Container',
102+
'debug' => true,
100103
), $options);
104+
$this->docStar = $options['debug'] ? '*' : '';
101105

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

223228
foreach ($definitions as $definition) {
224-
$code .= "\n".$this->getProxyDumper()->getProxyCode($definition);
229+
$proxyCode = "\n".$this->getProxyDumper()->getProxyCode($definition);
230+
if ($strip) {
231+
$proxyCode = "<?php\n".$proxyCode;
232+
$proxyCode = substr(Kernel::stripComments($proxyCode), 5);
233+
}
234+
$code .= $proxyCode;
225235
}
226236

227237
return $code;
@@ -589,7 +599,7 @@ private function addService($id, $definition)
589599
$visibility = $isProxyCandidate ? 'public' : 'protected';
590600
$code = <<<EOF
591601
592-
/**
602+
/*{$this->docStar}
593603
* Gets the '$id' service.$doc
594604
*$lazyInitializationDoc
595605
* $return
@@ -699,7 +709,7 @@ private function addServiceSynchronizer($id, Definition $definition)
699709

700710
return <<<EOF
701711
702-
/**
712+
/*{$this->docStar}
703713
* Updates the '$id' service.
704714
*/
705715
protected function synchronize{$this->camelize($id)}Service()
@@ -760,7 +770,7 @@ private function startClass($class, $baseClass)
760770
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
761771
$bagClass
762772
763-
/**
773+
/*{$this->docStar}
764774
* $class.
765775
*
766776
* This class has been auto-generated
@@ -786,7 +796,7 @@ private function addConstructor()
786796

787797
$code = <<<EOF
788798
789-
/**
799+
/*{$this->docStar}
790800
* Constructor.
791801
*/
792802
public function __construct()
@@ -823,7 +833,7 @@ private function addFrozenConstructor()
823833

824834
$code = <<<EOF
825835
826-
/**
836+
/*{$this->docStar}
827837
* Constructor.
828838
*/
829839
public function __construct()
@@ -970,11 +980,14 @@ public function getParameterBag()
970980
return $this->parameterBag;
971981
}
972982
EOF;
983+
if ('' === $this->docStar) {
984+
$code = str_replace('/**', '/*', $code);
985+
}
973986
}
974987

975988
$code .= <<<EOF
976989
977-
/**
990+
/*{$this->docStar}
978991
* Gets the default parameters.
979992
*
980993
* @return array An array of the default parameters

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,7 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
650650
$dumper->setProxyDumper(new ProxyDumper(md5((string) $cache)));
651651
}
652652

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

658655
$cache->write($content, $container->getResources());
659656
}

0 commit comments

Comments
 (0)
0