Skip to content

Commit c156bf9

Browse files
committed
Help LLVM remove panicking bounds checks
1 parent b749148 commit c156bf9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/kmeans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Kmeans {
124124
/// This replaces these entries with histogram colors that are currently least-fitting the palette.
125125
fn replace_unused_colors(palette: &mut PalF, hist: &HistogramInternal) -> Result<(), Error> {
126126
for pal_idx in 0..palette.len() {
127-
let pop = palette.pop_as_slice()[pal_idx];
127+
let Some(pop) = palette.pop_as_slice().get(pal_idx) else { break };
128128
if pop.popularity() == 0. && !pop.is_fixed() {
129129
let n = Nearest::new(palette)?;
130130
let mut worst = None;

src/pal.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,14 @@ impl PalF {
240240
}
241241

242242
pub fn set(&mut self, idx: usize, color: f_pixel, popularity: PalPop) {
243-
self.colors[idx] = color;
244-
self.pops[idx] = popularity;
243+
debug_assert!(idx < self.colors.len() && idx < self.pops.len());
244+
245+
if let Some(pops_idx) = self.pops.get_mut(idx) {
246+
*pops_idx = popularity;
247+
}
248+
if let Some(colors_idx) = self.colors.get_mut(idx) {
249+
*colors_idx = color;
250+
}
245251
}
246252

247253
#[inline(always)]

0 commit comments

Comments
 (0)