Use a raw array for Parser::buildTree

Improve memory usage by ~15% for very large templates.
This commit is contained in:
Justin Hileman
2013-05-11 22:32:42 -07:00
parent ade0b98761
commit 697f4ac26b
+6 -8
View File
@@ -26,7 +26,7 @@ class Mustache_Parser
*/ */
public function parse(array $tokens = array()) 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. * @throws Mustache_Exception_SyntaxException when nesting errors or mismatched section tags are encountered.
* *
* @param ArrayIterator $tokens Stream of Mustache tokens * @param array &$tokens Set of Mustache tokens
* @param array $parent Parent token (default: null) * @param array $parent Parent token (default: null)
* *
* @return array Mustache Token parse tree * @return array Mustache Token parse tree
*/ */
private function buildTree(ArrayIterator $tokens, array $parent = null) private function buildTree(array &$tokens, array $parent = null)
{ {
$nodes = array(); $nodes = array();
do { while (!empty($tokens)) {
$token = $tokens->current(); $token = array_shift($tokens);
$tokens->next();
if ($token === null) { if ($token === null) {
continue; continue;
@@ -78,8 +77,7 @@ class Mustache_Parser
break; break;
} }
} }
};
} while ($tokens->valid());
if (isset($parent)) { if (isset($parent)) {
$msg = sprintf('Missing closing tag: %s', $parent[Mustache_Tokenizer::NAME]); $msg = sprintf('Missing closing tag: %s', $parent[Mustache_Tokenizer::NAME]);