From: Fabien Potencier Date: Thu, 6 Oct 2011 11:13:28 +0000 (+0200) Subject: made an optimization by removing array_merge usage X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=bd5687ee10c5e410ce16e3c19753abd7620274fc;p=web%2Fkonrad%2Ftwig.git made an optimization by removing array_merge usage --- diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 7c12589..bf7d4c1 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -91,7 +91,6 @@ class Twig_Node_Module extends Twig_Node protected function compileDisplayBody(Twig_Compiler $compiler) { - $compiler->write("\$context = array_merge(\$this->env->getGlobals(), \$context);\n\n"); $compiler->subcompile($this->getNode('body')); if (null !== $this->getNode('parent')) { diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index cd1edcd..fa502fa 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -224,6 +224,14 @@ abstract class Twig_Template implements Twig_TemplateInterface */ public function display(array $context, array $blocks = array()) { + // we don't use array_merge as the context being generally + // bigger than globals, this code is faster. + foreach ($this->env->getGlobals() as $key => $value) { + if (!array_key_exists($key, $context)) { + $context[$key] = $value; + } + } + try { $this->doDisplay($context, $blocks); } catch (Twig_Error $e) { diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 05ee4f8..da7f942 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -76,8 +76,6 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { - \$context = array_merge(\$this->env->getGlobals(), \$context); - echo "foo"; } @@ -113,8 +111,6 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { - \$context = array_merge(\$this->env->getGlobals(), \$context); - \$context["macro"] = \$this->env->loadTemplate("foo.twig"); \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } @@ -154,8 +150,6 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { - \$context = array_merge(\$this->env->getGlobals(), \$context); - \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 36ad98e..eb76cf2 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -75,8 +75,6 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { \$this->checkSecurity(); - \$context = array_merge(\$this->env->getGlobals(), \$context); - echo "foo"; } @@ -124,8 +122,6 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { - \$context = array_merge(\$this->env->getGlobals(), \$context); - \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); }