From e7347160cbc73f3e7a8cb83a5ce3498082d05812 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 16 Oct 2011 22:18:02 +0200 Subject: [PATCH] added an optimization when the same temporary variable would have been created twice --- lib/Twig/NodeVisitor/Optimizer.php | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-) 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()); } -- 1.7.2.5