softmoth / raku-Template-Mustache

Raku library for the Mustache template format
https://modules.raku.org/dist/Template::Mustache:cpan:SOFTMOTH
Artistic License 2.0
21 stars 19 forks source link

Speed problems #35

Closed antoniogamiz closed 4 years ago

antoniogamiz commented 4 years ago

Hi, first of all, thanks for your work @softmoth. After updating documentable to use Mustache templates, I have noticed a huge increase in the execution time of documentable. In the previous versions (without templates), generate the documentation would take no more than 6-10 minutes. After using templates, this is the time it takes:

[=======================================================================]100.00%
Generate source files has taken 785.1736876 seconds 
Generating Kind::Syntax files πŸ‘‡ ...
[=======================================================================]100.00%
Generating Kind::Routine files πŸ‘‡ ...
[=======================================================================]100.00%
Generating per kind files has taken 957.6732023 seconds 
Generating indexes...
Generating index files has taken 35.87684523 seconds 
Writing search file...
Generating entries
Writing all generated files πŸ™Œ ...
[#######################################################################]100.00%
Writing generated files has taken 1.99796244 seconds 
Whole process has taken 2067.5031522 seconds

Around 30 minutes. This problem was also found by @finanalyst when he tried to remake the Pod::To::HTML module.

I don't know if this is the usual time any other implementation of Mustache (in a different language) would take to do the same. Any ideas on how to solve this?

softmoth commented 4 years ago

Thank you very much for opening this issue.

I've pushed some changes in version 1.2.0 which may help, caching parsed templates which were not being cached before. If you are re-using the same template files over and over (as I suspect you are), this should be much faster now. Just make sure that you keep a single Template::Mustache instance, as the cache is stored as a member and not shared between instances.

my $stache = Template::Mustache.new: from<path/to/templates>;

for @documentables -> $doc {
    # This will look up the 'document.mustache' template in the cache, and
    # any of the templates it references
    $stache.render: '{{< document }}', { 'title' => $doc.title, 'body' => $doc.body };
}

That being said, I am sure the Grammar for Template::Mustache is not efficient. It probably backtracks a lot more than it should. I have some ideas on fixing that, but haven't gotten to them. Hopefully the caching will be sufficient for your needs now. Please let me know how it goes!

Tim

antoniogamiz commented 4 years ago

Where could I see those changes? I don't see new commits in this repo. Thanks for the fast reply!

softmoth commented 4 years ago

Yes, it is here on this repo (now renamed to raku-Template-Mustache on Github). They were committed a few days ago now: https://github.com/softmoth/raku-Template-Mustache/commit/4811aadd403403c182245674f1d8b044594f075a

The 1.2.0 release on CPAN has it (current version is 1.2.1, with some logging features refactored).

antoniogamiz commented 4 years ago

Oh here is why I have not seen the speed changes:

https://github.com/Raku/Pod-To-HTML/blob/00d1130391a38d5a0b1b13a15738bfa889726c54/lib/Pod/To/HTML.pm#L273-L280 (preview code is not working for some reason) I suppose literals are not being cached, or cannot be cached?

antoniogamiz commented 4 years ago

Mm, after taking a look at the code it looks like that literals are indeed cached.

softmoth commented 4 years ago

Thank you, Antonio.

Yes, explicitly marked :literals were already being cached, after finanalyst added caching. Recent changes expanded caching to all templates, literals and non-literals alike. The :literal hack you pointed to is no longer needed (we can just point :from at the templates directory and let Template::Mustache find the partials); but having it in there won't slow things down noticeably, I guess.

I'm curious what the speed difference is with the updated to Pod-To-HTML. I honestly haven't put much effort into benchmarking and optimizing. I would like to do that eventually, but have other things that are a higher priority. If the speed with caching is still a major hurdle for you, I can change my priorities.

antoniogamiz commented 4 years ago

I think after these changes (https://github.com/Raku/Documentable/issues/117) it will be enough :-). As always, thanks for your help!