Skip to content

Commit

Permalink
rpc: Avoid unnecessary collect() calls
Browse files Browse the repository at this point in the history
In these cases, it's better to lazily iterate.
  • Loading branch information
vadorovsky committed Jan 29, 2025
1 parent fe4b325 commit 9a75ff2
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions rpc/src/cluster_tpu_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ impl TpuInfo for ClusterTpuInfo {

fn get_leader_tpus(&self, max_count: u64, protocol: Protocol) -> Vec<&SocketAddr> {
let recorder = self.poh_recorder.read().unwrap();
let leaders: Vec<_> = (0..max_count)
.filter_map(|i| recorder.leader_after_n_slots(i * NUM_CONSECUTIVE_LEADER_SLOTS))
.collect();
let leaders = (0..max_count)
.filter_map(|i| recorder.leader_after_n_slots(i * NUM_CONSECUTIVE_LEADER_SLOTS));
drop(recorder);
let mut unique_leaders = vec![];
for leader in leaders.iter() {
if let Some(addr) = self.recent_peers.get(leader).map(|addr| match protocol {
for leader in leaders {
if let Some(addr) = self.recent_peers.get(&leader).map(|addr| match protocol {
Protocol::UDP => &addr.0,
Protocol::QUIC => &addr.1,
}) {
Expand All @@ -73,19 +72,15 @@ impl TpuInfo for ClusterTpuInfo {
protocol: Protocol,
) -> Vec<(&SocketAddr, Slot)> {
let recorder = self.poh_recorder.read().unwrap();
let leaders: Vec<_> = (0..max_count)
.rev()
.filter_map(|future_slot| {
NUM_CONSECUTIVE_LEADER_SLOTS
.checked_mul(future_slot)
.and_then(|slots_in_the_future| {
recorder.leader_and_slot_after_n_slots(slots_in_the_future)
})
})
.collect();
let leaders: Vec<_> = (0..max_count).rev().filter_map(|future_slot| {
NUM_CONSECUTIVE_LEADER_SLOTS
.checked_mul(future_slot)
.and_then(|slots_in_the_future| {
recorder.leader_and_slot_after_n_slots(slots_in_the_future)
})
});
drop(recorder);
let addrs_to_slots = leaders
.into_iter()
.filter_map(|(leader_id, leader_slot)| {
self.recent_peers
.get(&leader_id)
Expand Down

0 comments on commit 9a75ff2

Please sign in to comment.