Browse code

fixed performance when compiling large files

Fabien Potencier authored on 01/04/2012 08:03:24
Showing 2 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.6.4 (2012-XX-XX)
2 2
 
3
+ * fixed performance when compiling large files
3 4
  * optimized parent template creation when the template does not use dynamic inheritance
4 5
 
5 6
 * 1.6.3 (2012-03-22)
... ...
@@ -23,6 +23,8 @@ class Twig_Compiler implements Twig_CompilerInterface
23 23
     protected $indentation;
24 24
     protected $env;
25 25
     protected $debugInfo;
26
+    protected $sourceOffset;
27
+    protected $sourceLine;
26 28
 
27 29
     /**
28 30
      * Constructor.
... ...
@@ -67,6 +69,8 @@ class Twig_Compiler implements Twig_CompilerInterface
67 69
     {
68 70
         $this->lastLine = null;
69 71
         $this->source = '';
72
+        $this->sourceOffset = 0;
73
+        $this->sourceLine = 0;
70 74
         $this->indentation = $indentation;
71 75
 
72 76
         $node->compile($this);
... ...
@@ -188,7 +192,9 @@ class Twig_Compiler implements Twig_CompilerInterface
188 192
     public function addDebugInfo(Twig_NodeInterface $node)
189 193
     {
190 194
         if ($node->getLine() != $this->lastLine) {
191
-            $this->debugInfo[substr_count($this->source, "\n")] = $node->getLine();
195
+            $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
196
+            $this->sourceOffset = strlen($this->source);
197
+            $this->debugInfo[$this->sourceLine] = $node->getLine();
192 198
 
193 199
             $this->lastLine = $node->getLine();
194 200
             $this->write("// line {$node->getLine()}\n");