Skip to content

Commit 1325bf6

Browse files
Initialize list_channels_with_filter result vec with capacity
1 parent 619be66 commit 1325bf6

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,14 +1616,13 @@ where
16161616
}
16171617

16181618
fn list_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<<SP::Target as SignerProvider>::Signer>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
1619-
let mut res = Vec::new();
16201619
// Allocate our best estimate of the number of channels we have in the `res`
16211620
// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
16221621
// a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
16231622
// of the ChannelMonitor handling. Therefore reallocations may still occur, but is
16241623
// unlikely as the `short_to_chan_info` map often contains 2 entries for
16251624
// the same channel.
1626-
res.reserve(self.short_to_chan_info.read().unwrap().len());
1625+
let mut res = Vec::with_capacity(self.short_to_chan_info.read().unwrap().len());
16271626
{
16281627
let best_block_height = self.best_block.read().unwrap().height();
16291628
let per_peer_state = self.per_peer_state.read().unwrap();
@@ -3401,38 +3400,6 @@ where
34013400
true
34023401
}
34033402

3404-
/// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3405-
/// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
3406-
/// to that peer is later closed while still being disconnected (i.e. force closed), we
3407-
/// therefore need to remove the peer from `peer_state` separately.
3408-
/// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
3409-
/// instead remove such peers awaiting removal through this function, which is called on a
3410-
/// timer through `timer_tick_occurred`, passing the peers disconnected peers with no channels,
3411-
/// to limit the negative effects on parallelism as much as possible.
3412-
///
3413-
/// Must be called without the `per_peer_state` lock acquired.
3414-
fn remove_peers_awaiting_removal(&self, pending_peers_awaiting_removal: HashSet<PublicKey>) {
3415-
if pending_peers_awaiting_removal.len() > 0 {
3416-
let mut per_peer_state = self.per_peer_state.write().unwrap();
3417-
for counterparty_node_id in pending_peers_awaiting_removal {
3418-
match per_peer_state.entry(counterparty_node_id) {
3419-
hash_map::Entry::Occupied(entry) => {
3420-
// Remove the entry if the peer is still disconnected and we still
3421-
// have no channels to the peer.
3422-
let remove_entry = {
3423-
let peer_state = entry.get().lock().unwrap();
3424-
!peer_state.is_connected && peer_state.channel_by_id.len() == 0
3425-
};
3426-
if remove_entry {
3427-
entry.remove_entry();
3428-
}
3429-
},
3430-
hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */ }
3431-
}
3432-
}
3433-
}
3434-
}
3435-
34363403
#[cfg(any(test, feature = "_test_utils"))]
34373404
/// Process background events, for functional testing
34383405
pub fn test_process_background_events(&self) {
@@ -3506,7 +3473,7 @@ where
35063473

35073474
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
35083475
let mut timed_out_mpp_htlcs = Vec::new();
3509-
let mut pending_peers_awaiting_removal = HashSet::new();
3476+
let mut pending_peers_awaiting_removal = Vec::new();
35103477
{
35113478
let per_peer_state = self.per_peer_state.read().unwrap();
35123479
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
@@ -3556,11 +3523,37 @@ where
35563523
});
35573524
let peer_should_be_removed = !peer_state.is_connected && peer_state.channel_by_id.len() == 0;
35583525
if peer_should_be_removed {
3559-
pending_peers_awaiting_removal.insert(counterparty_node_id);
3526+
pending_peers_awaiting_removal.push(counterparty_node_id);
3527+
}
3528+
}
3529+
}
3530+
3531+
// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3532+
// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels
3533+
// of to that peer is later closed while still being disconnected (i.e. force closed),
3534+
// we therefore need to remove the peer from `peer_state` separately.
3535+
// To avoid having to take the `per_peer_state` `write` lock once the channels are
3536+
// closed, we instead remove such peers awaiting removal here on a timer, to limit the
3537+
// negative effects on parallelism as much as possible.
3538+
if pending_peers_awaiting_removal.len() > 0 {
3539+
let mut per_peer_state = self.per_peer_state.write().unwrap();
3540+
for counterparty_node_id in pending_peers_awaiting_removal {
3541+
match per_peer_state.entry(counterparty_node_id) {
3542+
hash_map::Entry::Occupied(entry) => {
3543+
// Remove the entry if the peer is still disconnected and we still
3544+
// have no channels to the peer.
3545+
let remove_entry = {
3546+
let peer_state = entry.get().lock().unwrap();
3547+
!peer_state.is_connected && peer_state.channel_by_id.len() == 0
3548+
};
3549+
if remove_entry {
3550+
entry.remove_entry();
3551+
}
3552+
},
3553+
hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */ }
35603554
}
35613555
}
35623556
}
3563-
self.remove_peers_awaiting_removal(pending_peers_awaiting_removal);
35643557

35653558
self.claimable_payments.lock().unwrap().claimable_htlcs.retain(|payment_hash, (_, htlcs)| {
35663559
if htlcs.is_empty() {

0 commit comments

Comments
 (0)