From 82374deca50cb8901abe357d57e34e5d4dcbd979 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Thu, 20 Aug 2026 05:34:14 +0200 Subject: [PATCH] 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. --- src/obicompactvec/src/intmatrix.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/obicompactvec/src/intmatrix.rs b/src/obicompactvec/src/intmatrix.rs index 8d7cff3c..1d4b3246 100644 --- a/src/obicompactvec/src/intmatrix.rs +++ b/src/obicompactvec/src/intmatrix.rs @@ -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> { - let n_cols = self.n_cols(); - let mut out: Vec> = 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> = (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 = 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; } } }