Prompt
Before implementing data structures or algorithms, analyze allocation patterns and optimize for common cases. Key strategies:
- Use fixed-size arrays for known small collections: ```rust // Before pub fn all() -> Vec<&’static str> { vec![“namespace”, “class”, …] }
// After pub const fn all() -> [&’static str; 15] { [“namespace”, “class”, …] } ```
- Leverage specialized data structures to avoid allocations:
- Use
hashbrown::HashMapwithentry_reffor string keys - Consider
smallvecfor collections that are usually small - Keep allocations behind
Arcwhen possible
- Use
- Profile before optimizing:
- Measure binary size impact of changes
- Use tools like cargo-bloat to identify hot spots
- Consider both debug and release mode implications
The goal is to minimize allocations in hot paths while keeping code maintainable. Always validate optimizations with benchmarks and profiles.