Repository navigation
Speed up new_subdoc() for in-memory documents - #202
Merged
Merged
Conversation
Copy only the body XML of a Document or rendered DocxTemplate instead of saving and reopening the whole package, and read the style mapping straight from the styles XML instead of building a Style object for every style. Documents with custom properties, or templates with pending media replacements, still get a full copy. Fixes #201
This was referenced Nov 23, 2026
Compare all parts and relationships of the Document passed to new_subdoc(), not just its body, for a built document and four subdoc templates with numbering, footnotes, OLE objects and pandoc output.
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.
What this changes
Inserting an in-memory
Documentor a renderedDocxTemplategoes from about 23ms to about 4ms per subdoc. Two changes:The copy that keeps the caller's object untouched used to save it to a
BytesIOand reopen it. Building the subdoc only changes the body, though, and docxcompose just reads the styles, numbering and images. So now it deep-copies the body XML and wraps it in a newDocumentthat shares the original parts. A document with custom properties still gets the full copy, because dissolving those fields also touches its headers and footers. So does aDocxTemplatewith pendingreplace_*calls, since those only apply on save. A renderedDocxTemplateis used as rendered, without reloading it from the template file.docxcompose rebuilds the style id mapping for every subdoc by building a python-docx
Styleobject for every style of both documents, which was most of what was left.SubdocComposernow readsw:styleIdandw:namestraight from the XML with the sameBabelFishname conversion and order, so the mapping is the same. It still gets rebuilt per subdoc, since inserting one can add styles to the template.One thing that's different from before: parts the template links to instead of copying, like SmartArt diagram data, are now shared with the
Documentyou passed in rather than with a private copy. Nothing changes them during insertion, but if you edit thatDocument's diagrams or charts afternew_subdoc()and before saving, the edit shows up in the output. That's an unusual thing to do, so I think the speedup is worth it.Fixes #201
How I tested it
New unit tests check that every part and relationship of the passed
Document(styles, numbering, images, footnotes and the rest) is unchanged after inserting it twice, for a built document and four subdoc templates fromtests/templates, that images still work, that one with custom properties still works and stays unchanged, and that the style mapping matchesdoc.styles. I also compared the mapping with docxcompose's on all 43 templates intests/templates, and the feature script outputs match main except for zip timestamps inside one embedded file. All unit tests pass locally.Benchmark against main, medians of 3 rounds of 7 runs each:
DocumentsThe subdoc rows include building or rendering the 30 subdocs, which is most of what's left. Just the
new_subdoc()call goes from 22.8ms to 4.0ms for an in-memoryDocumentand from 14.3ms to 12.3ms for a stream.tests/unitCHANGES.rstupdated if users will notice the change (left for the release PR)