perf(xlsx): merge row-overlapping components without repeated rescans
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.1before the complete run. - Ruff lint, formatting, and
git diff --checkpass. 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.
| Operation | Populated cells | Before median | After median |
|---|---|---|---|
| Complete component discovery and merging | 64 | 0.685 ms | 0.434 ms |
| Complete component discovery and merging | 512 | 6.899 ms | 1.488 ms |
| Complete component discovery and merging | 2,048 | 87.243 ms | 5.032 ms |
| Complete component discovery and merging | 4,096 | 329.981 ms | 9.653 ms |
Public partition_xlsx on XLSX bytes | 64 | 18.573 ms | 17.390 ms |
Public partition_xlsx on XLSX bytes | 2,048 | 424.877 ms | 357.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.