Repository navigation
perf(sorting): accumulate dense XY-cut projections once - #4748
Conversation
cragwolfe
left a comment
There was a problem hiding this comment.
Static review of the projection change at da5abf7fb6746a2ee76bec3962d63bb4afb00bf0 found no actionable Critical, High, or Medium issues. I checked the interval boundaries, overlapping and degenerate boxes, both sorting directions, source preservation, and the existing geometry validation and recursion paths. The dense path preserves the previous projection counts for supported nonnegative integer coordinates; negative coordinates retain the prior path.
The surrounding sorter still has existing limits, including collapsed boxes and coordinate-sized allocations. The checked-in benchmark was reviewed as source; its timings were not independently verified. Tests were not run locally. CI is separate from this review and still requires workflow approval.
(authored by codex)
|
Thanks for the review. The hosted checks have now completed on the reviewed revision The separate Security workflow still reports |
Problem
XY-cut builds each projection by incrementing every covered pixel for every box. Overlapping wide/tall boxes repeatedly update the same positions, so that part of page ordering costs the sum of all projected box extents even after the existing Numba compilation.
Change
For dense projections, record each half-open interval's start/end and accumulate the counts once. This bounds nonnegative projection work by the number of boxes plus the axis length, using the existing output allocation. A single box and sparse projections retain direct filling; direct callers supplying negative coordinates keep the previous indexing path. Output dtype, shape, overlap counts and sorting order are unchanged.
This is an algorithmic change on top of #4173's JIT compilation, not another submission of that optimization. All-state searches for projection/XY-cut performance found no existing interval-difference implementation.
Validation
5390c569625e8e66254a55a6446757b2d2abe492: 100 overlapping 2000-pixel boxes perform 200,100 Python-loop iterations, beyond the linear bound. The six correctness/control tests pass on that base.git diff --checkpass.Compiled benchmark and limits
scripts/benchmark_xycut_projections.pycompiles the actual baseline function from a supplied Git revision and compares it with the current compiled function. It runs two warm-up pairs and seven measured pairs in alternating order, collects before each measurement, and verifies identical projections and public page-sort results. Input creation and result comparison are outside the timed interval.Python 3.12.14, Numba 0.68.1, Linux; local synthetic inputs. Median milliseconds:
The gain is concentrated in dense projections. Small/sparse complete sorts do not consistently improve and some measured slower, as shown above; this is not a general PDF/OCR throughput claim. Full model inference and end-to-end PDF partitioning were not measured.
Reproduce from the repository root:
Checklist
enhancementcategory label during maintainer triage.