Prompt
Select data structures and algorithms that optimize for the specific use case rather than defaulting to generic collections. Consider computational complexity, memory usage, and access patterns when making these choices.
Key principles:
- Use HashMap/BTreeMap for key-value lookups instead of Vec when you need efficient searching:
HashMap<String, i32>instead ofVec<(String, i32)> - Choose fixed-size types when the size is known: use
[u8; 32]for commit hashes instead ofVec<u8> - Use appropriate algorithms for bit manipulation:
i.count_ones()instead of casting to usize for bit counting - Leverage established, well-tested libraries for complex parsing instead of custom implementations (e.g., use
pegfor grammar parsing) - Design enums with non-zero discriminants when differentiating from empty storage values:
Active = 1instead ofActive = 0 - Avoid redundant computations by using operations that combine checks: use
checked_sub()and match on the result instead of separate comparison and subtraction
Example of efficient data structure selection:
// Instead of this:
let chain_ids_and_ids: Vec<(String, i32)> = fetch_data();
// Use this for lookups:
let chain_lookup: HashMap<String, i32> = fetch_data().into_iter().collect();
// Instead of this for bit counting:
sync_committee_bits.iter().map(|i| *i as usize).sum::<usize>()
// Use this:
sync_committee_bits.iter().map(|i| i.count_ones() as usize).sum::<usize>()