fix: Prevent cache from exceeding pre-allocated capacity
Problem: - Previous logic: push_back() first, then pop_front() if len > 10 - This temporarily creates 11 elements, exceeding VecDeque capacity of 10 - Wastes the benefit of with_capacity() pre-allocation Solution: - Check capacity BEFORE adding: if len >= 10, pop_front() first - Then push_back() new element - Guarantees never exceeding 10 elements at any time Changes: - mark_block_downloaded(): inverted order (pop before push) - Changed condition from `> CACHE_SIZE` to `>= CACHE_SIZE` - Documentation updated with correct logic and benefits
This commit is contained in:
@@ -118,18 +118,20 @@ AudioSegment::new_audio(42, AudioChunk::I16(...))
|
||||
const RECENT_BLOCKS_CACHE_SIZE: usize = 10;
|
||||
|
||||
fn mark_block_downloaded(&mut self, event_id: EventId) {
|
||||
self.recent_blocks.push_back(event_id);
|
||||
|
||||
// Limiter la taille du cache : retirer le plus ancien
|
||||
if self.recent_blocks.len() > RECENT_BLOCKS_CACHE_SIZE {
|
||||
// Retirer le plus ancien si on est déjà à la limite (évite de dépasser la capacité)
|
||||
if self.recent_blocks.len() >= RECENT_BLOCKS_CACHE_SIZE {
|
||||
self.recent_blocks.pop_front();
|
||||
}
|
||||
|
||||
// Puis ajouter le nouveau bloc
|
||||
self.recent_blocks.push_back(event_id);
|
||||
}
|
||||
```
|
||||
|
||||
**Avantages VecDeque** :
|
||||
- ✅ Ordre FIFO garanti (le plus ancien est toujours retiré)
|
||||
- ✅ Simple et prévisible
|
||||
- ✅ Ne dépasse jamais la capacité pré-allouée (retire avant d'ajouter)
|
||||
- ✅ Pour 10 éléments, `contains()` en O(n) reste très performant
|
||||
|
||||
## Support FLAC
|
||||
|
||||
@@ -67,12 +67,13 @@ impl RadioParadiseStreamSourceLogic {
|
||||
|
||||
/// Marque un bloc comme récemment téléchargé (FIFO)
|
||||
fn mark_block_downloaded(&mut self, event_id: EventId) {
|
||||
self.recent_blocks.push_back(event_id);
|
||||
|
||||
// Limiter la taille du cache : retirer le plus ancien
|
||||
if self.recent_blocks.len() > RECENT_BLOCKS_CACHE_SIZE {
|
||||
// Retirer le plus ancien si on est déjà à la limite (évite de dépasser la capacité)
|
||||
if self.recent_blocks.len() >= RECENT_BLOCKS_CACHE_SIZE {
|
||||
self.recent_blocks.pop_front();
|
||||
}
|
||||
|
||||
// Puis ajouter le nouveau bloc
|
||||
self.recent_blocks.push_back(event_id);
|
||||
}
|
||||
|
||||
/// Télécharge et décode un bloc FLAC
|
||||
|
||||
Reference in New Issue
Block a user