From 3bb702a72b1cef1668bce7328ee519064974f3d3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 7 Mar 2016 15:45:39 +0000 Subject: [PATCH 1/2] [HttpKernel] Fix mem usage when stripping the prod container (cherry picked from commit c8cb253) --- .../DependencyInjection/Dumper/PhpDumper.php | 18 ++++++++++++------ src/Symfony/Component/HttpKernel/Kernel.php | 5 +---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 4f65e0f9a331b..09fdc0c4fb177 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -53,6 +53,7 @@ class PhpDumper extends Dumper private $reservedVariables = array('instance', 'class'); private $targetDirRegex; private $targetDirMaxMatches; + private $docStar; /** * @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface @@ -97,7 +98,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, @@ -589,7 +592,7 @@ private function addService($id, $definition) $visibility = $isProxyCandidate ? 'public' : 'protected'; $code = <<docStar} * Gets the '$id' service.$doc *$lazyInitializationDoc * $return @@ -699,7 +702,7 @@ private function addServiceSynchronizer($id, Definition $definition) return <<docStar} * Updates the '$id' service. */ protected function synchronize{$this->camelize($id)}Service() @@ -760,7 +763,7 @@ private function startClass($class, $baseClass) use Symfony\Component\DependencyInjection\Exception\RuntimeException; $bagClass -/** +/*{$this->docStar} * $class. * * This class has been auto-generated @@ -786,7 +789,7 @@ private function addConstructor() $code = <<docStar} * Constructor. */ public function __construct() @@ -823,7 +826,7 @@ private function addFrozenConstructor() $code = <<docStar} * Constructor. */ public function __construct() @@ -970,11 +973,14 @@ public function getParameterBag() return $this->parameterBag; } EOF; + if ('' === $this->docStar) { + $code = str_replace('/**', '/*', $code); + } } $code .= <<docStar} * Gets the default parameters. * * @return array An array of the default parameters diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 04ec2c240b480..3cbd248841f11 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -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()); } From 25c336dfc8e10245b808523189b4088b005d54fa Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Tue, 8 Mar 2016 13:47:06 +0000 Subject: [PATCH 2/2] [HttpKernel] updated to full replace of /** => /* on container string to minimise for opcache in place of stripComments, which has been removed along with tests. --- .../DependencyInjection/Dumper/PhpDumper.php | 20 ++--- src/Symfony/Component/HttpKernel/Kernel.php | 56 ------------- .../Component/HttpKernel/Tests/KernelTest.php | 82 ------------------- 3 files changed, 8 insertions(+), 150 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 09fdc0c4fb177..f7f016f48afd0 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -53,7 +53,6 @@ class PhpDumper extends Dumper private $reservedVariables = array('instance', 'class'); private $targetDirRegex; private $targetDirMaxMatches; - private $docStar; /** * @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface @@ -100,7 +99,6 @@ public function dump(array $options = array()) '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, @@ -143,7 +141,8 @@ public function dump(array $options = array()) ; $this->targetDirRegex = null; - return $code; + // Replace '/**' comment openings with '/*' for non-debug container to optimise for opcache + return $options['debug'] ? $code : str_replace('/**', '/*', $code); } /** @@ -592,7 +591,7 @@ private function addService($id, $definition) $visibility = $isProxyCandidate ? 'public' : 'protected'; $code = <<docStar} + /** * Gets the '$id' service.$doc *$lazyInitializationDoc * $return @@ -702,7 +701,7 @@ private function addServiceSynchronizer($id, Definition $definition) return <<docStar} + /** * Updates the '$id' service. */ protected function synchronize{$this->camelize($id)}Service() @@ -763,7 +762,7 @@ private function startClass($class, $baseClass) use Symfony\Component\DependencyInjection\Exception\RuntimeException; $bagClass -/*{$this->docStar} +/** * $class. * * This class has been auto-generated @@ -789,7 +788,7 @@ private function addConstructor() $code = <<docStar} + /** * Constructor. */ public function __construct() @@ -826,7 +825,7 @@ private function addFrozenConstructor() $code = <<docStar} + /** * Constructor. */ public function __construct() @@ -973,14 +972,11 @@ public function getParameterBag() return $this->parameterBag; } EOF; - if ('' === $this->docStar) { - $code = str_replace('/**', '/*', $code); - } } $code .= <<docStar} + /** * Gets the default parameters. * * @return array An array of the default parameters diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 3cbd248841f11..8498ca969b8bf 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -676,62 +676,6 @@ protected function getContainerLoader(ContainerInterface $container) return new DelegatingLoader($resolver); } - /** - * Removes comments from a PHP source string. - * - * We don't use the PHP php_strip_whitespace() function - * as we want the content to be readable and well-formatted. - * - * @param string $source A PHP string - * - * @return string The PHP string with the comments removed - */ - public static function stripComments($source) - { - if (!function_exists('token_get_all')) { - return $source; - } - - $rawChunk = ''; - $output = ''; - $tokens = token_get_all($source); - $ignoreSpace = false; - for (reset($tokens); false !== $token = current($tokens); next($tokens)) { - if (is_string($token)) { - $rawChunk .= $token; - } elseif (T_START_HEREDOC === $token[0]) { - $output .= $rawChunk.$token[1]; - do { - $token = next($tokens); - $output .= $token[1]; - } while ($token[0] !== T_END_HEREDOC); - $rawChunk = ''; - } elseif (T_WHITESPACE === $token[0]) { - if ($ignoreSpace) { - $ignoreSpace = false; - - continue; - } - - // replace multiple new lines with a single newline - $rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]); - } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { - $ignoreSpace = true; - } else { - $rawChunk .= $token[1]; - - // The PHP-open tag already has a new-line - if (T_OPEN_TAG === $token[0]) { - $ignoreSpace = true; - } - } - } - - $output .= $rawChunk; - - return $output; - } - public function serialize() { return serialize(array($this->environment, $this->debug)); diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 6bf5c258846d3..fcee97aba286d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -292,88 +292,6 @@ public function testHandleBootsTheKernel() $kernel->handle($request, $type, $catch); } - public function testStripComments() - { - $source = <<<'EOF' -assertEquals($expected, $output); - } - public function testIsClassInActiveBundleFalse() { $kernel = $this->getKernelMockForIsClassInActiveBundleTest();