From ec61af7b711294bb95e0ac068ff1a21973eba095 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 5 Nov 2025 05:33:36 +0000 Subject: [PATCH] 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 --- pmoparadise/RADIO_PARADISE_STREAM_SOURCE.md | 10 ++++++---- pmoparadise/src/radio_paradise_stream_source.rs | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pmoparadise/RADIO_PARADISE_STREAM_SOURCE.md b/pmoparadise/RADIO_PARADISE_STREAM_SOURCE.md index ccf8235f..37c05589 100644 --- a/pmoparadise/RADIO_PARADISE_STREAM_SOURCE.md +++ b/pmoparadise/RADIO_PARADISE_STREAM_SOURCE.md @@ -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 diff --git a/pmoparadise/src/radio_paradise_stream_source.rs b/pmoparadise/src/radio_paradise_stream_source.rs index 45168b37..f07859ff 100644 --- a/pmoparadise/src/radio_paradise_stream_source.rs +++ b/pmoparadise/src/radio_paradise_stream_source.rs @@ -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