Skip to content

perf: apply init-and-skip pattern to ciou and diou distance#88

Draft
Smirkey wants to merge 1 commit into
mainfrom
perf/ciou-diou-init-skip
Draft

perf: apply init-and-skip pattern to ciou and diou distance#88
Smirkey wants to merge 1 commit into
mainfrom
perf/ciou-diou-init-skip

Conversation

@Smirkey
Copy link
Copy Markdown
Owner

@Smirkey Smirkey commented May 25, 2026

Summary

Follow-up to #85, applying the same init-and-skip pattern that gcanat used on iou_distance_slice to ciou_distance_slice and diou_distance_slice.

The pattern: pre-fill result with utils::ONE once, then continue on the no-overlap branch — instead of initializing to zeros and explicitly writing result[idx] = ONE per non-overlapping pair. One fewer write + one fewer index calculation in the no-overlap hot path.

Also adds ciou_distance_benchmark and diou_distance_benchmark (neither existed in bench_iou.rs before) so CodSpeed can measure the delta on this PR and on any future change to these functions.

Other distance functions are not eligible for this pattern:

  • giou_distance_slice always computes the enclosing-box term and writes every cell unconditionally.
  • tiou_distance_slice has no no-overlap branch at all.

Test plan

  • cd powerboxesrs && cargo test — all 91 + 18 doctests pass.
  • cd powerboxesrs && cargo test --no-default-features — slice-only build, 52 + 2 doctests pass.
  • cargo bench --bench bench_iou -- 'ciou distance|diou distance' --quick — new benches run (ciou ~50 µs, diou ~30 µs on 100×100).
  • cargo fmt --check on both crates.
  • CodSpeed: expect ciou distance benchmark and diou distance benchmark to appear in the report. If they show a regression or ~0% change, the LLVM optimizer was already eliding the redundant write — the refactor still stands as a readability cleanup.

Mirrors the iou_distance_slice refactor from #85: pre-fill result with ONE
and skip on no-overlap instead of writing ONE per-cell. Adds ciou+diou
benchmarks so CodSpeed can measure the win.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 25, 2026

Merging this PR will improve performance by 34.89%

⚡ 2 improved benchmarks
✅ 173 untouched benchmarks
🆕 2 new benchmarks
⏩ 1 skipped benchmark1

Performance Changes

Benchmark BASE HEAD Efficiency
test_diou_distance[float32] 212.6 µs 157.9 µs +34.68%
test_diou_distance[float64] 224.3 µs 166.1 µs +35.1%
🆕 ciou distance benchmark N/A 1.5 ms N/A
🆕 diou distance benchmark N/A 257.4 µs N/A

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/ciou-diou-init-skip (bb7d473) with main (6f1c650)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Guess you can skip that as well, boxes1 is already initialized to zero.

for i in 0..100 {
for j in 0..4 {
if j < 2 {
boxes1[[i, j]] = 0.0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here

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