Skip to content

Commit 87f30b3

Browse files
committed
IronDrop: festive cleanup
1 parent 50f7922 commit 87f30b3

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/ultra_compact_search.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/response_utils_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn test_create_error_response_headers_and_body() {
2424

2525
let handle = thread::spawn(move || {
2626
let (mut stream, _) = listener.accept().unwrap();
27-
let mut resp = create_error_response(401, "Unauthorized");
27+
let resp = create_error_response(401, "Unauthorized");
2828
// Should include WWW-Authenticate for 401
2929
resp.send(&mut stream, "[test]").unwrap();
3030
});

0 commit comments

Comments
 (0)