Categories > TinyButStrong general >

Speed up template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: RwD
Date: 2008-05-07
Time: 21:39

Speed up template

I have a rather large template file (25Kb) which has 336 tbs tags inside. When I merge the template with data the following happens:
- First the template loads 4 subtemplates adding some more tbs tags which I won't mention right now
- Second I merge 57 translation fields ([_; txt=something])
- Then I merge three different blocks taking care of the remaining 250+ tags.

This all takes about 4,5 seconds for 110 rows of data (merged into the blocks) in total. This is the optimal order to merge everything, because it took 6,6 seconds before I noticed the merging was pretty slow and I tried all other combinations for speed.

My question now is how I can speed up the process? I can show my template, but since it is quite a lot to look into perhaps I need some general pointers or some one on one help rather then in the forum...
By: TomH
Date: 2008-05-08
Time: 03:21

Re: Speed up template

An observation... 110 rows is really not a lot of data by my experience, so if this is taking more than 0,05 secs to render that many rows then there's a good place to start.
(1)Are there subqueries involved maybe?
(2)Does your query hit a LOT more rows than the 110 rows being displayed?
Try placing the db query by itself in a separate template for testing the time needed to complete only the query and the rendering of the output of the blocks so you are sure where the delay is coming from.
If this query is the big time consumer, and you are stuck with this query. then consider caching that piece of the result -adjust the MaxAge parameter to be representative of the expected frequency of changes to the db values.

If that seems not to be the source of the prob then all I can recommend is to deconstruct the major components of the page in separate templates for testing.

Sorry I can't be more help,
TomH
By: RwD
Date: 2008-05-08
Time: 17:35

Re: Speed up template

I actually don't send any queries to tbs because I don't think that is the job of a template engine. Instead tbs gets arrays and objects with data. tbs can handle both.

The thing is that if I just simply display (using print_r or some other php function) EVERYTHING that I got from the database I am displaying more then the slow template does and it is shown instantly. I already got to the point where I know that merging the array or objects is what is going slow. I just don't know why.

I suppose I can serialize the arrays and send you an example with some of the actual data?
By: RwD
Date: 2008-05-08
Time: 18:11

Re: Speed up template

Hmm, I isolated a part that, when taken out, makes the page load in 0.2sec. When I take everything else out except this part the page needs 2.9sec.

The time for everything in is 4.5sec

The two don't add up to the total and I dont see why this would make such a big difference for tbs when finding locator tags in the template... I'll have to investigate some more...
By: TomH
Date: 2008-05-08
Time: 18:31

Re: Speed up template

(1) Show us the php and template for the 2.9 second chunk

(2) I'd like to learn more about your comment
I actually don't send any queries to tbs because I don't think that is the job of a template engine. Instead tbs gets arrays and objects with data. tbs can handle both.
in what file do you perform the db query? and in what file do you create the array/object that feeds the TBS block?
By: RwD
Date: 2008-05-08
Time: 21:16

Re: Speed up template

I figured out what the problem is (and solved it). Inside one tbs block a certain function was called about 20 times. Though the sql data this function retrieved was cached because the result would be the same about every time the function was still called 110*20=2200 times. And that count is low compared to the actual number (about 3500).

So I changed these calls to be ran onload instead of onshow so the aren't replicated for every row....

I hope my explaination makes sense...
By: RwD
Date: 2008-05-08
Time: 21:23

Re: Speed up template

(1): nope, I solved it, see other reply...

(2): I have everything object oriented. So I simply have a database object that handles every query. So in my code I have a certain object that for example retrieves the names of all people in some organisation (using the db object for database interaction). The list is given to tbs as an array (which tbs can handle as well)

This way everything is seperated. I could -in theory- also start using another template engine if I changed the templates as well. But I would have to make minimal changes to my php code because I also have a template-engine-adapter class wrapped around tbs...

But this is all waaay off-topic
By: Skrol29
Date: 2008-05-08
Time: 21:26

Re: Speed up template

Hi RwD,

Do you have a lot of conditional display in your templates or in your TBS tags ?
Are your tags stored into objects, or arrats, or are they mostly direct Global variables ?
By: RwD
Date: 2008-05-08
Time: 21:36

Re: Speed up template

yep, enough conditional display and everything is in arrays or objects.

It took a slow customers server about 16seconds to generate the page. I have that down to 6 due to the above fix. Perhaps you'll have some more pointers. Or should I be looking at the things you mentioned (which I cannot change really)
By: RwD
Date: 2008-05-10
Time: 08:12

Re: Speed up template

I had the idea to also merge the var fields before the data blocks are done. The slow production server shows the entire page within 5sec now. Before it was 16sec. So I've made hughe improvements on efficiency of the template!

I hope this helps someone as well some day ;)