perf(tables): Speed up dense table postprocessing
What
Speed up dense table postprocessing while preserving extracted cells, text, ordering, HTML, and confidence values.
Full-job example: generated dense ledger
A two-page fictional ledger was processed through real table detection, OCR, and Table Transformer inference. Each crop contained approximately 950 inferred cells and 970 OCR tokens. These are controlled CPU test results, not production traffic measurements.
| Measurement | Before | After | Speedup |
|---|---|---|---|
| Table geometry processing, summed across both crops | 13.68 s | 0.33 s | 41× |
| Complete job, submission through completion | 90.07 s | 76.77 s | 1.17× — 13.30 s / 14.8% less elapsed time |
Values are means across five matched baseline/candidate pairs with fixed worker counts and resources, interleaved run order, and identical instrumentation. All five dense-job pairs improved, saving 4.92–23.18 seconds. Model inference and OCR timings remained approximately unchanged.
The 41× result applies to the geometry step, not the model or whole job. Ordinary table PDFs and a medium-density ledger did not show a reliable whole-job improvement. Unchanged-baseline and no-table controls showed substantial job-lifecycle noise, so these results establish a promising workload-specific benefit, not a universal speedup. Step spans can overlap and should not be added to reconstruct job duration.
Reproducible geometry-only benchmark
Run python -m benchmarks.table_geometry. Medians of seven interleaved runs on a shared arm64 Mac, Python 3.12.13 / NumPy 2.4.4:
| Cells / tokens | Before | After | Speedup |
|---|---|---|---|
| 10 / 40 | 0.574 ms | 0.506 ms | 1.13× |
| 100 / 400 | 56.498 ms | 26.161 ms | 2.16× |
| 500 / 2,000 | 1,399.155 ms | 575.815 ms | 2.43× |
This separate synthetic benchmark measures assignment and text selection; it excludes OCR, model inference, and HTML generation. Speedup is before-time divided by after-time.
Why
Dense tables require many repeated rectangle comparisons when assigning words to cells. Batching those comparisons reduces Python overhead. The benefit grows when this geometry work consumes a meaningful share of the job.
Thresholds, first-match ties, token order, and object identity are preserved. Temporary pairwise arrays are bounded; unsupported coordinates and unusual rectangles retain the scalar path. Winning scores retain scalar arithmetic to preserve exact confidence rounding. No model weights or dependencies change.
Validation
On 18754ba: 431 library tests passed, with one pre-existing skip. All 13 distinct real table inputs matched exact cells and confidence, including the previous rounding regressions; 114 file comparisons in the timing campaign matched. Additional randomized checks cover score values, scalar types, assignments, and confidence without new tolerances.
CI passes on Python 3.11–3.13; independent review and Cubic found no issues. Ruff lint/format and git diff --check pass. Human approval is still required.