Repository navigation
perf(docx): resolve each paragraph style once per document (5.3 s -> 1.0 s outside spaCy) - #4554
Conversation
cragwolfe
left a comment
There was a problem hiding this comment.
Reviewed 49b508b with GPT Pro. No actionable correctness, output-compatibility, performance, or release-integration issues found.
The caches preserve the existing style resolver and inheritance behavior, remain scoped to each partitioning run, and leave paragraph-specific numbering overrides and list-counter updates uncached. I also reviewed the checked-in tests and the separate changes proposed in #4538; this PR does not depend on that draft.
Exact-head CI is still running: required unit and Docker checks are pending; required ingest, lint, changelog, and Socket alert checks have passed. The Python 3.12 DOCX dependency test has also passed. No tests or benchmarks were run locally; the reported benchmark and corpus output comparisons were not independently reproduced.
(authored by codex)
partition_docx()looks up a paragraph's style through python-docx'sParagraph.styleup to six times per paragraph (paragraph.style and paragraph.style.namereads it twice, at three call sites). For a paragraph with no explicit style, which is most of them, each read scans every style definition in the document to find the default, so the cost grows with paragraphs times styles.The style name and the style-chain numbering only depend on the paragraph's style id, so each is now resolved once per document and kept in a dict on the partitioner and on
_ListLabels. Both dicts live on objects created per call, so nothing is shared between documents.Median of 7 alternating fresh-process runs, Python 3.13, Apple M5:
example-docs/handbook-872p.docx(48 styles)Output is unchanged:
elements_to_json()is byte-identical before and after for every.docxinexample-docs, with and without page breaks and table inference, including the 872-page handbook. A new test checks that a 60-paragraph document resolves its 3 styles 6 times instead of 360.test_docx.pypasses and ruff is clean. CHANGELOG and__version__are bumped to 0.27.28-dev3.Draft #4538 adds a similar style cache inside
_ListLabelsfor its heading check; if both land they can share it.