| View previous topic :: View next topic |
| Author |
Message |
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 69
|
Posted: Sun Feb 28, 2010 4:49 pm Post subject: Optimizing multiple calls to the same template |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4173 Location: Hamburg / Germany
|
Posted: Sun Feb 28, 2010 5:41 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7036 Location: Lincoln Nebraska, USA
|
Posted: Sun Feb 28, 2010 7:25 pm Post subject: |
|
|
| 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 |
|
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 105
|
Posted: Fri Mar 12, 2010 4:11 pm Post subject: |
|
|
| 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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4173 Location: Hamburg / Germany
|
Posted: Fri Mar 12, 2010 6:42 pm Post subject: |
|
|
| With merge_compiled_includes = true each included template is checked just once. |
|
| Back to top |
|
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 105
|
Posted: Sun Mar 14, 2010 1:02 pm Post subject: |
|
|
| 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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4173 Location: Hamburg / Germany
|
Posted: Sun Mar 14, 2010 10:56 pm Post subject: |
|
|
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 |
|
Lemon Juice Smarty Pro
Joined: 24 May 2006 Posts: 105
|
Posted: Mon Mar 15, 2010 8:12 am Post subject: |
|
|
| OK, thanks for the explanation! |
|
| Back to top |
|
|