From 6707bcf3da350ff273208bb38dc29acc4cc4850c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Wed, 20 Mar 2024 04:39:57 +0000 Subject: [PATCH] optimize LruTimeCache::remove_expired_values, (#242) by removing the double iteration --- src/lru_time_cache.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/lru_time_cache.rs b/src/lru_time_cache.rs index fae149e3a..4eab65d2c 100644 --- a/src/lru_time_cache.rs +++ b/src/lru_time_cache.rs @@ -88,17 +88,11 @@ impl LruTimeCache { /// 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(); } } }