From: Fabien Potencier Date: Sun, 16 Oct 2011 20:18:02 +0000 (+0200) Subject: added an optimization when the same temporary variable would have been created twice X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e7347160cbc73f3e7a8cb83a5ce3498082d05812;p=web%2Fkonrad%2Ftwig.git added an optimization when the same temporary variable would have been created twice --- diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index 196c5d7..c9c2f8d 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -107,7 +107,17 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface protected function optimizeVariables($node, $env) { if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) { - $this->prependedNodes[0][] = new Twig_Node_SetTemp($node->getAttribute('name'), $node->getLine()); + // only add it if it's not already there + $exists = false; + foreach ($this->prependedNodes[0] as $n) { + if ($n->getAttribute('name') === $node->getAttribute('name')) { + $exists = true; + break; + } + } + if (!$exists) { + $this->prependedNodes[0][] = new Twig_Node_SetTemp($node->getAttribute('name'), $node->getLine()); + } return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getLine()); }