From 669a4b829dff2c5a4affc23f2dad2c0d641c910c Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sun, 12 Apr 2020 22:03:07 +0200 Subject: [PATCH] split_off channged to retain This changes `split_off`, which does one allocation and copies `n` items in the best case, `2 * n` items in the worst case to `retain`, which does zero allocations and copies zero items in the best case and `3 * (n - 1)` items in worst case (because swap is 3 copies and `retain` has to swap due to possibility of panic). I believe this should be net-benefit due to usually small `n`. --- src/rpc.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index 89423c5..ffb6fff 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -497,14 +497,15 @@ impl RPC { let mut senders = senders.lock().unwrap(); match msg { Notification::Periodic => { - for sender in senders.split_off(0) { + senders.retain(|sender| { if let Err(TrySendError::Disconnected(_)) = sender.try_send(Message::PeriodicUpdate) { - continue; + false // drop disconnected clients + } else { + true } - senders.push(sender); - } + }) } Notification::Exit => acceptor.send(None).unwrap(), // mark acceptor as done }