From 854b535624ae4be19db404412de8422d01d325f7 Mon Sep 17 00:00:00 2001 From: Pablo Deymonnaz Date: Tue, 31 Mar 2026 12:49:31 -0300 Subject: [PATCH] Avoid unnecessary Vec allocations in greedy proof selection In extend_proofs_greedily, the greedy set-cover loop was allocating a Vec for every candidate proof on each iteration just to compare coverage counts via .len(). Only the winning proof's coverage Vec is actually needed (to extend the covered set and count attestations). Now the selection pass computes only a count (no allocation) for each candidate, and collects the actual Vec only for the winner. This avoids N-1 unnecessary allocations per iteration, where N is the number of remaining proofs. --- crates/blockchain/src/store.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/blockchain/src/store.rs b/crates/blockchain/src/store.rs index 3d70e08..10fd26e 100644 --- a/crates/blockchain/src/store.rs +++ b/crates/blockchain/src/store.rs @@ -973,25 +973,31 @@ fn extend_proofs_greedily( let mut remaining_indices: HashSet = (0..proofs.len()).collect(); while !remaining_indices.is_empty() { - // Pick proof covering the most uncovered validators + // Pick proof covering the most uncovered validators (count only, no allocation) let best = remaining_indices .iter() .map(|&idx| { - let new_coverage: Vec = proofs[idx] + let count = proofs[idx] .participant_indices() .filter(|vid| !covered.contains(vid)) - .collect(); - (idx, new_coverage) + .count(); + (idx, count) }) - .max_by_key(|(_, cov)| cov.len()); + .max_by_key(|&(_, count)| count); - let Some((best_idx, new_covered)) = best else { + let Some((best_idx, best_count)) = best else { break; }; - if new_covered.is_empty() { + if best_count == 0 { break; } + // Collect coverage only for the winning proof + let new_covered: Vec = proofs[best_idx] + .participant_indices() + .filter(|vid| !covered.contains(vid)) + .collect(); + let proof = &proofs[best_idx]; attestations.push(AggregatedAttestation { aggregation_bits: proof.participants.clone(),