@@ -359,8 +359,9 @@ impl CompactCache {
359359
360360 pub fn put ( & mut self , query : & str , entry_ids : Vec < u32 > ) {
361361 if self . cache . len ( ) >= self . max_entries {
362- // Simple eviction: remove first entry
363- self . cache . remove ( 0 ) ;
362+ // Simple eviction: remove last entry (O(1))
363+ // This avoids O(n) shifting that occurs with removing at index 0
364+ self . cache . pop ( ) ;
364365 }
365366
366367 let hash = Self :: hash ( query) ;
@@ -481,6 +482,20 @@ mod tests {
481482 // In debug mode, timing can vary - just check it's reasonable (<100ms)
482483 assert ! ( elapsed. as_millis( ) < 100 ) ; // Should be under 100ms even in debug
483484 }
485+
486+ #[ test]
487+ fn test_compact_cache_eviction_pop_last ( ) {
488+ let mut cache = CompactCache :: new ( 3 ) ;
489+ cache. put ( "a" , vec ! [ 1 ] ) ;
490+ cache. put ( "b" , vec ! [ 2 ] ) ;
491+ cache. put ( "c" , vec ! [ 3 ] ) ;
492+ // Fill beyond capacity triggers eviction using pop()
493+ cache. put ( "d" , vec ! [ 4 ] ) ;
494+ // Ensure we still have max_entries
495+ assert_eq ! ( cache. cache. len( ) , 3 ) ;
496+ // The cache stores sorted by hash; check that 'd' is retrievable
497+ assert ! ( cache. get( "d" ) . is_some( ) ) ;
498+ }
484499}
485500
486501/// Demonstrates memory savings
0 commit comments