Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

perf(rust): optimize face nms without vec removals

laurenspriem228b9bd3a5ed
diff --git a/mobile/apps/photos/rust/src/ml/face/detect.rs b/mobile/apps/photos/rust/src/ml/face/detect.rs
index 2a57ffa43b..043c9e1c71 100644
--- a/mobile/apps/photos/rust/src/ml/face/detect.rs
+++ b/mobile/apps/photos/rust/src/ml/face/detect.rs
@@ -122,20 +122,28 @@ fn naive_non_max_suppression(
) -> Vec<FaceDetection> {
detections.sort_by(|a, b| b.score.total_cmp(&a.score));
- let mut i = 0usize;
- while i + 1 < detections.len() {
- let mut j = i + 1;
- while j < detections.len() {
+ let mut suppressed = vec![false; detections.len()];
+ for i in 0..detections.len() {
+ if suppressed[i] {
+ continue;
+ }
+
+ for j in (i + 1)..detections.len() {
+ if suppressed[j] {
+ continue;
+ }
let iou = calculate_iou(&detections[i], &detections[j]);
if iou >= iou_threshold {
- detections.remove(j);
- } else {
- j += 1;
+ suppressed[j] = true;
}
}
- i += 1;
}
+
detections
+ .into_iter()
+ .enumerate()
+ .filter_map(|(index, detection)| (!suppressed[index]).then_some(detection))
+ .collect()
}
fn calculate_iou(a: &FaceDetection, b: &FaceDetection) -> f32 {