Skip to content

perf(docx): skip the math XML round-trip when there are no equations - #2851

Open
Houyun Sun (houyunsun) wants to merge 1 commit into
microsoft:mainfrom
houyunsun:docx-skip-math-preprocess-without-equations
Open

Houyun Sun (houyunsun) wants to merge 1 commit into
microsoft:mainfrom
houyunsun:docx-skip-math-preprocess-without-equations

Conversation

@houyunsun

Copy link
Copy Markdown

_pre_process_math runs on word/document.xml for every .docx. It decodes the part, parses it with BeautifulSoup and re-serializes it, even when the document has no equations at all. On a 12.5 MB document.xml that round-trip is ~9s of work whose result is discarded, since there is no oMath to replace.

This adds the same guard _pre_process_strike already has a few lines above: if the OMML prefix isn't in the bytes, return them unchanged.

if b"oMath" not in content:
    return content

Documents that do have equations are untouched — test_math_pre_process_still_converts_equations covers that, and the existing test_docx_equations / test_docx_malformed_equations still pass.

The bug that comes with it

Apache POI's ExternalEntityInText.docx currently fails with ExpatError: no element found: line 3, column 0. The part starts with a DOCTYPE that carries an internal subset (<!ENTITY testent SYSTEM "http://poi.apache.org/">), and BeautifulSoup re-serializes the prolog as a bare <!DOCTYPE REMOTE> — the entity declaration is gone, but &testent; is still in the body, and mammoth's expat parse gives up on it. Now that documents without math are left alone, that file converts like any other; the entity stays unresolved and nothing is fetched.

Measurements

bug65649.docx from POI's test-data (475 KB zip, 12.5 MB document.xml), on a 2-core box:

_pre_process_math
before 9.43 s (re-serialized, output discarded)
after 0.006 s (returns the input)

Tests

Added to tests/test_docx.py:

  • test_math_pre_process_skips_documents_without_equations — bytes with a DOCTYPE/entity and no math come back unchanged
  • test_math_pre_process_still_converts_equations — a document with oMath is still turned into $a+b$
  • test_docx_with_doctype_and_no_equations_converts — end-to-end through MarkItDown(), built from the existing test.docx fixture (asserts the fixture has no oMath first, so a future fixture swap doesn't quietly neuter it)

With the guard reverted, the first and third fail (2 failed, 1 passed); with it, all three pass.

$ pytest packages/markitdown/tests/test_docx.py -q
104 passed

$ pytest packages/markitdown/tests/ -q
45 failed, 1121 passed, 12 skipped

The 45 failures are the test_cli_vectors / test_module_vectors / test_pdf URL tests, which need outbound network from the test host — identical set before and after the change.

ruff check is clean on both files.

Fixes #2834

_pre_process_math decodes word/document.xml, parses it with BeautifulSoup
and re-serializes it for every .docx, even when nothing in the file is a
formula. On a 12.5 MB document.xml that round-trip is ~9s of work whose
result is thrown away, because there is no oMath to replace.

Guard it the same way _pre_process_strike already does a few lines above
and hand back the original bytes when the OMML prefix is absent. Output is
byte-for-byte identical, only the parser run disappears.

It also fixes a conversion failure on documents whose DOCTYPE carries an
internal subset: BeautifulSoup re-serializes the prolog as a bare
<!DOCTYPE REMOTE>, the entity declarations go away while &testent; stays in
the body, and mammoth's expat parse then dies with "no element found".
Those documents have no math either, so leaving their XML alone is enough.

Fixes microsoft#2834
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.

DOCX: math pre-processing re-parses every document.xml even without equations (32 s -> 17.5 s on a 12.5 MB document)

1 participant