Skip to content
Merged
Show file tree
Hide file tree
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
44 changes: 42 additions & 2 deletions vote/benches/vote_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use {
rand::Rng,
solana_account::AccountSharedData,
solana_pubkey::Pubkey,
solana_vote::vote_account::VoteAccount,
solana_vote::vote_account::{VoteAccount, VoteAccounts},
solana_vote_interface::state::{VoteInit, VoteStateV4, VoteStateVersions},
std::{collections::HashMap, sync::Arc},
};

fn new_rand_vote_account<R: Rng>(
Expand Down Expand Up @@ -56,5 +57,44 @@ fn bench_vote_account_try_from(b: &mut Bencher) {
});
}

benchmark_group!(benches, bench_vote_account_try_from);
fn bench_staked_nodes_compute(b: &mut Bencher) {
let mut rng = rand::thread_rng();
let mut vote_accounts_map = HashMap::new();

// Create a scenario with ~400 vote accounts
// Each vote account has one node_pubkey and receives delegated stake
// The stake represents the total from multiple stake accounts delegating to it
let num_vote_accounts = 400;

for _ in 0..num_vote_accounts {
let (account, _) = new_rand_vote_account(&mut rng, None);
let vote_account = VoteAccount::try_from(account).unwrap();
// Stake amount represents total delegated stake (from multiple stake accounts)
let stake: u64 = rng.gen_range(1_000_000..100_000_000);
vote_accounts_map.insert(Pubkey::new_unique(), (stake, vote_account));
}

// Add some zero-stake accounts (vote accounts with no delegations)
for _ in 0..50 {
let (account, _) = new_rand_vote_account(&mut rng, None);
let vote_account = VoteAccount::try_from(account).unwrap();
vote_accounts_map.insert(Pubkey::new_unique(), (0, vote_account));
}

let vote_accounts_map = Arc::new(vote_accounts_map);

// Benchmark measures only the staked_nodes() computation
// Create new VoteAccounts each iteration to bypass OnceLock cache
b.iter(|| {
let vote_accounts = VoteAccounts::from(Arc::clone(&vote_accounts_map));
let staked_nodes = vote_accounts.staked_nodes();
assert!(!staked_nodes.is_empty());
});
}

benchmark_group!(
benches,
bench_vote_account_try_from,
bench_staked_nodes_compute
);
benchmark_main!(benches);
27 changes: 16 additions & 11 deletions vote/src/vote_account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use {
crate::vote_state_view::VoteStateView,
itertools::Itertools,
serde::{
de::{MapAccess, Visitor},
ser::Serializer,
Expand Down Expand Up @@ -140,16 +139,22 @@ impl VoteAccounts {
pub fn staked_nodes(&self) -> Arc<HashMap</*node_pubkey:*/ Pubkey, /*stake:*/ u64>> {
self.staked_nodes
.get_or_init(|| {
Arc::new(
self.vote_accounts
.values()
.filter(|(stake, _)| *stake != 0u64)
.map(|(stake, vote_account)| (*vote_account.node_pubkey(), stake))
.into_grouping_map()
.aggregate(|acc, _node_pubkey, stake| {
Some(acc.unwrap_or_default() + stake)
}),
)
// Count non-zero stake accounts for optimal capacity allocation
let non_zero_count = self
.vote_accounts
.values()
.filter(|(stake, _)| *stake != 0)
.count();

let mut staked_nodes = HashMap::with_capacity(non_zero_count);

for (stake, vote_account) in self.vote_accounts.values() {
if *stake != 0 {
*staked_nodes.entry(*vote_account.node_pubkey()).or_default() += *stake;
}
}

Arc::new(staked_nodes)
})
.clone()
}
Expand Down
Loading