How to speed up template rendering in Symfony2?

performance, php, symfony, twig

Solution

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.

Problem

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. 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. 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.

Original source