From b4cb04c7f090b99a868356a64164349736b48bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 19 Mar 2024 17:08:10 +0000 Subject: [PATCH] optimize LruTimeCache::remove_expired_values, 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(); } } }