perf: apply init-and-skip pattern to ciou and diou distance#88
Draft
Smirkey wants to merge 1 commit into
Draft
Conversation
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.
Merging this PR will improve performance by 34.89%
Performance Changes
Tip Curious why this is faster? Comment Comparing Footnotes
|
gcanat
reviewed
May 25, 2026
| for i in 0..100 { | ||
| for j in 0..4 { | ||
| if j < 2 { | ||
| boxes1[[i, j]] = 0.0; |
Collaborator
There was a problem hiding this comment.
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #85, applying the same
init-and-skippattern that gcanat used oniou_distance_slicetociou_distance_sliceanddiou_distance_slice.The pattern: pre-fill
resultwithutils::ONEonce, thencontinueon the no-overlap branch — instead of initializing to zeros and explicitly writingresult[idx] = ONEper non-overlapping pair. One fewer write + one fewer index calculation in the no-overlap hot path.Also adds
ciou_distance_benchmarkanddiou_distance_benchmark(neither existed inbench_iou.rsbefore) 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_slicealways computes the enclosing-box term and writes every cell unconditionally.tiou_distance_slicehas 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 --checkon both crates.ciou distance benchmarkanddiou distance benchmarkto 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.