perf(xlsx): merge row-overlapping components without repeated rescans

Merged
AF
2d ago
+110-16

Problem

After finding populated-cell components, XLSX subtable detection repeatedly unions every component that overlaps in row range. Each pairwise merge copies the accumulated cell set and discards its cached bounds, so the next comparison scans that growing set again. A wide sparse row containing many disconnected populated cells makes this phase quadratic.

The earlier #4563 / #4357 work changed populated-cell discovery and HTML rendering; it retained this pairwise row-overlap merge.

Change

Accumulate one cell set for each overlapping group and track its maximum row separately. Materialize the merged component when the group ends. Component sorting, transitive row-overlap semantics, source cell sets, table order, and document rendering are preserved.

Correctness and regression checks

  • A deterministic work-bound regression uses 200 disconnected cells on one row. The unchanged implementation visits 20,099 cells while recomputing component extents and fails the linear budget; the fix passes.
  • Compare cell membership and bounds against the previous pairwise algorithm on 100 seeded worksheets, including unchanged source component sets.
  • Compare complete serialized output from a real XLSX workbook under both header settings and both HTML-output settings.
  • New regressions and existing XLSX tests: 164 passed, with one existing pandas future warning about a null-value comparison. Installed the lockfile's test-only networkx==3.6.1 before the complete run.
  • Ruff lint, formatting, and git diff --check pass. No full repository suite was run.

Measurements

Local Python 3.12, seven alternating before/after pairs per case with garbage collection before each sample. Input is a synthetic row with one populated cell every other column. Every component result or serialized document result has the same SHA-256 before and after.

OperationPopulated cellsBefore medianAfter median
Complete component discovery and merging640.685 ms0.434 ms
Complete component discovery and merging5126.899 ms1.488 ms
Complete component discovery and merging2,04887.243 ms5.032 ms
Complete component discovery and merging4,096329.981 ms9.653 ms
Public partition_xlsx on XLSX bytes6418.573 ms17.390 ms
Public partition_xlsx on XLSX bytes2,048424.877 ms357.962 ms

The public-path measurements include workbook decoding and table/text generation, with language detection and HTML metadata disabled. Their smaller improvement is expected because those other phases are unchanged. These synthetic measurements do not establish a general speedup for all workbooks.

Suggested label: enhancement.