Skip to content

Speed up new_subdoc() for in-memory documents - #202

Merged
cvas22 merged 2 commits into
mainfrom
perf/subdoc-in-memory-copy
Nov 23, 2026
Merged

cvas22 merged 2 commits into
mainfrom
perf/subdoc-in-memory-copy

Conversation

@cvas22

@cvas22 cvas22 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

What this changes

Inserting an in-memory Document or a rendered DocxTemplate goes 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 BytesIO and 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 new Document that 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 a DocxTemplate with pending replace_* calls, since those only apply on save. A rendered DocxTemplate is used as rendered, without reloading it from the template file.

docxcompose rebuilds the style id mapping for every subdoc by building a python-docx Style object for every style of both documents, which was most of what was left. SubdocComposer now reads w:styleId and w:name straight from the XML with the same BabelFish name 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 Document you passed in rather than with a private copy. Nothing changes them during insertion, but if you edit that Document's diagrams or charts after new_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 from tests/templates, that images still work, that one with custom properties still works and stays unchanged, and that the style mapping matches doc.styles. I also compared the mapping with docxcompose's on all 43 templates in tests/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:

Case main this PR change
30 paragraphs 30ms 29ms same
1000 paragraphs 376ms 375ms same
1000 paragraphs, sandbox off 370ms 369ms same
1000-row table loop 85ms 85ms same
30 subdocs from streams 528ms 468ms ~1.1x faster
30 in-memory Documents 1153ms 534ms ~2.2x faster
30 rendered sub-templates 1176ms 568ms ~2.1x faster

The 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-memory Document and from 14.3ms to 12.3ms for a stream.

  • Tests added or updated in tests/unit
  • CHANGES.rst updated if users will notice the change (left for the release PR)
  • Docs updated if behavior or the API changed (no API change)

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
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.
@cvas22
cvas22 merged commit 64efe2a into main Nov 23, 2026
11 checks passed
@cvas22
cvas22 deleted the perf/subdoc-in-memory-copy branch November 23, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code Changes to the docform package tests Changes to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new_subdoc() is slow for in-memory documents

1 participant