Repository navigation
Make rendering large templates linear again - #210
Merged
Merged
Conversation
map_tree() replaced the body element with the rendered tree, and lxml takes quadratic time to move a big subtree between documents. Detaching the old body and attaching a table-heavy new one were both quadratic, so a few thousand variables cost seconds and tens of thousands minutes. Splice the rendered body into the document XML and parse it once, then point the document and its part at the new root. Fixes #74
This was referenced Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#74 reports 27 minutes for a template with about 12,000 variables. Profiling showed most of the render time in
map_tree(), which does a singleroot.replace(body, tree). lxml has to move a large subtree from one document to another for that, and it takes quadratic time: detaching the template's old body costs 0.2s, 0.8s and 3.1s at 3k, 6k and 12k variables, and attaching a body with one big table is just as bad.map_tree()now serializes the document, splices in the rendered body and parses it once with python-docx's parser, then points both the Document and its part at the new root. Nothing moves between documents anymore. If the body markers can't be found it falls back to the old replace.Render time, same machine, one run each (Python 3.14, Windows):
The curve is linear now; what's left is mostly Jinja compiling the template. I couldn't reproduce the full 27 minutes with generated templates, so the reporter's file may have something else going on too.
All 41 example outputs from
tests/runtests.pyproduce byte-identicalword/document.xmlbefore and after. There's no timing test since that would be flaky in CI; the new test checks the document and its part share the rendered root and that python-docx reads see the rendered content.Fixes #74