Skip to content

fix(xlsx): do not create empty cells while finding tables - #5024

Merged
DanielNg0729 merged 5 commits into
docling-project:mainfrom
chdre:fix/xlsx-no-empty-cell-materialization
Oct 29, 2026
Merged

DanielNg0729 merged 5 commits into
docling-project:mainfrom
chdre:fix/xlsx-no-empty-cell-materialization

Conversation

@chdre

@chdre chdre commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Table detection in MsExcelDocumentBackend called sheet.iter_rows(...) and sheet.cell(...) over the whole data bounds. In openpyxl both create a Cell for every position they visit and keep it in sheet._cells. A sheet with a few values in a large rectangle therefore grew by one cell object per empty position, about 200 MB per million cells, and that memory was not released.

This change reads sheet._cells directly in _find_data_tables and _find_table_bounds:

  • Existing cells are visited in the same row-major order.
  • A missing cell counts as having no value and no text.
  • A missing merge anchor is still recognised, through a new _MergedCellIndex.contains_at.

The output does not change. _cells was already read directly in _find_true_data_bounds and _value_bounds.

Checklist

  • Documentation has been updated, if necessary. (not needed, internal change)
  • Examples have been added, if necessary. (not needed)
  • Tests have been added, if necessary.

Review evidence

Measured with DocumentConverter().convert() on generated workbooks, median of 3, tracemalloc (Python allocations only), Windows, Intel Core Ultra 9 285HX.

Workbook Cells in sheet after table detection Peak memory Wall time Markdown
Sparse: values in A1 and AD33334 (before) 1,000,020 194.3 MB 4.1 s identical
Sparse (after) 2 1.1 MB 0.5 s identical
Dense, 5000 rows x 30 columns (before) 150,000 388.3 MB 12.4 s identical
Dense (after) 150,000 388.3 MB 11.7 s identical

Dense sheets are unchanged, as expected.

New tests in tests/test_backend_msexcel.py:

  • test_find_data_tables_does_not_materialize_empty_cells: the sheet's cell count is unchanged by table detection, and peak memory stays under 50 MiB. It is the regression test and fails on main.
  • test_sparse_merged_anchor_and_comment_only_cell_tables: coverage test, output equality for merges and comments in a sparse sheet. It also passes on main.
  • test_missing_merge_anchor_inside_table_keeps_span: coverage test for the contains_at fallback, a merge whose anchor cell is missing keeps its span. It passes on main, which recreates the cell instead.

Old and new _find_data_tables output was also compared on all workbooks in tests/data and on 300 random sheets (values, merges, comments, gap tolerance 0 to 3), with 0 differences.

Commands run:

  • uv run pytest tests/test_backend_msexcel.py tests/test_backend_msexcel_charts.py: 43 passed, 2 skipped.
  • uv run prek run --files ...: all hooks passed.

Not run:

  • The full test suite.
  • Ground truth was not regenerated. test_e2e_excel_conversions passes unchanged.

Not included:

  • Lighter intermediate objects for ExcelCell and TableCell.
  • Processing a sheet in row windows.
  • The chart code that still calls sheet.cell(); it is already capped by MAX_CHART_TABLE_CELLS.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @chdre, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working performance xlsx issue related to xlsx backend labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit cd20b9bbe4. The analysis read the diff only. It did not run code from this pull request.

The PR fixes a memory leak in MsExcelDocumentBackend: _find_data_tables and _find_table_bounds now read sheet._cells directly instead of calling sheet.iter_rows() and sheet.cell(), which avoids openpyxl materialising empty Cell objects for every position in the bounding rectangle. A new _MergedCellIndex.contains_at method handles the merged-anchor lookup when the anchor cell is absent from _cells.

CI safety: needs maintainer care before you approve CI.

  • .github/max-lines-ignore: changes CI workflows, actions, or CI scripts
1 related pull request(s) in the same code
PR State Signal Assessment
#5015 open changes the same code related: PR 5015 also modifies msexcel_backend.py and the test file, but it addresses image-in-cell reading, not the empty-cell materialisation memory issue. The two PRs fix different problems in the same backend file.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions 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.

AI first review (advisory): no blocker found

This review covers the full diff at 7dbc2abc65. It read the code and did not run it.

I checked table detection in MsExcelDocumentBackend for sparse sheets, coordinate traversal order, and merged-cell fallback handling. The change avoids creating empty Cell objects in sheet._cells and keeps table extraction results intact.

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/msexcel_backend.py 83.33% 3 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot mentioned this pull request Oct 27, 2026
1 of 3 tasks
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Oct 27, 2026

@github-actions github-actions 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.

AI first review (advisory): no blocker found

This review covers the changes since 7dbc2abc65 at 9840b009e2. It read the code and did not run it.

The incremental change adds one test (test_threaded_comment_fallback_without_xml) for the threaded-comment XML-absent fallback path. The test logic is correct: cells with values are already in _cells after load, comments are injected in-memory, and _parse_threaded_comments correctly returns empty because no XML part exists in the saved file. The earlier review found no blockers; the original change has no new behavioral code in this diff. The human reviewer should confirm that Codecov's 5 uncovered lines do not include critical branches from this PR's new code paths.

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 27, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @chdre for this improvement. The performance fix is correct and well-benchmarked.

I only have a minor observations, mainly on the tests and the documentation.

  • test_missing_merge_anchor_inside_table_keeps_span does not fail on main. On the main branch the test passes because the old code uses sheet.cell(), which re-creates the missing cell on the fly. Either:
    • Remove the test if its only purpose is to validate internal correctness of the new _cells.get() path, or
    • Reframe it as an end-to-end test using DocumentConverter on a file with a missing merge anchor (i.e. trigger the same condition through the public API, which would fail on main before the fix only if the old sheet.cell() path were also changed).
  • test_threaded_comment_fallback_without_xml does not fail on main either. This test exercises the existing threaded-comment fallback path that was present before this PR. Running it on main produces the expected result. A test that already passed before the change provides no regression signal and gives no evidence that the PR leaves the fallback intact. Remove or reframe it as above; if it is intended to document a contract that was previously untested, note that in the PR description and make clear it is a coverage addition, not a regression test.
  • test_sparse_merged_anchor_and_comment_only_cell_tables passes on main. For the same reason as above, this test passes on main. It is a useful correctness check, but it should be labelled as a coverage test, not a regression test. The PR description implies it demonstrates behavior that the fix preserves; that framing is accurate, but the claim that the test "fails on main" is not.
  • Docstring for contains_at says "without a cell object". The phrase "without a cell object" describes the call context, not the method's contract. A reader who calls it with a coordinate that does have a cell object gets the same result. See my suggestion.
  • sorted() in the outer loop has O(N log N) cost for dense sheets. For a dense sheet where N = R × C, the sorted() call allocates a list of all N coordinate tuples and sorts it. The PR benchmark shows dense sheets are slightly faster overall, which is encouraging. The cost is acceptable. Worth noting as a trade-off in a comment alongside the sorted() call for future readers who might remove it thinking it is unnecessary.

Comment thread docling/backend/msexcel_backend.py Resolved
@chdre

chdre commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in the latest commit: removed the threaded comment fallback test (it only covered code this PR does not touch), labelled the two tests that pass on main as coverage tests in both the code and the description, kept the missing-anchor test because it is the only one reaching the new contains_at fallback, updated the contains_at docstring as suggested, and added a comment on the sorted() trade-off.

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Oct 28, 2026
ceberam
ceberam previously approved these changes Oct 28, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great, thanks 🚀

@ceberam

ceberam commented Oct 28, 2026

Copy link
Copy Markdown
Member

Sorry @chdre to bother you again, but I've just seen a conflict that needs to be resolved. Could you please rebase to main?

chdre added 4 commits October 28, 2026 13:29
Table detection used sheet.iter_rows and sheet.cell, which make openpyxl create a Cell for every empty position in the data bounds. Read sheet._cells directly instead, so memory is proportional to the filled cells. Output is unchanged.

Signed-off-by: Christer Dreierstad <[email protected]>
Remove the threaded comment fallback test, which exercises code this change does not touch. Label the two tests that pass on main as coverage tests. Tighten the contains_at docstring and note the sorted() trade-off.

Signed-off-by: Christer Dreierstad <[email protected]>
tests/test_backend_msexcel.py grew past the 1500 line limit after rebasing onto main.

Signed-off-by: Christer Dreierstad <[email protected]>
@chdre
chdre force-pushed the fix/xlsx-no-empty-cell-materialization branch from 77513f8 to 270d0f1 Compare October 28, 2026 12:31
@chdre

chdre commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main and resolved the conflict with the number-format change (a missing cell now yields empty text before _displayed_cell_text is applied). The rebase pushed tests/test_backend_msexcel.py over the 1500 line limit, so the new tests now live in tests/test_backend_msexcel_sparse.py.

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Oct 28, 2026
@ceberam

ceberam commented Oct 28, 2026

Copy link
Copy Markdown
Member

Thanks @chdre for fixing the issue.

The rebase pushed tests/test_backend_msexcel.py over the 1500 line limit, so the new tests now live in tests/test_backend_msexcel_sparse.py.

I think it's better to add this test module to the exception list .github/max-lines-ignore (it's a test file anyway). We can then keep the new tests within the same module.

Add the file to .github/max-lines-ignore instead of splitting it.

Signed-off-by: Christer Dreierstad <[email protected]>
@chdre

chdre commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Good point, done.

@github-actions github-actions Bot added ai:ci-needs-care AI triage: check CI-safety findings before you approve CI and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Oct 28, 2026
@ceberam
ceberam requested a review from DanielNg0729 October 28, 2026 12:58
@DanielNg0729
DanielNg0729 merged commit 8f7ae54 into docling-project:main Oct 29, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-needs-care AI triage: check CI-safety findings before you approve CI bug Something isn't working performance xlsx issue related to xlsx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants