From 22418819ba75714a6ad82496fa64d98af4a29044 Mon Sep 17 00:00:00 2001 From: Christian Meissl Date: Fri, 17 May 2024 21:07:12 +0200 Subject: [PATCH] timers: short-cut timeout extension pattern in case we constantly extend the same timer the head might already point to the timer we want to cancel. in this case we can directly remove the timer to reduce building up the heap unnecessarily. --- src/sources/timer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/sources/timer.rs b/src/sources/timer.rs index 7a4cba85..2b164dc3 100644 --- a/src/sources/timer.rs +++ b/src/sources/timer.rs @@ -229,6 +229,16 @@ impl TimerWheel { } pub(crate) fn cancel(&mut self, counter: u32) { + if self + .heap + .peek() + .map(|data| data.counter == counter) + .unwrap_or(false) + { + self.heap.pop(); + return; + }; + self.heap .iter() .find(|data| data.counter == counter)