@@ -37,7 +37,9 @@ use crate::ln::types::ChannelId;
3737use crate :: routing:: utxo:: { self , UtxoLookup , UtxoResolver } ;
3838use crate :: types:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
3939use crate :: types:: string:: PrintableString ;
40- use crate :: util:: indexed_map:: { Entry as IndexedMapEntry , IndexedMap } ;
40+ use crate :: util:: indexed_map:: {
41+ Entry as IndexedMapEntry , IndexedMap , OccupiedEntry as IndexedMapOccupiedEntry ,
42+ } ;
4143use crate :: util:: logger:: { Level , Logger } ;
4244use crate :: util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
4345use crate :: util:: ser:: { MaybeReadable , Readable , ReadableArgs , RequiredWrapper , Writeable , Writer } ;
@@ -2356,9 +2358,7 @@ where
23562358 return ;
23572359 }
23582360 let min_time_unix: u32 = ( current_time_unix - STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS ) as u32 ;
2359- // Sadly BTreeMap::retain was only stabilized in 1.53 so we can't switch to it for some
2360- // time.
2361- let mut scids_to_remove = Vec :: new ( ) ;
2361+ let mut scids_to_remove = new_hash_set ( ) ;
23622362 for ( scid, info) in channels. unordered_iter_mut ( ) {
23632363 if info. one_to_two . is_some ( )
23642364 && info. one_to_two . as_ref ( ) . unwrap ( ) . last_update < min_time_unix
@@ -2382,19 +2382,24 @@ where
23822382 if announcement_received_timestamp < min_time_unix as u64 {
23832383 log_gossip ! ( self . logger, "Removing channel {} because both directional updates are missing and its announcement timestamp {} being below {}" ,
23842384 scid, announcement_received_timestamp, min_time_unix) ;
2385- scids_to_remove. push ( * scid) ;
2385+ scids_to_remove. insert ( * scid) ;
23862386 }
23872387 }
23882388 }
23892389 if !scids_to_remove. is_empty ( ) {
23902390 let mut nodes = self . nodes . write ( ) . unwrap ( ) ;
2391- for scid in scids_to_remove {
2392- let info = channels
2393- . remove ( & scid)
2394- . expect ( "We just accessed this scid, it should be present" ) ;
2395- self . remove_channel_in_nodes ( & mut nodes, & info, scid) ;
2396- self . removed_channels . lock ( ) . unwrap ( ) . insert ( scid, Some ( current_time_unix) ) ;
2391+ let mut removed_channels_lck = self . removed_channels . lock ( ) . unwrap ( ) ;
2392+
2393+ let channels_removed_bulk = channels. remove_fetch_bulk ( & scids_to_remove) ;
2394+ self . removed_node_counters . lock ( ) . unwrap ( ) . reserve ( channels_removed_bulk. len ( ) ) ;
2395+ let mut nodes_to_remove = hash_set_with_capacity ( channels_removed_bulk. len ( ) ) ;
2396+ for ( scid, info) in channels_removed_bulk {
2397+ self . remove_channel_in_nodes_callback ( & mut nodes, & info, scid, |e| {
2398+ nodes_to_remove. insert ( * e. key ( ) ) ;
2399+ } ) ;
2400+ removed_channels_lck. insert ( scid, Some ( current_time_unix) ) ;
23972401 }
2402+ nodes. remove_bulk ( & nodes_to_remove) ;
23982403 }
23992404
24002405 let should_keep_tracking = |time : & mut Option < u64 > | {
@@ -2633,16 +2638,17 @@ where
26332638 Ok ( ( ) )
26342639 }
26352640
2636- fn remove_channel_in_nodes (
2641+ fn remove_channel_in_nodes_callback < RM : FnMut ( IndexedMapOccupiedEntry < NodeId , NodeInfo > ) > (
26372642 & self , nodes : & mut IndexedMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ,
2643+ mut remove_node : RM ,
26382644 ) {
26392645 macro_rules! remove_from_node {
26402646 ( $node_id: expr) => {
26412647 if let IndexedMapEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
26422648 entry. get_mut( ) . channels. retain( |chan_id| short_channel_id != * chan_id) ;
26432649 if entry. get( ) . channels. is_empty( ) {
26442650 self . removed_node_counters. lock( ) . unwrap( ) . push( entry. get( ) . node_counter) ;
2645- entry . remove_entry ( ) ;
2651+ remove_node ( entry ) ;
26462652 }
26472653 } else {
26482654 panic!(
@@ -2655,6 +2661,14 @@ where
26552661 remove_from_node ! ( chan. node_one) ;
26562662 remove_from_node ! ( chan. node_two) ;
26572663 }
2664+
2665+ fn remove_channel_in_nodes (
2666+ & self , nodes : & mut IndexedMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ,
2667+ ) {
2668+ self . remove_channel_in_nodes_callback ( nodes, chan, short_channel_id, |e| {
2669+ e. remove_entry ( ) ;
2670+ } ) ;
2671+ }
26582672}
26592673
26602674impl ReadOnlyNetworkGraph < ' _ > {
0 commit comments