Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

banking_stage: track VoteStorage size instead of costly iterator #743

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions core/src/banking_stage/latest_unprocessed_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use {
std::{
collections::HashMap,
ops::DerefMut,
sync::{Arc, RwLock},
sync::{
atomic::{AtomicUsize, Ordering},
Arc, RwLock,
},
},
};

Expand Down Expand Up @@ -144,21 +147,16 @@ pub(crate) struct VoteBatchInsertionMetrics {
#[derive(Debug, Default)]
pub struct LatestUnprocessedVotes {
latest_votes_per_pubkey: RwLock<HashMap<Pubkey, Arc<RwLock<LatestValidatorVotePacket>>>>,
num_unprocessed_votes: AtomicUsize,
}

impl LatestUnprocessedVotes {
pub fn new() -> Self {
Self::default()
}

/// Expensive because this involves iterating through and locking every unprocessed vote
pub fn len(&self) -> usize {
self.latest_votes_per_pubkey
.read()
.unwrap()
.values()
.filter(|lock| !lock.read().unwrap().is_vote_taken())
.count()
self.num_unprocessed_votes.load(Ordering::Relaxed)
}

pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -220,6 +218,7 @@ impl LatestUnprocessedVotes {
if slot > latest_slot || ((slot == latest_slot) && (timestamp > latest_timestamp)) {
let old_vote = std::mem::replace(latest_vote.deref_mut(), vote);
if old_vote.is_vote_taken() {
self.num_unprocessed_votes.fetch_add(1, Ordering::Relaxed);
return None;
} else {
return Some(old_vote);
Expand All @@ -233,6 +232,7 @@ impl LatestUnprocessedVotes {
// and when a new vote account starts voting.
let mut latest_votes_per_pubkey = self.latest_votes_per_pubkey.write().unwrap();
latest_votes_per_pubkey.insert(pubkey, Arc::new(RwLock::new(vote)));
self.num_unprocessed_votes.fetch_add(1, Ordering::Relaxed);
None
}

Expand Down Expand Up @@ -319,7 +319,10 @@ impl LatestUnprocessedVotes {
.filter_map(|pubkey| {
self.get_entry(pubkey).and_then(|lock| {
let mut latest_vote = lock.write().unwrap();
latest_vote.take_vote()
latest_vote.take_vote().map(|vote| {
self.num_unprocessed_votes.fetch_sub(1, Ordering::Relaxed);
vote
})
})
})
.collect_vec()
Expand All @@ -335,8 +338,8 @@ impl LatestUnprocessedVotes {
.filter(|lock| lock.read().unwrap().is_forwarded())
.for_each(|lock| {
let mut vote = lock.write().unwrap();
if vote.is_forwarded() {
vote.take_vote();
if vote.is_forwarded() && vote.take_vote().is_some() {
self.num_unprocessed_votes.fetch_sub(1, Ordering::Relaxed);
}
});
}
Expand Down Expand Up @@ -726,6 +729,9 @@ mod tests {
let mut latest_vote = lock.write().unwrap();
if !latest_vote.is_vote_taken() {
latest_vote.take_vote();
latest_unprocessed_votes_tpu
.num_unprocessed_votes
.fetch_sub(1, Ordering::Relaxed);
}
});
}
Expand Down
Loading