From cd97d057b0f38ae434e80dae4598652e4c795fda Mon Sep 17 00:00:00 2001 From: Nacl-HCl <308349766+Nacl-HCl@users.noreply.github.com> Date: Mon, 2 Nov 2026 12:22:28 +0800 Subject: [PATCH 1/2] perf(client): make pool waiter cleanup amortized O(1) on Checkout drop Fixes hyperium/hyper#4245 --- src/client/legacy/pool.rs | 60 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/client/legacy/pool.rs b/src/client/legacy/pool.rs index 10ef9095..6b70c444 100644 --- a/src/client/legacy/pool.rs +++ b/src/client/legacy/pool.rs @@ -469,7 +469,14 @@ impl PoolInner { fn clean_waiters(&mut self, key: &K) { let mut remove_waiters = false; if let Some(waiters) = self.waiters.get_mut(key) { - waiters.retain(|tx| !tx.is_canceled()); + // Dropping a `Checkout` leaves its parked sender in the queue as + // canceled. `put` already skips such canceled waiters lazily, so + // there is no need to rescan the whole queue on every drop: + // popping just the canceled prefix is amortized O(1), since each + // sender is popped at most once. + while waiters.front().is_some_and(|tx| tx.is_canceled()) { + waiters.pop_front(); + } remove_waiters = waiters.is_empty(); } if remove_waiters { @@ -1087,6 +1094,57 @@ mod tests { assert!(!pool.locked().waiters.contains_key(&key)); } + #[tokio::test] + async fn test_pool_checkout_drop_cleans_up_waiters_many() { + // The reported O(n^2): dropping many parked checkouts must leave no + // waiter entries behind. + let pool = pool_no_timer::, KeyImpl>(); + let key = host_key("foo"); + + let mut checkouts = Vec::new(); + for _ in 0..200 { + let mut checkout = pool.checkout(key.clone()); + PollOnce(&mut checkout).await; + checkouts.push(checkout); + } + assert_eq!(pool.locked().waiters.get(&key).unwrap().len(), 200); + + drop(checkouts); + assert!(!pool.locked().waiters.contains_key(&key)); + } + + #[tokio::test] + async fn test_pool_checkout_drop_out_of_order_then_put() { + // A canceled waiter in the middle of the queue must not break `put`: + // live waiters ahead of it are still woken, and the queue is fully + // cleaned once every checkout is dropped. + let pool = pool_no_timer::, KeyImpl>(); + let key = host_key("foo"); + + let mut checkout1 = pool.checkout(key.clone()); + let mut checkout2 = pool.checkout(key.clone()); + let mut checkout3 = pool.checkout(key.clone()); + + PollOnce(&mut checkout1).await; + PollOnce(&mut checkout2).await; + PollOnce(&mut checkout3).await; + assert_eq!(pool.locked().waiters.get(&key).unwrap().len(), 3); + + // drop the middle waiter; its sender stays parked but canceled + drop(checkout2); + + // putting a connection still wakes the live waiter at the front, + // skipping the canceled one + drop(pool.pooled(c(key.clone()), Uniq(7))); + match checkout1.await { + Ok(pooled) => assert_eq!(*pooled, Uniq(7)), + Err(_) => panic!("not ready"), + } + + drop(checkout3); + assert!(!pool.locked().waiters.contains_key(&key)); + } + #[derive(Debug)] struct CanClose { #[allow(unused)] From a07530b3e056f647fc5db04b06d32daf781be19e Mon Sep 17 00:00:00 2001 From: Nacl-HCl <308349766+Nacl-HCl@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:47:04 +0000 Subject: [PATCH 2/2] perf(client): bound stale waiters in lazy pool waiter cleanup The lazy cleanup in clean_waiters only pops the canceled prefix, so a canceled waiter parked behind a long-lived live waiter stays in the queue with no bound on how many can pile up. Bound it: clean_waiters now counts drops that leave a canceled sender behind a live waiter, and once that count reaches half the queue it does a full retain and resets the count. Stale senders can no longer accumulate without bound, and the rescan stays amortized O(1) since it only runs after at least len/2 such drops. Adds regression test test_pool_checkout_drop_bounds_stale_waiters, which fails on the previous version. --- src/client/legacy/pool.rs | 69 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/client/legacy/pool.rs b/src/client/legacy/pool.rs index 6b70c444..a318eb3a 100644 --- a/src/client/legacy/pool.rs +++ b/src/client/legacy/pool.rs @@ -93,6 +93,10 @@ struct PoolInner { // them that the Conn could be used instead of waiting for a brand new // connection. waiters: HashMap>>, + // Drops that left a canceled sender parked behind a live waiter in + // `waiters[key]`, per key. Bounds how many canceled senders can pile + // up before `clean_waiters` rescans the queue: see that function. + stale_waiters: HashMap, // A oneshot channel is used to allow the interval to be notified when // the Pool completely drops. That way, the interval can cancel immediately. idle_interval_ref: Option>, @@ -132,6 +136,7 @@ impl Pool { idle_interval_ref: None, max_idle_per_host: config.max_idle_per_host, waiters: HashMap::new(), + stale_waiters: HashMap::new(), exec, timer, timeout: config.idle_timeout, @@ -385,6 +390,7 @@ impl PoolInner { } if remove_waiters { self.waiters.remove(&key); + self.stale_waiters.remove(&key); } match value { @@ -420,6 +426,7 @@ impl PoolInner { // this Connecting task didn't complete successfully. // those waiters would never receive a connection. self.waiters.remove(key); + self.stale_waiters.remove(key); } fn spawn_idle_interval(&mut self, pool_ref: &Arc>>) { @@ -461,7 +468,7 @@ impl PoolInner { } } -impl PoolInner { +impl PoolInner { /// Any `FutureResponse`s that were created will have made a `Checkout`, /// and possibly inserted into the pool that it is waiting for an idle /// connection. If a user ever dropped that future, we need to clean out @@ -474,13 +481,31 @@ impl PoolInner { // there is no need to rescan the whole queue on every drop: // popping just the canceled prefix is amortized O(1), since each // sender is popped at most once. + let mut popped = 0; while waiters.front().is_some_and(|tx| tx.is_canceled()) { waiters.pop_front(); + popped += 1; + } + if popped == 0 && !waiters.is_empty() { + // The front waiter is live, so the just-dropped checkout is + // still parked behind it as a canceled sender. Count it: once + // such stale senders reach half the queue, rescan the whole + // queue, so canceled senders can never pile up without + // bound behind a long-lived live waiter. The rescan stays + // amortized O(1): it only runs after at least `len / 2` + // drops left a stale sender behind. + let stale = self.stale_waiters.entry(key.clone()).or_default(); + *stale += 1; + if *stale * 2 >= waiters.len() { + waiters.retain(|tx| !tx.is_canceled()); + *stale = 0; + } } remove_waiters = waiters.is_empty(); } if remove_waiters { self.waiters.remove(key); + self.stale_waiters.remove(key); } } } @@ -1145,6 +1170,48 @@ mod tests { assert!(!pool.locked().waiters.contains_key(&key)); } + #[tokio::test] + async fn test_pool_checkout_drop_bounds_stale_waiters() { + // Canceled waiters parked behind live ones must not pile up without + // bound: once they reach half the queue, the next drop rescans and + // clears them, while live waiters stay parked. + let pool = pool_no_timer::, KeyImpl>(); + let key = host_key("foo"); + + let mut live = Vec::new(); + for _ in 0..10 { + let mut checkout = pool.checkout(key.clone()); + PollOnce(&mut checkout).await; + live.push(checkout); + } + assert_eq!(pool.locked().waiters.get(&key).unwrap().len(), 10); + + for _ in 0..5 { + let mut checkout = pool.checkout(key.clone()); + PollOnce(&mut checkout).await; + drop(checkout); + } + // Not yet half the queue, so the stale senders are still parked: + // dropping stays lazy and cheap. + assert_eq!(pool.locked().waiters.get(&key).unwrap().len(), 15); + + for _ in 0..15 { + let mut checkout = pool.checkout(key.clone()); + PollOnce(&mut checkout).await; + drop(checkout); + } + // The rescan fired once the stale senders reached half the queue, + // so only the 10 live waiters remain. + assert_eq!(pool.locked().waiters.get(&key).unwrap().len(), 10); + + // And the live waiters still get their connection. + drop(pool.pooled(c(key.clone()), Uniq(7))); + match live.remove(0).await { + Ok(pooled) => assert_eq!(*pooled, Uniq(7)), + Err(_) => panic!("not ready"), + } + } + #[derive(Debug)] struct CanClose { #[allow(unused)]