Skip to content

Commit

Permalink
optimize LruTimeCache::remove_expired_values, (#242)
Browse files Browse the repository at this point in the history
by removing the double iteration
  • Loading branch information
jxs authored Mar 20, 2024
1 parent 15b1725 commit 6707bcf
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/lru_time_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,11 @@ impl<K: Clone + Eq + Hash, V> LruTimeCache<K, V> {

/// Removes expired items from the cache.
fn remove_expired_values(&mut self, now: Instant) {
let mut expired_keys = vec![];

for (key, (_, time)) in self.map.iter_mut() {
while let Some((_front, (_value, time))) = self.map.front() {
if *time + self.ttl >= now {
break;
}
expired_keys.push(key.clone());
}

for k in expired_keys {
self.map.remove(&k);
self.map.pop_front();
}
}
}
Expand Down

0 comments on commit 6707bcf

Please sign in to comment.