File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -41,27 +41,19 @@ impl PeersIter {
4141 }
4242 }
4343
44- // Sort peer ids by ascending affinity
44+ // Sort peer ids by descending affinity
4545 self . peer_ids
46- . sort_unstable_by_key ( |( affinity, _) | * affinity) ;
46+ . sort_unstable_by_key ( |( affinity, _) | std :: cmp :: Reverse ( * affinity) ) ;
4747
4848 if let Some ( batch_len) = batch_len {
49- // Remove peers which we don't need. Iterate from the the biggest affinity
50- let mut iter = self . peer_ids . iter ( ) . rev ( ) ;
51- if let Some ( ( top_affinity, _) ) = iter. next ( ) {
52- let mut remaining_count = 0 ;
53-
54- // Leave only peers with the same affinity, or at least `max_tasks` of them
55- for ( affinity, _) in iter {
56- if * affinity >= * top_affinity || remaining_count < batch_len {
57- remaining_count += 1 ;
58- } else {
59- break ;
60- }
61- }
62-
63- // Remove prefix
64- self . peer_ids . drain ( ..self . peer_ids . len ( ) - remaining_count) ;
49+ if let Some ( top_affinity) = self . peer_ids . first ( ) . map ( |( affinity, _) | * affinity) {
50+ let mut offset = 0usize ;
51+ tracing:: trace!( top_affinity, batch_len, "clearing peer ids" ) ;
52+ self . peer_ids . retain ( |( affinity, _) | {
53+ let retain = offset < batch_len || * affinity >= top_affinity;
54+ i += 1 ;
55+ retain
56+ } ) ;
6557 }
6658 }
6759 }
You can’t perform that action at this time.
0 commit comments