Twig inside kohana is much slower due to the way kohana finds files (when it autoloads classes or whatever).
I tested Twig standalone in the document root and it is very fast. Inside kohana it takes as much time to render a small template as the whole framework itself takes to render the same thing with views with php in them + alittle more (and this is after the template has been cached !). I first thought it wasnt caching properly, but no, that wasnt it, its the file loading.
I was ready to give up twig in favor of mustache which is lightning fast (again with some tweaks, from the original kostache)
But after some more testing I found the problem (and the solutions)
Itried 2 variants:
1) because twig naming convention and folder structure is same as kohana (class Twig_Lexer is in
modules/twig/vendor/Twig/lib/Twig/Lexer.php
(Twig/Lexer.php))
you can move the whole
lib/Twig folder
inside classes (that file would now be at:
modues/twig/classes/Twig/Lexer.php
Now twig autoloader is no longer needed (in init.php).
This halved the time it took for loading files. (I tested it, for ex, the time it took from passing execution from ... = new Twig_lexer; to the constructor inside Lexer.php)
2) I found an even faster way:
I Changed the profiler call inside kohana::find_file to print also the file name it loads, then printed the profiling view at the bottom of index.php
Twig inside kohana is much slower due to the way kohana finds files (when it autoloads classes or whatever).
I tested Twig standalone in the document root and it is very fast. Inside kohana it takes as much time to render a small template as the whole framework itself takes to render the same thing with views with php in them + alittle more (and this is after the template has been cached !). I first thought it wasnt caching properly, but no, that wasnt it, its the file loading.
I was ready to give up twig in favor of mustache which is lightning fast (again with some tweaks, from the original kostache)
But after some more testing I found the problem (and the solutions)
Itried 2 variants:
1) because twig naming convention and folder structure is same as kohana (class Twig_Lexer is in modules/twig/vendor/Twig/lib/Twig/Lexer.php (Twig/Lexer.php)) you can move the whole lib/Twig folder inside classes (that file would now be at: modues/twig/classes/Twig/Lexer.php
Now twig autoloader is no longer needed (in init.php).
This halved the time it took for loading files. (I tested it, for ex, the time it took from passing execution from ... = new Twig_lexer; to the constructor inside Lexer.php)
2) I found an even faster way:
I Changed the profiler call inside kohana::find_file to print also the file name it loads, then printed the profiling view at the bottom of index.php
Now I only did this for cached templates, when twig rebuilds them there are probably more.
In any case, here is what I added to /modules/twig/classes/twig.php:
If you DID move the Twig folder from inside vendor into classes/twig (notice small t in twig) use this:
If you did NOT move the Twig folder from inside vendor into classes use this:
Then underneath: