Skip to content

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

Merged
cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
Afloat16:linear-xycut-projections-20261114
Nov 17, 2026
Merged

cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
Afloat16:linear-xycut-projections-20261114

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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:

Workload Baseline Changed
Projection, 5000 overlapping boxes across 4000 pixels 8.120 0.057
Public page sort, same input, x-first 15.512 7.824
Public page sort, same input, y-first 15.810 8.299
Public page sort, 500 overlapping boxes, x-first 1.454 0.999
Public page sort, 600 boxes in two columns, x-first 16.139 15.686
Public page sort, 600 boxes in two columns, y-first 19.980 20.389
Public page sort, 20 overlapping boxes, x-first 0.265 0.347
Public page sort, 20 sparse boxes, x-first 0.643 0.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.

View guided diff

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The hosted checks have now completed on the reviewed revision da5abf7fb6746a2ee76bec3962d63bb4afb00bf0: CI, CodeQL, and Partition Benchmark are successful. All 50 reported check runs are successful, including the Python 3.11–3.13 unit-test matrix.

The separate Security workflow still reports action_required on that same revision. Could a maintainer approve this remaining workflow and apply the performance category label? There have been no code changes since your approval.

@cragwolfe
cragwolfe added this pull request to the merge queue Nov 17, 2026
Merged via the queue into Unstructured-IO:main with commit 230ed8b Nov 17, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants