From 8327d327c10738517a8ff6c6aa986baa70305ae4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 13 Aug 2016 23:29:08 -0700 Subject: [PATCH] Optimize next_timeout slightly Avoid moves as it apparently adversely affects perf --- src/timer_wheel.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/timer_wheel.rs b/src/timer_wheel.rs index 8993e97b3..a1fe66951 100644 --- a/src/timer_wheel.rs +++ b/src/timer_wheel.rs @@ -231,21 +231,22 @@ impl TimerWheel { /// scheduled in this wheel. pub fn next_timeout(&self) -> Option { // TODO: can this be optimized to not look at the whole array? - let timeouts = self.wheel.iter().map(|slot| slot.next_timeout); - let min = timeouts.fold(None, |prev, cur| { - match (prev, cur) { - (None, cur) => cur, - (Some(time), None) => Some(time), - (Some(a), Some(b)) => Some(cmp::min(a, b)), + let mut min = None; + for a in self.wheel.iter().filter_map(|s| s.next_timeout.as_ref()) { + if let Some(b) = min { + if b < a { + continue + } } - }); + min = Some(a); + } if let Some(min) = min { debug!("next timeout {:?}", min); debug!("now {:?}", Instant::now()); } else { debug!("next timeout never"); } - return min + min.map(|t| *t) } /// Cancels the specified timeout.