René's URL Explorer Experiment


Title: Make section block iteration linear in document size by scks1 · Pull Request #1635 · python-openxml/python-docx · GitHub

Open Graph Title: Make section block iteration linear in document size by scks1 · Pull Request #1635 · python-openxml/python-docx

X Title: Make section block iteration linear in document size by scks1 · Pull Request #1635 · python-openxml/python-docx

Description: Iterating the blocks of every section — the natural way to partition a document by section — currently costs time quadratic in the document size, because each section's blocks are computed from a document-prefix XPath. On a generated document with 400 sections and 16,000 paragraphs, partitioning takes ~307 s; with this change it takes ~2 s, with identical results. Problem _SectBlockElementIterator computes the blocks of section i by evaluating an XPath that returns every w:p/w:tbl from the start of the document through the end of section i, plus a count(...) XPath over the prefix through section i−1, and slicing. Each section therefore rescans the whole prefix before it. Measured in block elements materialized across a full partition: document blocks sections before after generated 1,000 25 25,000 1,025 generated 4,000 100 400,000 4,100 generated 8,000 50 400,000 8,050 generated 16,000 400 6,400,000 16,400 At fixed block count, the "before" work grows in direct proportion to the number of sections. This is the cost that led the unstructured project to rewrite its DOCX partitioning rather than ask python-docx for each section's blocks, citing severe slowdowns on documents with hundreds of sections and thousands of paragraphs. A profile of the stock code attributes ~93% of partition time to these two XPath evaluations. Change One file, docx/oxml/section.py: _SectBlockElementIterator first tries a sibling-navigation computation: a section's blocks are the body-level w:p/w:tbl siblings running back from the section's terminal node (the paragraph carrying its w:sectPr, or the body-level w:sectPr itself) to — but not including — the preceding section's terminal paragraph. Collecting them costs time proportional to the section's own size, independent of the document prefix, and keeps no state between calls. The original XPath implementation is retained verbatim as a fallback (_iter_sect_block_elements_xpath) and is used whenever the sectPr does not terminate a body-level section in the ordinary way — e.g. a sectPr nested in other content, or a paragraph carrying more than one sectPr — so behavior on such documents is unchanged by construction. No public API is added, removed, or altered. CT_SectPr.preceding_sectPr is intentionally untouched: I measured its preceding:: XPath first, and libxml2 stops at the nearest match, so its cost is already bounded by the size of the preceding section rather than the document prefix. Correctness A dump comparing, per document, every section's block elements (order and text), the preceding_sectPr chain, and header/footer inheritance resolution for all six header/footer kinds is byte-identical before and after the change, across generated documents (tables as blocks, 1-block sections, an empty final section, SDT-wrapped content) and a corpus of real-world-shaped documents (multi-section headers/footers, footnotes, comments, tracked changes, shapes, a LibreOffice-produced file). The full test suite passes as before: 1,600 passed, with the same 9 pre-existing collection/setup errors in tests/opc/test_phys_pkg.py on both versions in my environment (a pytest-9 warning-as-error artifact, unrelated to this change). Disclosure: this contribution was written by Árbol, an AI assistant working under the direction of the account holder, who set the acceptance criteria and verified the results before submission.

Open Graph Description: Iterating the blocks of every section — the natural way to partition a document by section — currently costs time quadratic in the document size, because each section's blocks are computed from...

X Description: Iterating the blocks of every section — the natural way to partition a document by section — currently costs time quadratic in the document size, because each section's blocks are computed ...

Opengraph URL: https://github.com/python-openxml/python-docx/pull/1635

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:7fea0622-bd2a-efa1-5c51-7269f3ec01c0
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id9976:144E47:81C05E:96A17A:6AFF65D4
html-safe-noncee8fee92b9b980b378e8b768a3cb1cde76d0539dbf717ff3a38e25ca197a1dc98
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTc2OjE0NEU0Nzo4MUMwNUU6OTZBMTdBOjZBRkY2NUQ0IiwidmlzaXRvcl9pZCI6IjE0MTgzNzMyNDAwMzE1MDM4MjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac7d6d50415c3a59c6b66964f458695e828671534162579d94805eb2ffca559a11
hovercard-subject-tagpull_request:5098524838
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python-openxml/python-docx/pull/1635/files
twitter:imagehttps://avatars.githubusercontent.com/u/126534658?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/126534658?s=400&v=4
og:image:altIterating the blocks of every section — the natural way to partition a document by section — currently costs time quadratic in the document size, because each section's blocks are computed from...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None41b58589a1c4eda1e3c8904144a412b88ca4455f63ca2ad3629f2e9f3c3a1d12
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python-openxml/python-docx git https://github.com/python-openxml/python-docx.git
octolytics-dimension-user_id3403760
octolytics-dimension-user_loginpython-openxml
octolytics-dimension-repository_id13592924
octolytics-dimension-repository_nwopython-openxml/python-docx
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id13592924
octolytics-dimension-repository_network_root_nwopython-openxml/python-docx
turbo-body-classeslogged-out env-production page-responsive
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8968d6d6acc65d74e4df332d23932dd3fd0502a4
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python-openxml/python-docx/pull/1635/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-openxml%2Fpython-docx%2Fpull%2F1635%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython-openxml%2Fpython-docx%2Fpull%2F1635%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python-openxml%2Fpython-docx
Reloadhttps://github.com/python-openxml/python-docx/pull/1635/files
Reloadhttps://github.com/python-openxml/python-docx/pull/1635/files
Reloadhttps://github.com/python-openxml/python-docx/pull/1635/files
python-openxmlhttps://github.com/python-openxml
python-docxhttps://github.com/python-openxml/python-docx
Codehttps://github.com/python-openxml/python-docx
Issues387 (387)https://github.com/python-openxml/python-docx/issues
Pull requests167 (167)https://github.com/python-openxml/python-docx/pulls
Actionshttps://github.com/python-openxml/python-docx/actions
Projectshttps://github.com/python-openxml/python-docx/projects
Wikihttps://github.com/python-openxml/python-docx/wiki
Security and qualityhttps://github.com/python-openxml/python-docx/security
Insightshttps://github.com/python-openxml/python-docx/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython-openxml%2Fpython-docx%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython-openxml%2Fpython-docx%2Fissues%2Fnew%2Fchoose
scks1https://github.com/scks1
python-openxml:masterhttps://github.com/python-openxml/python-docx/tree/master
scks1:section-block-iteration-linearhttps://github.com/scks1/python-docx/tree/section-block-iteration-linear
Conversation 0 https://github.com/python-openxml/python-docx/pull/1635
Commits 1 https://github.com/python-openxml/python-docx/pull/1635/commits
Checks 0 https://github.com/python-openxml/python-docx/pull/1635/checks
Files changed https://github.com/python-openxml/python-docx/pull/1635/files
Please reload this pagehttps://github.com/python-openxml/python-docx/pull/1635/files
Make section block iteration linear in document size https://github.com/python-openxml/python-docx/pull/1635/files#top
Show all changes 1 commit https://github.com/python-openxml/python-docx/pull/1635/files
66b4ec4 Make section block iteration linear in document size scks1 Oct 31, 2026 https://github.com/python-openxml/python-docx/pull/1635/commits/66b4ec4475396b734beb746e085f5fbb8d2a9052
Clear filters https://github.com/python-openxml/python-docx/pull/1635/files
Please reload this pagehttps://github.com/python-openxml/python-docx/pull/1635/files
Please reload this pagehttps://github.com/python-openxml/python-docx/pull/1635/files
src/docx/oxml/section.pyhttps://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
View file https://github.com/python-openxml/python-docx/blob/66b4ec4475396b734beb746e085f5fbb8d2a9052/src/docx/oxml/section.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python-openxml/python-docx/pull/1635/{{ revealButtonHref }}
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com/python-openxml/python-docx/pull/1635/files#diff-883b408570bcf196766b5b76083f68e7d8ef2c6cfb340422ac6a91f600cd004c
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.