fix: Use while loop instead of if for robust cache size guarantee

Problem:
- With `if >= CACHE_SIZE`, only ONE element removed per call
- If cache ever had >10 elements (abnormal state), would stay oversized
- Example: 12 elements → if removes 1 → 11 elements → add 1 → 12 elements 

Solution:
- Use `while >= CACHE_SIZE` to remove ALL excess elements
- Example: 12 elements → while removes 2 → 10 elements → add 1 → 10 elements 
- Guarantees exactly ≤10 elements regardless of initial state

Changes:
- mark_block_downloaded(): changed `if` to `while`
- Updated comment to reflect "tous les éléments excédentaires"
- Documentation updated with robustness guarantee
This commit is contained in:
Claude
2025-11-05 05:35:09 +00:00
parent ec61af7b71
commit 17dec3351e
2 changed files with 5 additions and 4 deletions

View File

@@ -118,8 +118,8 @@ AudioSegment::new_audio(42, AudioChunk::I16(...))
const RECENT_BLOCKS_CACHE_SIZE: usize = 10;
fn mark_block_downloaded(&mut self, event_id: EventId) {
// 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 {
// Retirer tous les éléments excédentaires (garantit <= CACHE_SIZE)
while self.recent_blocks.len() >= RECENT_BLOCKS_CACHE_SIZE {
self.recent_blocks.pop_front();
}
@@ -131,6 +131,7 @@ fn mark_block_downloaded(&mut self, event_id: EventId) {
**Avantages VecDeque** :
- ✅ Ordre FIFO garanti (le plus ancien est toujours retiré)
- ✅ Simple et prévisible
- ✅ Robuste : `while` garantit exactement 10 éléments max, même en cas d'état anormal
- ✅ 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

View File

@@ -67,8 +67,8 @@ impl RadioParadiseStreamSourceLogic {
/// Marque un bloc comme récemment téléchargé (FIFO)
fn mark_block_downloaded(&mut self, event_id: EventId) {
// 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 {
// Retirer tous les éléments excédentaires (garantit <= CACHE_SIZE)
while self.recent_blocks.len() >= RECENT_BLOCKS_CACHE_SIZE {
self.recent_blocks.pop_front();
}