Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Function optimize?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Tom Sommer
Administrator


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Sat Apr 26, 2003 9:53 pm    Post subject: Function optimize? Reply with quote

I'm worried about performance,

I have a function which generates a headline

Called like so
Code:
{headline name='News'}


Function looks like this

Code:
function smarty_function_headline($params, &$smarty)
{
   $smarty->assign('headline_title', $params['name']);
   return $smarty->fetch('headline.html');

}


My worry is that when I use this in a loop, the calling of all those fetch() is not good.... so is there a better way of doing this?
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 10:38 pm    Post subject: Reply with quote

if the name is the only dynamic element in your template, that depends on the loop, you can save the return-value of fetch() in your function, then it has to be done only once.

sth like:
Code:

<?php

function smarty_function_headline($params, &$smarty) {
    static $html;

    if (!isset($html)) {
        $smarty->assign('name', '###name###');
        $html = $smarty->fetch('capture/headline.tpl');
    }

    return str_replace('###name###', $params['name'], $html);
}
?>


HTH
messju


Last edited by messju on Sat Apr 26, 2003 10:46 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Tom Sommer
Administrator


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Sat Apr 26, 2003 10:43 pm    Post subject: Reply with quote

OMG, that lowered the loadtime so much... Thank you, never thought of that Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 27, 2003 4:10 am    Post subject: Reply with quote

edit:

I was wondering why smarty doesn't have a generalized memory cache in front of the persistent output and compile caches. AFAIK, every fetch requires at minimum a disk load of a persistently cached file. I would think that keeping items in a non-persistent cache like that would speed up not only Toms case above, but also speed up all multiple calls to a given template. Further, it could probably be serialized across sessions and eventually (using SRM?) across users.

2c
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Apr 27, 2003 2:51 pm    Post subject: Reply with quote

toms case would have been optimized automatically, if caching was enabled: if the templates' output comes from the cache, all included templates' output is also in the cached-file. so there is only one fread() not matter how many {include} statements (even inside loops) you have had in the templates source.

caching parsed php-sourcecode (in our case compiled templates) in shared memory is the domain of php-accelerator. don't bother to build a better solution in php, you cannot win.

caching cached templates' output in shared memory can be a dangerous thing. if you use cache_ids heavily, your shared memory will be full very soon. your application would blow then. or you will have to swap from and to shared memory. thats will be tremendously slower than, letting your operating system do such things. AFAIK linux for example uses all unused memory as disk-cache. so IMHO there's no need to rebuild such a thing at php-level either.

caching the output of a fetch() call has not to be implemented. thats exactly was Smarty's caching does.

caching the output of Smarty's compiler (the compiled templates) cannot be done more efficiently in php as in simply installing phpa.

just my thoughts and 2c Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 27, 2003 3:18 pm    Post subject: Reply with quote

I see what your saying. Good points. I guess it would only be useful if there was a shared memory manager and then only for compilation. Since there is no shared memory... Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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