Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up template rendering in Symfony2?

I have a symfony2 application that renders about 6000 database entities.

I optimized the ORM (propel) queries and the database requests are now very fast. What remains is the symfony controller and the twig template rendering engine.

Is there a way to speed up rendering (e.g. switching to php templates?) Can I get more detailed profiling information than from the symfony profiler?

Here's an excerpt from the profiler data.

symfony profiler screenshot

Edit: I profiled my code using xDebug and found that generating the objects is very expensive. Creating thousands of PHP objects from database rows in PropelObjectFormater->getAllObjectsFromRow takes most of the time.

profiler_run_excerpt

The image shows the branch that takes up 95% of the computing time, which is ~2.8 seconds. The database retrieval takes ~0.5 seconds, the rendering ~1 second and formatting takes the most time with ~1.5 seconds.

I'm not sure what impact the length of the file containing the PHP class has, but Propel generates a lot of code (my most complex entity base class has almost 10k lines of code) so this might slow down object creation as well.

I think using an array formatter (thus bypassing the object creation step) would be a solution, but that somewhat defeats the purpose of the ORM.

like image 380
cw' Avatar asked Dec 05 '13 13:12

cw'


2 Answers

When in production mode, Symfony2's Twig template rendering is actually done using cached templates, meaning that it is a PHP template; a Twig template compiled into pure PHP code. In dev mode, it of course doesn't cache the templates like that, but compiles it each time. Read more about it here, in the docs.

It will take PHP some time to compile a page with that much data, even when the template is just the normal PHP-HTML infusion code that tends to perform fastest. - You may want to look into normal HTTP caching, like Varnish, if this page is something that doesn't have to be rendered from the database each time.

As for the profiling. Something like XDebug should get you more detailed profiling info than what is built into Symfony2.

like image 174
Atli Avatar answered Sep 22 '22 03:09

Atli


6K of iterations is a lot of work.

Idea #1:

Can you implement data pagination? That seems highly reasonable...

Idea #2 (thick client):

Implement some JavaScript templating mechanism. Your controller should then return JSON in order for JS to go though it and render it completely.

Even in this situation, relying of browser's speed to do 6K loops fast is lot to ask. You will need to implement pauses in between (like, after every 150th iteration) so the browser doesn't go into non-responsive mode...

like image 30
Jovan Perovic Avatar answered Sep 21 '22 03:09

Jovan Perovic