refactor: simplify sub_matrix with iterator-driven column population

Refactored `sub_matrix` to delegate column population to `fill_sub_matrix`, replacing manual buffer allocation and explicit permutation loops with direct value assignment via `enumerate_slots_values`. This eliminates intermediate allocations, reduces pipeline overhead, and simplifies control flow while preserving slot ordering and permutation semantics.
This commit is contained in:
Eric Coissac
2026-08-20 14:17:05 +02:00
parent ac38aa759b
commit 82374deca5
+4 -11
View File
@@ -368,13 +368,8 @@ impl PersistentCompactIntMatrix {
/// in the same order as `slots`. Column access is sequential to maximize
/// cache efficiency on the underlying mmap.
pub fn sub_matrix(&self, slots: &[usize]) -> Vec<Vec<u32>> {
let n_cols = self.n_cols();
let mut out: Vec<Vec<u32>> = Vec::with_capacity(n_cols);
for c in 0..n_cols {
let mut col_buf = vec![0u32; slots.len()];
self.col_view(c).fill_slots_values(slots, &mut col_buf);
out.push(col_buf);
}
let mut out: Vec<Vec<u32>> = (0..self.n_cols()).map(|_| Vec::new()).collect();
self.fill_sub_matrix(slots, &mut out);
out
}
@@ -396,10 +391,8 @@ impl PersistentCompactIntMatrix {
let sorted_slots: Vec<usize> = perm.iter().map(|&i| slots[i]).collect();
for (c, col) in out.iter_mut().enumerate() {
col.resize(n, 0);
let mut tmp = vec![0u32; n];
self.col_view(c).fill_slots_values_sorted(&sorted_slots, &mut tmp);
for (i, &orig_idx) in perm.iter().enumerate() {
col[orig_idx] = tmp[i];
for (i, v) in self.col_view(c).enumerate_slots_values(&sorted_slots) {
col[perm[i]] = v;
}
}
}