Skip to content

perf(docx): resolve each paragraph style once per document (5.3 s -> 1.0 s outside spaCy) - #4554

Merged
cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
deadstrobe5:docx-style-lookup-once
Oct 16, 2026
Merged

cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
deadstrobe5:docx-style-lookup-once

Conversation

@deadstrobe5

@deadstrobe5 deadstrobe5 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

partition_docx() looks up a paragraph's style through python-docx's Paragraph.style up to six times per paragraph (paragraph.style and paragraph.style.name reads 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:

Document total before total after outside spaCy before outside spaCy after
3,000 paragraphs on python-docx's default template (164 styles) 12.5 s 8.5 s 5.30 s 0.97 s
example-docs/handbook-872p.docx (48 styles) 13.98 s 10.36 s 5.26 s 1.94 s

Output is unchanged: elements_to_json() is byte-identical before and after for every .docx in example-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.py passes and ruff is clean. CHANGELOG and __version__ are bumped to 0.27.28-dev3.

Draft #4538 adds a similar style cache inside _ListLabels for its heading check; if both land they can share it.

View guided diff

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@cragwolfe
cragwolfe merged commit e80c88c into Unstructured-IO:main Oct 16, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants