jonathangeiger / kohana-twig

A Kohana 3.0 module for integrating Twig
49 stars 23 forks source link

Use this to make twig faster with kohana #9

Closed snrp closed 13 years ago

snrp commented 14 years ago

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

<div id="kohana-profiler">
<?php echo View::factory('profiler/stats') ?>
</div>

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:

<?php defined('SYSPATH') or die('No direct script access.');

    $folder = realpath(dirname(__FILE__).'/twig');

If you did NOT move the Twig folder from inside vendor into classes use this:

<?php defined('SYSPATH') or die('No direct script access.');

    $folder = realpath(dirname(__FILE__).'/../vendor/Twig/lib/Twig');

Then underneath:

require_once "$folder/LexerInterface.php" ;
require_once "$folder/ParserInterface.php" ;
require_once "$folder/CompilerInterface.php" ;
require_once "$folder/LoaderInterface.php" ;
require_once "$folder/ExtensionInterface.php" ;
require_once "$folder/TemplateInterface.php" ;
require_once "$folder/NodeInterface.php" ;

require_once "$folder/Extension.php" ;
require_once "$folder/Extension/Core.php" ;

require_once "$folder/Template.php" ;

require_once "$folder/Node.php" ;
require_once "$folder/Node/Expression.php" ;
require_once "$folder/Node/Expression/GetAttr.php" ;

require_once "$folder/Lexer.php" ;
require_once "$folder/Parser.php" ;
require_once "$folder/Compiler.php" ;

require_once "$folder/Environment.php" ;

class Twig extends Kohana_Twig {}
snrp commented 14 years ago

lol, I just discovered on the forum that you turn on the internal cache wich caches file paths, and all this wasnt even necesary... :P

ThePixelDeveloper commented 13 years ago

Closing as it's no longer an issue.