Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Optimizing multiple calls to the same template

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 69

PostPosted: Sun Feb 28, 2010 4:49 pm    Post subject: Optimizing multiple calls to the same template Reply with quote

Consider the following sample code:
Code:
{foreach from=$item_list key=key item=item}{include file='item.tpl'}{/foreach}


As far as I can see, each time item.tpl is included Smarty 2 (and probably Smarty 3) checks whether template should be recompiled. This involves multiple calls to file_exists and filemtime functions. Filemtime function is cached, however file_exists function is not cached. If I have 100 items in $item_list, I'll get 100 calls to filesystem function.

My question is: did you optimize Smarty for multiple calls to the same template like in the sample above?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4173
Location: Hamburg / Germany

PostPosted: Sun Feb 28, 2010 5:41 pm    Post subject: Reply with quote

In Smarty you can use
$smarty->merge_compiled_includes = true;

It does merge the compiled code of included templates into the compiled output of the main template to improve performance.

In this case each included template gets checked once.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7036
Location: Lincoln Nebraska, USA

PostPosted: Sun Feb 28, 2010 7:25 pm    Post subject: Reply with quote

Merging compiled templates won't stop the template checks. You can set compile_check = false to skip the checks. Just be aware if you change any templates, they won't recompile.
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 105

PostPosted: Fri Mar 12, 2010 4:11 pm    Post subject: Reply with quote

mohrt wrote:
Merging compiled templates won't stop the template checks

Why not? Unless the template filename is a variable I don't see a reason for template checks apart from the initial check when compiling. Can you explain to me why included templates are checked when merge_compiled_includes = true?

And in the example of the OP, will item.tpl be checked 1000 times at run time for 1000-item array when merge_compiled_includes = true?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4173
Location: Hamburg / Germany

PostPosted: Fri Mar 12, 2010 6:42 pm    Post subject: Reply with quote

With merge_compiled_includes = true each included template is checked just once.
Back to top
View user's profile Send private message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 105

PostPosted: Sun Mar 14, 2010 1:02 pm    Post subject: Reply with quote

U.Tews wrote:
With merge_compiled_includes = true each included template is checked just once.

Ok, so as I understand it there is not much speed improvement with merged compiles unless compile_check is off?

1. When compile_check is on and merge_compiled_includes is on, then smarty has to stat each included file.

2. When compile_check is on and merge_compiled_includes is off, then smarty has to stat and read each included file.

Template files are ususally very small so I suppose there would be not much difference between these 2 cases. Am I right?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4173
Location: Hamburg / Germany

PostPosted: Sun Mar 14, 2010 10:56 pm    Post subject: Reply with quote

Merging the compiled templates does have always better performance because only the merged compiled template has to be loaded and is processed as one template. If compile_check is on each inluded template is checked just once independed of the number of invocation.

If merge_compiled_includes is off each invocation of the {include} tag will result in loading and individual processing of the corresponding compiled template. If compile_check is on the check is performed on each invocation.

The above is true when caching is off or as long as the called page is not already in cache. If a page is in cache there will be no difference in performace dependend on the status of merge_compiled_includes.
Back to top
View user's profile Send private message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 105

PostPosted: Mon Mar 15, 2010 8:12 am    Post subject: Reply with quote

OK, thanks for the explanation!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP