From e9d4167d4fb9b2b8c1da200e40bae7de549ba293 Mon Sep 17 00:00:00 2001 From: Filipe Oliveira Date: Thu, 2 Jul 2026 21:14:49 +0100 Subject: [PATCH] perf: iterate counts[] in value_at_quantile to avoid per-element bounds check Scan with counts.iter().enumerate() instead of indexing counts[i] in a 0..len loop, so the element is handed to the loop without a bounds check on the hot prefix-sum path. Results unchanged. --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e3a4e4b..ddb17a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1350,11 +1350,12 @@ impl Histogram { count_at_quantile = 1; } + // Iterate the slice directly so the element is handed to the loop without a + // per-element bounds check on the hot prefix-sum path. + // TODO overflow let mut total_to_current_index: u64 = 0; - for i in 0..self.counts.len() { - // Direct indexing is safe; indexes must reside in counts array. - // TODO overflow - total_to_current_index += self.counts[i].as_u64(); + for (i, count) in self.counts.iter().enumerate() { + total_to_current_index += count.as_u64(); if total_to_current_index >= count_at_quantile { let value_at_index = self.value_for(i); return if quantile == 0.0 {