Given following template-
{% for x in blah %}
{% for y in x %}
{% bunch o nodes %{
{% endfor %}
{% endfor %}
The way the template rendering subsystem works, each 'bunch o nodes' is added onto a list w/in 'for y', then collapsed into a string, and returned to 'for x'; which builds up a list, collapses it down into into a string, and returns it. Problems follow:
* wind up continually generating larger and larger strings as data is added on, percolating it's way up to the actual template.render. This isn't totally friendly to performance (specifically has a knack for forcing the alloc pool to expand repeatedly).
* Data is already ready to be sent, but it's locked away in the template- basically, template rendering tries to build a final string of all contents- until that's finished nothing is sent
* Said "final string of all content" means that django winds up being more memory hungry, pretty much without any reason.
A backwards compatible solution is the introduction of an iter_render method to Node, that yields finalized chunks as they're available- this doesn't mean yielding each char as it's available, means (for a ForNode? for example), yielding each chunk of data for iteration; via this approach, Template.iter_render has chunks of data percolate up as they're available, instead of just one big ass string at the end.
Attached is a patch implementing iter_render, and converting existing tags/templates/involved codepaths over to iteration where sane; some node rendering still collapse internally (filter expressions primarily, although will be targeting those later). This patch includes the patch from ticket #4523 (isinstance usage there gets ugly when a lot of items are passing through it).
Final other change worth noting- changes the base http so that it's no longer flushing after every single write during content dumping. Reason behind this should be fairly obvious- fine if you're writing a single item, sucks if you're writing a few hundred.
One point of potential contention is the change to Node;
class Node(object):
# misc fluff
def render(self, context):
return ''.join(self.iter_render(context))
def iter_render(self.context):
return (self.render(context),)
In other words, the default Node definition for rendering will trigger a RuntimeError? on execution, going recursive. The reason for this choice is backwards compatibility- all custom Node derivatives at this point define render, thus iter_render has to defer to it if iter_render is default. Lack of overriding can probably be detected (and explode up front) via a metaclass trick, although open to suggestions.
The cons are pretty well covered above; Pros are
* instant results; as long as you don't have any tags/nodes that require collapsing their subtree down into a single string, data is sent out immediately. Rather nice if you have a slow page- you start get chunks of the page immediately, rather then having to wait for the full 10s/whatever time for the page to render.
* Far less abusive on memory; usual 'spanish inquisition' test (term coined by mtreddinick, but it works), reduction from 84m to 71m for usage at the end of rendering. What I find rather interesting about that reduction is that the resultant page is 6.5 mb; the extra 7mb I'm actually not sure where the reduction comes from (suspect there is some copy idiocy somewhere forcing a new string)- alternatively, may just be intermediate data hanging around, since I've been working with this code in one form or another for 3 months now, and still haven't figured out the 'why' for that diff.
* Surprisingly, at least for the test cases I have locally, I'm not seeing a noticable hit on small pages, nothing above background noise at the very least- larger pages, seeing a mild speed up (which was expected- 4-5%).
Patch is attached, bzr branch should it bitrot few months down the line, http://pkgcore.org/~ferringb/django/iter-render . Will be bugging the ml about this ticket also, since I'd expect folks will want to chime in; finally, docs aren't updated in this patch- didn't see the point in doing it till I was sure folks would go for the change.