From d1f72d8ef4e1b2b4bc20c6c8736858004fcb7005 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 14 Jan 2016 14:08:57 +0100 Subject: [PATCH] Fix perf and mem issue when using token_get_all --- .../Translation/PhpExtractor.php | 6 +++--- .../ClassLoader/ClassCollectionLoader.php | 17 +++++++++-------- .../Component/ClassLoader/ClassMapGenerator.php | 17 +++++++++-------- src/Symfony/Component/HttpKernel/Kernel.php | 9 +++++---- .../Component/HttpKernel/Tests/KernelTest.php | 4 ++-- .../Routing/Loader/AnnotationFileLoader.php | 14 +++++++------- 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php b/src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php index e8479eb5cf..8bfa99da62 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php @@ -81,7 +81,7 @@ class PhpExtractor implements ExtractorInterface */ protected function normalizeToken($token) { - if (is_array($token)) { + if (isset($token[1]) && 'b"' !== $token) { return $token[1]; } @@ -95,7 +95,7 @@ class PhpExtractor implements ExtractorInterface { for (; $tokenIterator->valid(); $tokenIterator->next()) { $t = $tokenIterator->current(); - if (!is_array($t) || ($t[0] !== T_WHITESPACE)) { + if (T_WHITESPACE !== $t[0]) { break; } } @@ -112,7 +112,7 @@ class PhpExtractor implements ExtractorInterface for (; $tokenIterator->valid(); $tokenIterator->next()) { $t = $tokenIterator->current(); - if (!is_array($t)) { + if (!isset($t[1])) { break; } diff --git a/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php b/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php index f811cd0a71..a137a3cf5c 100644 --- a/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php +++ b/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php @@ -149,8 +149,9 @@ class ClassCollectionLoader $inNamespace = false; $tokens = token_get_all($source); - for (reset($tokens); false !== $token = current($tokens); next($tokens)) { - if (is_string($token)) { + for ($i = 0; isset($tokens[$i]); ++$i) { + $token = $tokens[$i]; + if (!isset($token[1]) || 'b"' === $token) { $rawChunk .= $token; } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { // strip comments @@ -162,12 +163,12 @@ class ClassCollectionLoader $rawChunk .= $token[1]; // namespace name and whitespaces - while (($t = next($tokens)) && is_array($t) && in_array($t[0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) { - $rawChunk .= $t[1]; + while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) { + $rawChunk .= $tokens[$i][1]; } - if ('{' === $t) { + if ('{' === $tokens[$i]) { $inNamespace = false; - prev($tokens); + --$i; } else { $rawChunk = rtrim($rawChunk)."\n{"; $inNamespace = true; @@ -175,8 +176,8 @@ class ClassCollectionLoader } elseif (T_START_HEREDOC === $token[0]) { $output .= self::compressCode($rawChunk).$token[1]; do { - $token = next($tokens); - $output .= is_string($token) ? $token : $token[1]; + $token = $tokens[++$i]; + $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; } while ($token[0] !== T_END_HEREDOC); $output .= "\n"; $rawChunk = ''; diff --git a/src/Symfony/Component/ClassLoader/ClassMapGenerator.php b/src/Symfony/Component/ClassLoader/ClassMapGenerator.php index a4ddce5115..2f88df12e3 100644 --- a/src/Symfony/Component/ClassLoader/ClassMapGenerator.php +++ b/src/Symfony/Component/ClassLoader/ClassMapGenerator.php @@ -95,10 +95,10 @@ class ClassMapGenerator $classes = array(); $namespace = ''; - for ($i = 0, $max = count($tokens); $i < $max; ++$i) { + for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; - if (is_string($token)) { + if (!isset($token[1])) { continue; } @@ -108,9 +108,9 @@ class ClassMapGenerator case T_NAMESPACE: $namespace = ''; // If there is a namespace, extract it - while (($t = $tokens[++$i]) && is_array($t)) { - if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) { - $namespace .= $t[1]; + while (isset($tokens[++$i][1])) { + if (in_array($tokens[$i][0], array(T_STRING, T_NS_SEPARATOR))) { + $namespace .= $tokens[$i][1]; } } $namespace .= '\\'; @@ -121,7 +121,7 @@ class ClassMapGenerator // Skip usage of ::class constant $isClassConstant = false; for ($j = $i - 1; $j > 0; --$j) { - if (is_string($tokens[$j])) { + if (!isset($tokens[$j][1])) { break; } @@ -138,10 +138,11 @@ class ClassMapGenerator } // Find the classname - while (($t = $tokens[++$i]) && is_array($t)) { + while (isset($tokens[++$i][1])) { + $t = $tokens[$i]; if (T_STRING === $t[0]) { $class .= $t[1]; - } elseif ($class !== '' && T_WHITESPACE == $t[0]) { + } elseif ('' !== $class && T_WHITESPACE === $t[0]) { break; } } diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 04ec2c240b..6df51ae12d 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -699,14 +699,15 @@ abstract class Kernel implements KernelInterface, TerminableInterface $output = ''; $tokens = token_get_all($source); $ignoreSpace = false; - for (reset($tokens); false !== $token = current($tokens); next($tokens)) { - if (is_string($token)) { + for ($i = 0; isset($tokens[$i]); ++$i) { + $token = $tokens[$i]; + if (!isset($token[1]) || 'b"' === $token) { $rawChunk .= $token; } elseif (T_START_HEREDOC === $token[0]) { $output .= $rawChunk.$token[1]; do { - $token = next($tokens); - $output .= $token[1]; + $token = $tokens[++$i]; + $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; } while ($token[0] !== T_END_HEREDOC); $rawChunk = ''; } elseif (T_WHITESPACE === $token[0]) { diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 6bf5c25884..fa1294e806 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -307,7 +307,7 @@ modified'; $heredoc = <<