Just like in this topic, I have a performance issue in dev mode when adding a twig "render" tag in my app (related documentation: Embedding controllers).
Without this render tag, my pages are generated in less than 70 ms.
With the render tag, it's at least 170 ms.
And each render tag added in the app increases the page generation by 100 ms (which is A LOT : why does a normal page run in 60 ms and a render tag in 100 ms?).
I may need 4 or 5 of them on every page of my app, so that would mean at least 500 ms for each page in dev mode.
I totally understand that there is no problem in prod mode, but it's clearly not comfortable in development.
So, does somebody know any way to get rid of any useless calls, logs or code while using "render" tag in dev mode?
I have explained it just 10 hours ago. Long story short: migrate to Twig extensions.
One of my favorite features in symfony is the render tag, embedding controller calls. The profiler adds a lot overhead to every controller calls though, not only speed but uses a lot of memory. You have a few options to speed it up.
The profiler writes every data into a sqlite database by default. IIRC sqlite doesn't allow parallel inserts, so every request have to wait for their turn to access the db to flush data collectors. You can use your development db (mysql or whatever you use) to persist profiler data. A year ago I gained a lot with this in terms of speed.
You can also disable the profiler for sub requests, or only use the profiler when an exception happens. See the framework config reference for the full details.
# config_dev.yaml
framework:
profiler:
only_exceptions: false
only_master_requests: false
dsn: sqlite:%kernel.cache_dir%/profiler.db
You can move your controller logic to a service and reference it as twig global variable and then include the template rendered by the controller.
See https://stackoverflow.com/a/13245994/982075 for instructions.
The choice depends on your application. I believe that the most practical ways are:
1) Use the render
tag for heavy rendered templates and use hinclude library to load them in an asynchronous way. This is very helpful when each template to be rendered is "slow" by itself (e.g. many db connections, large texts, etc.).
2) Do as proposed by m2mdas. This is a very fast solution for common cases.
I’d also follow Elnur’s suggestion with twig extensions. An alternative would be using Sonata Block Bundle: http://sonata-project.org/bundles/block/master/doc/index.html . The overhead of a subrequest with Sonata Block is about 7ms afair.