Please help me optimizing this loop
let FieldBounds {
field_start_x,
field_start_y,
clamped_start_x,
clamped_start_y,
clamped_end_x,
clamped_end_y,
} = self.field_bounds[hidden_col];
let sum_col_start =
flat_index!([self.hidden_size.cols, self.hidden_size.z], [hidden_col, 0]);
let sum_col = &mut self.hidden_sum[sum_col_start..(sum_col_start + self.hidden_size.z)];
let total_col =
&self.hidden_totals[sum_col_start..(sum_col_start + self.hidden_size.z)];
let commited_col =
&self.is_commited[sum_col_start..(sum_col_start + self.hidden_size.z)];
for visible_y in clamped_start_y..=clamped_end_y {
let in_field_y = visible_y - field_start_y;
for visible_x in clamped_start_x..=clamped_end_x {
let in_field_x = visible_x - field_start_x;
let in_field_idx = flat_index!(
[self.diameter, self.diameter],
[in_field_y as usize, in_field_x as usize]
);
let input_cell_idx = flat_index!(
[self.visible_size.y, self.visible_size.x],
[visible_y as usize, visible_x as usize]
);
let input_cell = input[input_cell_idx];
let dictionary_start = flat_index!(
[
self.hidden_size.cols,
self.area,
self.visible_size.z,
self.hidden_size.z
],
[hidden_col, in_field_idx, input_cell, 0]
);
let dictionary_col =
&self.dictionary[dictionary_start..(dictionary_start + self.hidden_size.z)];
for (sum, &weight) in sum_col.iter_mut().zip(dictionary_col) {
*sum += weight as u16;
}
}
}
Here, self.dictionary is a giant flat array. The issue here is that the input_cell is basically a random number in the range 0..self.visible_size.z, this means the CPU is indexing randomly across an array that doesn't fit in cache. I thought it'd be enough with just making the input the fastest-changing dimension but then I lose SIMD.
Can anyone help me to optimize this?
Posted Jul 3, 2026, 6:19 PM UTC
07