From 2043bb5ece54391f898fe7aab5b388d0f268839d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jul 2018 10:20:41 -0700 Subject: [PATCH] do not reallocate waiters --- src/client/connector.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/client/connector.rs b/src/client/connector.rs index c95c47cd8..6d391af87 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -509,28 +509,30 @@ impl ClientConnector { } } + // TODO: waiters should be sorted by deadline. maybe timewheel? fn collect_waiters(&mut self) { let now = Instant::now(); let mut next = None; for waiters in self.waiters.as_mut().unwrap().values_mut() { - let mut new_waiters = VecDeque::new(); - while let Some(waiter) = waiters.pop_front() { - if waiter.wait <= now { + let mut idx = 0; + while idx < waiters.len() { + let wait = waiters[idx].wait; + if wait <= now { self.stats.timeouts += 1; + let waiter = waiters.swap_remove_back(idx).unwrap(); let _ = waiter.tx.send(Err(ClientConnectorError::Timeout)); } else { if let Some(n) = next { - if waiter.wait < n { - next = Some(waiter.wait); + if wait < n { + next = Some(wait); } } else { - next = Some(waiter.wait); + next = Some(wait); } - new_waiters.push_back(waiter); + idx += 1; } } - *waiters = new_waiters; } if next.is_some() {