From bb7d473edf3fcf8f19e3dfc4bb861545d6167a1a Mon Sep 17 00:00:00 2001 From: guillaume Date: Mon, 25 May 2026 15:14:27 +0200 Subject: [PATCH 1/2] perf: apply init-and-skip pattern to ciou and diou distance 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. --- powerboxesrs/benches/bench_iou.rs | 40 +++++++++++++++++++++++++++++++ powerboxesrs/src/ciou.rs | 3 +-- powerboxesrs/src/diou.rs | 3 +-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/powerboxesrs/benches/bench_iou.rs b/powerboxesrs/benches/bench_iou.rs index 8cca8fa..fe9911c 100644 --- a/powerboxesrs/benches/bench_iou.rs +++ b/powerboxesrs/benches/bench_iou.rs @@ -1,5 +1,7 @@ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion}; use ndarray::Array2; +use powerboxesrs::ciou::ciou_distance; +use powerboxesrs::diou::diou_distance; use powerboxesrs::giou::{ giou_distance, parallel_giou_distance, parallel_rotated_giou_distance, rotated_giou_distance, }; @@ -80,6 +82,42 @@ pub fn parallel_giou_distance_benchmark(c: &mut Criterion) { }); } +pub fn ciou_distance_benchmark(c: &mut Criterion) { + let mut boxes1 = Array2::::zeros((100, 4)); + for i in 0..100 { + for j in 0..4 { + if j < 2 { + boxes1[[i, j]] = 0.0; + } else { + boxes1[[i, j]] = 10.0; + } + } + } + let boxes2 = boxes1.clone(); + + c.bench_function("ciou distance benchmark", |b| { + b.iter(|| ciou_distance(black_box(&boxes1), black_box(&boxes2))) + }); +} + +pub fn diou_distance_benchmark(c: &mut Criterion) { + let mut boxes1 = Array2::::zeros((100, 4)); + for i in 0..100 { + for j in 0..4 { + if j < 2 { + boxes1[[i, j]] = 0.0; + } else { + boxes1[[i, j]] = 10.0; + } + } + } + let boxes2 = boxes1.clone(); + + c.bench_function("diou distance benchmark", |b| { + b.iter(|| diou_distance(black_box(&boxes1), black_box(&boxes2))) + }); +} + /// Generate 100x5 rotated boxes with varied angles for benchmarking fn make_rotated_boxes(n: usize) -> Array2 { let mut boxes = Array2::::zeros((n, 5)); @@ -154,6 +192,8 @@ criterion_group!( parallel_iou_distance_benchmark, giou_distance_benchmark, parallel_giou_distance_benchmark, + ciou_distance_benchmark, + diou_distance_benchmark, rotated_iou_distance_benchmark, parallel_rotated_iou_distance_benchmark, rotated_giou_distance_benchmark, diff --git a/powerboxesrs/src/ciou.rs b/powerboxesrs/src/ciou.rs index e702bfc..03560c6 100644 --- a/powerboxesrs/src/ciou.rs +++ b/powerboxesrs/src/ciou.rs @@ -28,7 +28,7 @@ where { let two = N::one() + N::one(); let four_over_pi_sq: f64 = 4.0 / (std::f64::consts::PI * std::f64::consts::PI); - let mut result = vec![0.0f64; n1 * n2]; + let mut result = vec![utils::ONE; n1 * n2]; let areas1 = boxes::box_areas_slice(boxes1, n1); let areas2 = boxes::box_areas_slice(boxes2, n2); @@ -44,7 +44,6 @@ where let x2 = utils::min(a1_x2, a2_x2); let y2 = utils::min(a1_y2, a2_y2); if x2 < x1 || y2 < y1 { - result[i * n2 + j] = utils::ONE; continue; } let intersection = (x2 - x1) * (y2 - y1); diff --git a/powerboxesrs/src/diou.rs b/powerboxesrs/src/diou.rs index f907276..01acf78 100644 --- a/powerboxesrs/src/diou.rs +++ b/powerboxesrs/src/diou.rs @@ -24,7 +24,7 @@ where N: Num + PartialOrd + ToPrimitive + Float, { let two = N::one() + N::one(); - let mut result = vec![0.0f64; n1 * n2]; + let mut result = vec![utils::ONE; n1 * n2]; let areas1 = boxes::box_areas_slice(boxes1, n1); let areas2 = boxes::box_areas_slice(boxes2, n2); @@ -40,7 +40,6 @@ where let x2 = utils::min(a1_x2, a2_x2); let y2 = utils::min(a1_y2, a2_y2); if x2 < x1 || y2 < y1 { - result[i * n2 + j] = utils::ONE; continue; } let intersection = (x2 - x1) * (y2 - y1); From c3ec7d11bcf7750ede5f2f0dc6f66c40937b17f9 Mon Sep 17 00:00:00 2001 From: guillaume Date: Sat, 13 Jun 2026 17:28:17 +0200 Subject: [PATCH 2/2] refactor: drop redundant zero-init in ciou/diou benchmarks --- powerboxesrs/benches/bench_iou.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/powerboxesrs/benches/bench_iou.rs b/powerboxesrs/benches/bench_iou.rs index fe9911c..adbb6e9 100644 --- a/powerboxesrs/benches/bench_iou.rs +++ b/powerboxesrs/benches/bench_iou.rs @@ -85,12 +85,8 @@ pub fn parallel_giou_distance_benchmark(c: &mut Criterion) { pub fn ciou_distance_benchmark(c: &mut Criterion) { let mut boxes1 = Array2::::zeros((100, 4)); for i in 0..100 { - for j in 0..4 { - if j < 2 { - boxes1[[i, j]] = 0.0; - } else { - boxes1[[i, j]] = 10.0; - } + for j in 2..4 { + boxes1[[i, j]] = 10.0; } } let boxes2 = boxes1.clone(); @@ -103,12 +99,8 @@ pub fn ciou_distance_benchmark(c: &mut Criterion) { pub fn diou_distance_benchmark(c: &mut Criterion) { let mut boxes1 = Array2::::zeros((100, 4)); for i in 0..100 { - for j in 0..4 { - if j < 2 { - boxes1[[i, j]] = 0.0; - } else { - boxes1[[i, j]] = 10.0; - } + for j in 2..4 { + boxes1[[i, j]] = 10.0; } } let boxes2 = boxes1.clone();