Optimize performance by minimizing unnecessary memory allocations and system calls. Key practices: 1. Avoid unnecessary String allocations: ```rust // Instead of
Optimize performance by minimizing unnecessary memory allocations and system calls. Key practices:
// Use when possible let s = some_str.into(); // Or let s = Cow::Borrowed(some_str);
2. Pre-allocate collections when size is known:
```rust
// Instead of
let mut vec = Vec::new();
items.iter().for_each(|i| vec.push(i));
// Use
let mut vec = Vec::with_capacity(items.len());
items.iter().for_each(|i| vec.push(i));
// Use
static TIME_CACHE: LazyLock
4. Return iterators instead of collecting into vectors when possible:
```rust
// Instead of
pub fn items(&self) -> Vec<Item> {
self.items.iter().map(|i| i.clone()).collect()
}
// Use
pub fn items(&self) -> impl Iterator<Item = Item> + '_ {
self.items.iter().cloned()
}
// Cache the information once let is_dir = entry.file_type() .is_ok_and(|ft| ft.is_dir()); ```
Enter the URL of a public GitHub repository