Browse code

optimized parent template creation when the template does not use dynamic inheritance

Fabien Potencier authored on 30/03/2012 19:05:14
Showing 5 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 * 1.6.4 (2012-XX-XX)
2 2
 
3
- * n/a
3
+ * optimized parent template creation when the template does not use dynamic inheritance
4 4
 
5 5
 * 1.6.3 (2012-03-22)
6 6
 
... ...
@@ -96,7 +96,12 @@ class Twig_Node_Module extends Twig_Node
96 96
         $compiler->subcompile($this->getNode('body'));
97 97
 
98 98
         if (null !== $this->getNode('parent')) {
99
-            $compiler->write("\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
99
+            if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
100
+                $compiler->write("\$this->parent");
101
+            } else {
102
+                $compiler->write("\$this->getParent(\$context)");
103
+            }
104
+            $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
100 105
         }
101 106
     }
102 107
 
... ...
@@ -121,6 +126,17 @@ class Twig_Node_Module extends Twig_Node
121 126
             ->write("parent::__construct(\$env);\n\n")
122 127
         ;
123 128
 
129
+        // parent
130
+        if (null === $this->getNode('parent')) {
131
+            $compiler->write("\$this->parent = false;\n\n");
132
+        } elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
133
+            $compiler
134
+                ->write("\$this->parent = \$this->env->loadTemplate(")
135
+                ->subcompile($this->getNode('parent'))
136
+                ->raw(");\n\n")
137
+            ;
138
+        }
139
+
124 140
         $countTraits = count($this->getNode('traits'));
125 141
         if ($countTraits) {
126 142
             // traits
... ...
@@ -166,9 +182,6 @@ class Twig_Node_Module extends Twig_Node
166 182
             $compiler
167 183
                 ->outdent()
168 184
                 ->write(");\n\n")
169
-            ;
170
-
171
-            $compiler
172 185
                 ->write("\$this->blocks = array_merge(\n")
173 186
                 ->indent()
174 187
                 ->write("\$this->traits,\n")
... ...
@@ -300,7 +313,7 @@ class Twig_Node_Module extends Twig_Node
300 313
         ;
301 314
     }
302 315
 
303
-    public function compileDebugInfo(Twig_Compiler $compiler)
316
+    protected function compileDebugInfo(Twig_Compiler $compiler)
304 317
     {
305 318
         $compiler
306 319
             ->write("public function getDebugInfo()\n", "{\n")
... ...
@@ -311,7 +324,7 @@ class Twig_Node_Module extends Twig_Node
311 324
         ;
312 325
     }
313 326
 
314
-    public function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
327
+    protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
315 328
     {
316 329
         if ($node instanceof Twig_Node_Expression_Constant) {
317 330
             $compiler
... ...
@@ -20,6 +20,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
20 20
 {
21 21
     static protected $cache = array();
22 22
 
23
+    protected $parent;
23 24
     protected $parents;
24 25
     protected $env;
25 26
     protected $blocks;
... ...
@@ -62,6 +63,10 @@ abstract class Twig_Template implements Twig_TemplateInterface
62 63
      */
63 64
     public function getParent(array $context)
64 65
     {
66
+        if (null !== $this->parent) {
67
+            return $this->parent;
68
+        }
69
+
65 70
         $parent = $this->doGetParent($context);
66 71
         if (false === $parent) {
67 72
             return false;
... ...
@@ -107,7 +107,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
107 107
     protected function doDisplay(array \$context, array \$blocks = array())
108 108
     {
109 109
         \$context["macro"] = \$this->env->loadTemplate("foo.twig");
110
-        \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
110
+        \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
111 111
     }
112 112
 
113 113
     public function getTemplateName()
... ...
@@ -118,7 +118,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
118 118
     protected function doDisplay(array \$context, array \$blocks = array())
119 119
     {
120 120
         \$this->checkSecurity();
121
-        \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
121
+        \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
122 122
     }
123 123
 
124 124
     protected function checkSecurity() {