Skip to content

Optimize XLSX subtable detection memory usage - #4357

Closed
CyMule wants to merge 11 commits into
mainfrom
xlsx-sparse-connected-components
Closed

CyMule wants to merge 11 commits into
mainfrom
xlsx-sparse-connected-components

Conversation

@CyMule

@CyMule CyMule commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replace worksheet-sized NetworkX graph construction with traversal over populated cells. Preserve four-neighbor connectivity, row-overlap merging, element ordering, text, HTML, and metadata. NetworkX remains a test dependency for comparison with the previous algorithm, and is removed from the XLSX runtime extra.

Compatibility

No new file-size, row, cell, or processing limit is introduced. Parsing, populated-cell semantics, and table rendering are unchanged. Exact serialized output matches the old algorithm for generated multi-sheet workbooks across header/HTML options and six existing XLSX fixtures. This is evidence of compatibility, not a guarantee for every possible workbook. Pandas still materializes worksheet data and a boolean mask; memory is reduced, not capped.

Validation

Synced with current main on October 1, 2026 (released as 0.27.15).

  • XLSX suite: 88 passed, including all 512 possible 3x3 occupancy patterns compared with the legacy NetworkX algorithm.
  • Exact serialized-output parity: six existing workbooks and generated multi-sheet workbooks with four header/HTML combinations.
  • Ruff and lock consistency checks passed.
  • Fresh component-detection benchmark, three repetitions per case:
Case Sparse seconds / additional sampled-peak MB Dense seconds / additional sampled-peak MB
Dense table 0.0100 / 6.4 0.0318 / 19.1
Sparse wide edges 0.0035 / 1.9 5.8608 / 1573.5
Separated blocks 0.0034 / 0.2 0.8578 / 200.7

These measure component detection, not total API latency. Peak RSS is sampled every 5 ms, so it is a lower bound on the true peak.

@CyMule
CyMule marked this pull request as ready for review October 1, 2026 17:15

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 6 files

Shadow auto-approve: would not auto-approve because issues were found.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 2 files (changes from recent commits).

Shadow auto-approve: would not auto-approve. Auto-approval blocked by 3 unresolved issues from previous reviews.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

0 issues found across 1 file (changes from recent commits).

Shadow auto-approve: would not auto-approve. This PR does not meet the repository auto-approval settings.

Re-trigger cubic

@CyMule
CyMule marked this pull request as draft October 2, 2026 03:33
@CyMule
CyMule marked this pull request as ready for review October 2, 2026 04:12
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 7 files

Shadow auto-approve: would not auto-approve. This PR does not meet the repository auto-approval settings.

Re-trigger cubic

@CyMule CyMule closed this Oct 20, 2026
linhongyu510 pushed a commit to linhongyu510/unstructured that referenced this pull request Oct 21, 2026
…IO#4563)

## What

`partition_xlsx()` with subtable detection (the default) needs
substantially less memory on dense worksheets, and the saving grows with
sheet size. Output is unchanged.

On synthetic dense sheets (a ten-column sheet split into three dense
subtables, half integers, half short Unicode strings), peak RSS in a
fresh process drops by roughly 40–60% depending on size, with comparable
runtime. Working memory per populated cell drops to roughly a third.
Serialized element JSON is byte-identical before and after.

## Why

Two whole-sheet intermediates dominated memory on dense sheets:

1. **Subtable detection built a NetworkX graph** with a node per
populated cell and an edge per neighbor pair. It now uses an iterative
four-neighbor flood fill over populated cells (the approach from Unstructured-IO#4357),
with the same component admission order, cell membership, row-overlap
merging, table order and bounding boxes.
2. **Table HTML was rendered for the whole table at once** (pandas
`to_html` string, its parsed DOM and a full-table token list). Rows are
now formatted by pandas' formatter for the whole column, so
float/date/string formatting is unchanged, and each row is rendered and
parsed on its own before the next.

`networkx` is no longer needed at runtime by the XLSX partitioner, so it
moves from the `xlsx` extra to the test dependencies (it's still used by
other extras that depend on it).

## Behavior

- Same elements, text, `text_as_html` and metadata; exact-output tests
cover mixed types, NaN/empty cells, wide floats that switch to
scientific notation in another row, dates and HTML escaping.
- The renderer uses pandas' private formatter classes, as the existing
reader already does for `OpenpyxlReader`. Verified on pandas 2.1.4
(renderer-parity comparisons), 2.2.3 and 2.3.3.
- The worksheet cell limit, empty-row compaction, empty-sheet handling
and XLS padding handling are unchanged.

## Validation

- XLSX and dense-memory suites pass. New tests compare component
detection and rendered HTML against the previous implementation on
generated sheets.
- Ruff check/format pass.

## Release

Sets the release to **0.27.32** (0.27.31 was taken by Unstructured-IO#4538, which
merged first). It merges after Unstructured-IO#4419, Unstructured-IO#4516, Unstructured-IO#4517 and Unstructured-IO#4562, and its
CHANGELOG collects their entries and this one into a single 0.27.32
section. Unstructured-IO#4524 and Unstructured-IO#4523 are not included and will go into a later
release.

Supersedes Unstructured-IO#4357.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/Unstructured-IO/unstructured/pull/4563?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
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.

1 participant