@@ -37,7 +37,9 @@ use crate::ln::types::ChannelId;
37
37
use crate :: routing:: utxo:: { self , UtxoLookup , UtxoResolver } ;
38
38
use crate :: types:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
39
39
use 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
+ } ;
41
43
use crate :: util:: logger:: { Level , Logger } ;
42
44
use crate :: util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
43
45
use crate :: util:: ser:: { MaybeReadable , Readable , ReadableArgs , RequiredWrapper , Writeable , Writer } ;
@@ -2356,9 +2358,7 @@ where
2356
2358
return ;
2357
2359
}
2358
2360
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 ( ) ;
2362
2362
for ( scid, info) in channels. unordered_iter_mut ( ) {
2363
2363
if info. one_to_two . is_some ( )
2364
2364
&& info. one_to_two . as_ref ( ) . unwrap ( ) . last_update < min_time_unix
@@ -2382,19 +2382,24 @@ where
2382
2382
if announcement_received_timestamp < min_time_unix as u64 {
2383
2383
log_gossip ! ( self . logger, "Removing channel {} because both directional updates are missing and its announcement timestamp {} being below {}" ,
2384
2384
scid, announcement_received_timestamp, min_time_unix) ;
2385
- scids_to_remove. push ( * scid) ;
2385
+ scids_to_remove. insert ( * scid) ;
2386
2386
}
2387
2387
}
2388
2388
}
2389
2389
if !scids_to_remove. is_empty ( ) {
2390
2390
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) ) ;
2397
2401
}
2402
+ nodes. remove_bulk ( & nodes_to_remove) ;
2398
2403
}
2399
2404
2400
2405
let should_keep_tracking = |time : & mut Option < u64 > | {
@@ -2633,16 +2638,17 @@ where
2633
2638
Ok ( ( ) )
2634
2639
}
2635
2640
2636
- fn remove_channel_in_nodes (
2641
+ fn remove_channel_in_nodes_callback < RM : FnMut ( IndexedMapOccupiedEntry < NodeId , NodeInfo > ) > (
2637
2642
& self , nodes : & mut IndexedMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ,
2643
+ mut remove_node : RM ,
2638
2644
) {
2639
2645
macro_rules! remove_from_node {
2640
2646
( $node_id: expr) => {
2641
2647
if let IndexedMapEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
2642
2648
entry. get_mut( ) . channels. retain( |chan_id| short_channel_id != * chan_id) ;
2643
2649
if entry. get( ) . channels. is_empty( ) {
2644
2650
self . removed_node_counters. lock( ) . unwrap( ) . push( entry. get( ) . node_counter) ;
2645
- entry . remove_entry ( ) ;
2651
+ remove_node ( entry ) ;
2646
2652
}
2647
2653
} else {
2648
2654
panic!(
@@ -2655,6 +2661,14 @@ where
2655
2661
remove_from_node ! ( chan. node_one) ;
2656
2662
remove_from_node ! ( chan. node_two) ;
2657
2663
}
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
+ }
2658
2672
}
2659
2673
2660
2674
impl ReadOnlyNetworkGraph < ' _ > {
0 commit comments