Repository navigation
perf(docx): skip the math XML round-trip when there are no equations - #2851
Open
Houyun Sun (houyunsun) wants to merge 1 commit into
Open
Houyun Sun (houyunsun) wants to merge 1 commit into
Houyun Sun (houyunsun) wants to merge 1 commit into
Conversation
_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
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.
_pre_process_mathruns onword/document.xmlfor 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 MBdocument.xmlthat round-trip is ~9s of work whose result is discarded, since there is nooMathto replace.This adds the same guard
_pre_process_strikealready has a few lines above: if the OMML prefix isn't in the bytes, return them unchanged.Documents that do have equations are untouched —
test_math_pre_process_still_converts_equationscovers that, and the existingtest_docx_equations/test_docx_malformed_equationsstill pass.The bug that comes with it
Apache POI's
ExternalEntityInText.docxcurrently fails withExpatError: no element found: line 3, column 0. The part starts with aDOCTYPEthat 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.docxfrom POI'stest-data(475 KB zip, 12.5 MBdocument.xml), on a 2-core box:_pre_process_mathTests
Added to
tests/test_docx.py:test_math_pre_process_skips_documents_without_equations— bytes with a DOCTYPE/entity and no math come back unchangedtest_math_pre_process_still_converts_equations— a document withoMathis still turned into$a+b$test_docx_with_doctype_and_no_equations_converts— end-to-end throughMarkItDown(), built from the existingtest.docxfixture (asserts the fixture has nooMathfirst, 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.The 45 failures are the
test_cli_vectors/test_module_vectors/test_pdfURL tests, which need outbound network from the test host — identical set before and after the change.ruff checkis clean on both files.Fixes #2834