From 697f4ac26b0f7d3093085dd544a9dd32ce361372 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 11 May 2013 22:32:42 -0700 Subject: [PATCH] Use a raw array for Parser::buildTree Improve memory usage by ~15% for very large templates. --- src/Mustache/Parser.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Mustache/Parser.php b/src/Mustache/Parser.php index ab7db84..f983ad8 100644 --- a/src/Mustache/Parser.php +++ b/src/Mustache/Parser.php @@ -26,7 +26,7 @@ class Mustache_Parser */ public function parse(array $tokens = array()) { - return $this->buildTree(new ArrayIterator($tokens)); + return $this->buildTree($tokens); } /** @@ -34,18 +34,17 @@ class Mustache_Parser * * @throws Mustache_Exception_SyntaxException when nesting errors or mismatched section tags are encountered. * - * @param ArrayIterator $tokens Stream of Mustache tokens - * @param array $parent Parent token (default: null) + * @param array &$tokens Set of Mustache tokens + * @param array $parent Parent token (default: null) * * @return array Mustache Token parse tree */ - private function buildTree(ArrayIterator $tokens, array $parent = null) + private function buildTree(array &$tokens, array $parent = null) { $nodes = array(); - do { - $token = $tokens->current(); - $tokens->next(); + while (!empty($tokens)) { + $token = array_shift($tokens); if ($token === null) { continue; @@ -78,8 +77,7 @@ class Mustache_Parser break; } } - - } while ($tokens->valid()); + }; if (isset($parent)) { $msg = sprintf('Missing closing tag: %s', $parent[Mustache_Tokenizer::NAME]);