perf(sorting): accumulate dense XY-cut projections once

Merged
AF
15d ago
+216-2

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

  • Both deterministic dense-work regressions fail on unchanged 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.
  • Existing XY-cut tests plus the new file: 17 passed. The new oracle compares exact pixel counts for both axes, int32/int64, 100 seeded random sets, touching/reversed/zero extents and supported negative-index cases. Public page sorting preserves column/row order and original element references.
  • Independent read-only review found no blocking issue. Ruff check/format and git diff --check pass.

Compiled benchmark and limits

scripts/benchmark_xycut_projections.py compiles 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:

WorkloadBaselineChanged
Projection, 5000 overlapping boxes across 4000 pixels8.1200.057
Public page sort, same input, x-first15.5127.824
Public page sort, same input, y-first15.8108.299
Public page sort, 500 overlapping boxes, x-first1.4540.999
Public page sort, 600 boxes in two columns, x-first16.13915.686
Public page sort, 600 boxes in two columns, y-first19.98020.389
Public page sort, 20 overlapping boxes, x-first0.2650.347
Public page sort, 20 sparse boxes, x-first0.6430.713

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:

PYTHONPATH=. python scripts/benchmark_xycut_projections.py --baseline-ref 5390c569625e8e66254a55a6446757b2d2abe492

Checklist

  • The PR title is a user-facing release-note entry with a conventional commit prefix.
  • Please apply the enhancement category label during maintainer triage.
  • No changelog edit or version bump.