Perf: Store deserialized sysvars in the sysvars cache (#22455)
* Perf: Store deserialized sysvars in sysvars cache * add bench
This commit is contained in:
@ -17,6 +17,10 @@ use {
|
||||
signature::{Keypair, Signer},
|
||||
transaction::Transaction,
|
||||
},
|
||||
solana_vote_program::{
|
||||
vote_instruction,
|
||||
vote_state::{Vote, VoteInit},
|
||||
},
|
||||
std::{sync::Arc, thread::sleep, time::Duration},
|
||||
test::Bencher,
|
||||
};
|
||||
@ -62,6 +66,52 @@ pub fn create_builtin_transactions(
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn create_vote_transactions(
|
||||
bank_client: &BankClient,
|
||||
mint_keypair: &Keypair,
|
||||
) -> Vec<Transaction> {
|
||||
let blockhash = bank_client.get_latest_blockhash().unwrap();
|
||||
(0..4096)
|
||||
.map(|_| {
|
||||
// Seed the signer account
|
||||
let payer_keypair = Keypair::new();
|
||||
bank_client
|
||||
.transfer_and_confirm(27_000_000, mint_keypair, &payer_keypair.pubkey())
|
||||
.unwrap_or_else(|_| panic!("{}:{}", line!(), file!()));
|
||||
|
||||
// Setup vote
|
||||
let vote_keypair = Keypair::new();
|
||||
let instructions = vote_instruction::create_account(
|
||||
&payer_keypair.pubkey(),
|
||||
&vote_keypair.pubkey(),
|
||||
&VoteInit {
|
||||
node_pubkey: payer_keypair.pubkey(),
|
||||
authorized_voter: payer_keypair.pubkey(),
|
||||
authorized_withdrawer: payer_keypair.pubkey(),
|
||||
commission: 100u8,
|
||||
},
|
||||
26_858_640,
|
||||
);
|
||||
let message = Message::new(&instructions, Some(&payer_keypair.pubkey()));
|
||||
bank_client
|
||||
.send_and_confirm_message(&[&payer_keypair, &vote_keypair], message)
|
||||
.unwrap();
|
||||
|
||||
let vote_ix = vote_instruction::vote(
|
||||
&vote_keypair.pubkey(),
|
||||
&payer_keypair.pubkey(),
|
||||
Vote {
|
||||
slots: vec![0],
|
||||
hash: blockhash,
|
||||
timestamp: None,
|
||||
},
|
||||
);
|
||||
let message = Message::new(&[vote_ix], Some(&payer_keypair.pubkey()));
|
||||
Transaction::new(&[&payer_keypair], message, blockhash)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn create_native_loader_transactions(
|
||||
bank_client: &BankClient,
|
||||
mint_keypair: &Keypair,
|
||||
@ -123,9 +173,14 @@ fn do_bench_transactions(
|
||||
) {
|
||||
solana_logger::setup();
|
||||
let ns_per_s = 1_000_000_000;
|
||||
let (mut genesis_config, mint_keypair) = create_genesis_config(100_000_000);
|
||||
let (mut genesis_config, mint_keypair) = create_genesis_config(100_000_000_000_000);
|
||||
genesis_config.ticks_per_slot = 100;
|
||||
let mut bank = Bank::new_for_benches(&genesis_config);
|
||||
|
||||
let bank = Bank::new_for_benches(&genesis_config);
|
||||
// freeze bank so that slot hashes is populated
|
||||
bank.freeze();
|
||||
|
||||
let mut bank = Bank::new_from_parent(&Arc::new(bank), &Pubkey::default(), 1);
|
||||
bank.add_builtin(
|
||||
"builtin_program",
|
||||
&Pubkey::new(&BUILTIN_PROGRAM_ID),
|
||||
@ -166,6 +221,12 @@ fn bench_bank_sync_process_native_loader_transactions(bencher: &mut Bencher) {
|
||||
do_bench_transactions(bencher, &sync_bencher, &create_native_loader_transactions);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
#[ignore]
|
||||
fn bench_bank_sync_process_vote_transactions(bencher: &mut Bencher) {
|
||||
do_bench_transactions(bencher, &sync_bencher, &create_vote_transactions);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
#[ignore]
|
||||
fn bench_bank_async_process_builtin_transactions(bencher: &mut Bencher) {
|
||||
|
@ -1439,7 +1439,7 @@ impl Bank {
|
||||
bank.update_rent();
|
||||
bank.update_epoch_schedule();
|
||||
bank.update_recent_blockhashes();
|
||||
bank.fill_sysvar_cache();
|
||||
bank.fill_missing_sysvar_cache_entries();
|
||||
bank
|
||||
}
|
||||
|
||||
@ -1626,7 +1626,7 @@ impl Bank {
|
||||
new.update_stake_history(Some(parent_epoch));
|
||||
new.update_clock(Some(parent_epoch));
|
||||
new.update_fees();
|
||||
new.fill_sysvar_cache();
|
||||
new.fill_missing_sysvar_cache_entries();
|
||||
|
||||
time.stop();
|
||||
|
||||
@ -1697,7 +1697,7 @@ impl Bank {
|
||||
new.inherit_specially_retained_account_fields(account),
|
||||
)
|
||||
});
|
||||
|
||||
new.fill_missing_sysvar_cache_entries();
|
||||
new.freeze();
|
||||
new
|
||||
}
|
||||
@ -1954,10 +1954,6 @@ impl Bank {
|
||||
}
|
||||
|
||||
self.store_account_and_update_capitalization(pubkey, &new_account);
|
||||
|
||||
// Update the entry in the cache
|
||||
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
||||
sysvar_cache.update_entry(pubkey, &new_account);
|
||||
}
|
||||
|
||||
fn inherit_specially_retained_account_fields(
|
||||
@ -2077,6 +2073,10 @@ impl Bank {
|
||||
self.inherit_specially_retained_account_fields(account),
|
||||
)
|
||||
});
|
||||
// Simply force fill sysvar cache rather than checking which sysvar was
|
||||
// actually updated since tests don't need to be optimized for performance.
|
||||
self.reset_sysvar_cache();
|
||||
self.fill_missing_sysvar_cache_entries();
|
||||
}
|
||||
|
||||
fn update_slot_history(&self) {
|
||||
@ -3624,9 +3624,13 @@ impl Bank {
|
||||
return Err(TransactionError::UnsupportedVersion);
|
||||
}
|
||||
|
||||
let slot_hashes: SlotHashes = self
|
||||
.get_cached_sysvar(&sysvar::slot_hashes::id())
|
||||
.ok_or(TransactionError::AccountNotFound)?;
|
||||
let slot_hashes = self
|
||||
.sysvar_cache
|
||||
.read()
|
||||
.unwrap()
|
||||
.get_slot_hashes()
|
||||
.map_err(|_| TransactionError::AccountNotFound)?;
|
||||
|
||||
Ok(address_table_lookups
|
||||
.iter()
|
||||
.map(|address_table_lookup| {
|
||||
|
@ -1,33 +1,149 @@
|
||||
use {
|
||||
super::Bank,
|
||||
solana_sdk::{
|
||||
account::ReadableAccount,
|
||||
pubkey::Pubkey,
|
||||
sysvar::{self, Sysvar},
|
||||
},
|
||||
solana_program_runtime::sysvar_cache::SysvarCache,
|
||||
solana_sdk::{account::ReadableAccount, sysvar::Sysvar},
|
||||
};
|
||||
|
||||
impl Bank {
|
||||
pub(crate) fn fill_sysvar_cache(&mut self) {
|
||||
pub(crate) fn fill_missing_sysvar_cache_entries(&self) {
|
||||
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
||||
for id in sysvar::ALL_IDS.iter() {
|
||||
if !sysvar_cache.iter().any(|(key, _data)| key == id) {
|
||||
if let Some(account) = self.get_account_with_fixed_root(id) {
|
||||
sysvar_cache.push_entry(*id, account.data().to_vec());
|
||||
}
|
||||
if sysvar_cache.get_clock().is_err() {
|
||||
if let Some(clock) = self.load_sysvar_account() {
|
||||
sysvar_cache.set_clock(clock);
|
||||
}
|
||||
}
|
||||
if sysvar_cache.get_epoch_schedule().is_err() {
|
||||
if let Some(epoch_schedule) = self.load_sysvar_account() {
|
||||
sysvar_cache.set_epoch_schedule(epoch_schedule);
|
||||
}
|
||||
}
|
||||
#[allow(deprecated)]
|
||||
if sysvar_cache.get_fees().is_err() {
|
||||
if let Some(fees) = self.load_sysvar_account() {
|
||||
sysvar_cache.set_fees(fees);
|
||||
}
|
||||
}
|
||||
if sysvar_cache.get_rent().is_err() {
|
||||
if let Some(rent) = self.load_sysvar_account() {
|
||||
sysvar_cache.set_rent(rent);
|
||||
}
|
||||
}
|
||||
if sysvar_cache.get_slot_hashes().is_err() {
|
||||
if let Some(slot_hashes) = self.load_sysvar_account() {
|
||||
sysvar_cache.set_slot_hashes(slot_hashes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the value of a cached sysvar by its id
|
||||
pub fn get_cached_sysvar<T: Sysvar>(&self, id: &Pubkey) -> Option<T> {
|
||||
let sysvar_cache = self.sysvar_cache.read().unwrap();
|
||||
sysvar_cache.iter().find_map(|(key, data)| {
|
||||
if id == key {
|
||||
bincode::deserialize(data).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
pub(crate) fn reset_sysvar_cache(&self) {
|
||||
let mut sysvar_cache = self.sysvar_cache.write().unwrap();
|
||||
*sysvar_cache = SysvarCache::default();
|
||||
}
|
||||
|
||||
fn load_sysvar_account<T: Sysvar>(&self) -> Option<T> {
|
||||
if let Some(account) = self.get_account_with_fixed_root(&T::id()) {
|
||||
bincode::deserialize(account.data()).ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use {
|
||||
super::*,
|
||||
solana_sdk::{genesis_config::create_genesis_config, pubkey::Pubkey},
|
||||
std::sync::Arc,
|
||||
};
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_sysvar_cache_initialization() {
|
||||
let (genesis_config, _mint_keypair) = create_genesis_config(100_000);
|
||||
let bank0 = Arc::new(Bank::new_for_tests(&genesis_config));
|
||||
|
||||
let bank0_sysvar_cache = bank0.sysvar_cache.read().unwrap();
|
||||
let bank0_cached_clock = bank0_sysvar_cache.get_clock();
|
||||
let bank0_cached_epoch_schedule = bank0_sysvar_cache.get_epoch_schedule();
|
||||
let bank0_cached_fees = bank0_sysvar_cache.get_fees();
|
||||
let bank0_cached_rent = bank0_sysvar_cache.get_rent();
|
||||
|
||||
assert!(bank0_cached_clock.is_ok());
|
||||
assert!(bank0_cached_epoch_schedule.is_ok());
|
||||
assert!(bank0_cached_fees.is_ok());
|
||||
assert!(bank0_cached_rent.is_ok());
|
||||
assert!(bank0
|
||||
.sysvar_cache
|
||||
.read()
|
||||
.unwrap()
|
||||
.get_slot_hashes()
|
||||
.is_err());
|
||||
|
||||
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), bank0.slot() + 1);
|
||||
|
||||
let bank1_sysvar_cache = bank1.sysvar_cache.read().unwrap();
|
||||
let bank1_cached_clock = bank1_sysvar_cache.get_clock();
|
||||
let bank1_cached_epoch_schedule = bank0_sysvar_cache.get_epoch_schedule();
|
||||
let bank1_cached_fees = bank0_sysvar_cache.get_fees();
|
||||
let bank1_cached_rent = bank0_sysvar_cache.get_rent();
|
||||
|
||||
assert!(bank1_cached_clock.is_ok());
|
||||
assert!(bank1_cached_epoch_schedule.is_ok());
|
||||
assert!(bank1_cached_fees.is_ok());
|
||||
assert!(bank1_cached_rent.is_ok());
|
||||
assert!(bank1.sysvar_cache.read().unwrap().get_slot_hashes().is_ok());
|
||||
|
||||
assert_ne!(bank0_cached_clock, bank1_cached_clock);
|
||||
assert_eq!(bank0_cached_epoch_schedule, bank1_cached_epoch_schedule);
|
||||
assert_eq!(bank0_cached_fees, bank1_cached_fees);
|
||||
assert_eq!(bank0_cached_rent, bank1_cached_rent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(deprecated)]
|
||||
fn test_reset_and_fill_sysvar_cache() {
|
||||
let (genesis_config, _mint_keypair) = create_genesis_config(100_000);
|
||||
let bank0 = Arc::new(Bank::new_for_tests(&genesis_config));
|
||||
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), bank0.slot() + 1);
|
||||
|
||||
let bank1_sysvar_cache = bank1.sysvar_cache.read().unwrap();
|
||||
let bank1_cached_clock = bank1_sysvar_cache.get_clock();
|
||||
let bank1_cached_epoch_schedule = bank1_sysvar_cache.get_epoch_schedule();
|
||||
let bank1_cached_fees = bank1_sysvar_cache.get_fees();
|
||||
let bank1_cached_rent = bank1_sysvar_cache.get_rent();
|
||||
let bank1_cached_slot_hashes = bank1_sysvar_cache.get_slot_hashes();
|
||||
|
||||
assert!(bank1_cached_clock.is_ok());
|
||||
assert!(bank1_cached_epoch_schedule.is_ok());
|
||||
assert!(bank1_cached_fees.is_ok());
|
||||
assert!(bank1_cached_rent.is_ok());
|
||||
assert!(bank1_cached_slot_hashes.is_ok());
|
||||
|
||||
drop(bank1_sysvar_cache);
|
||||
bank1.reset_sysvar_cache();
|
||||
|
||||
let bank1_sysvar_cache = bank1.sysvar_cache.read().unwrap();
|
||||
assert!(bank1_sysvar_cache.get_clock().is_err());
|
||||
assert!(bank1_sysvar_cache.get_epoch_schedule().is_err());
|
||||
assert!(bank1_sysvar_cache.get_fees().is_err());
|
||||
assert!(bank1_sysvar_cache.get_rent().is_err());
|
||||
assert!(bank1_sysvar_cache.get_slot_hashes().is_err());
|
||||
|
||||
drop(bank1_sysvar_cache);
|
||||
bank1.fill_missing_sysvar_cache_entries();
|
||||
|
||||
let bank1_sysvar_cache = bank1.sysvar_cache.read().unwrap();
|
||||
assert_eq!(bank1_sysvar_cache.get_clock(), bank1_cached_clock);
|
||||
assert_eq!(
|
||||
bank1_sysvar_cache.get_epoch_schedule(),
|
||||
bank1_cached_epoch_schedule
|
||||
);
|
||||
assert_eq!(bank1_sysvar_cache.get_fees(), bank1_cached_fees);
|
||||
assert_eq!(bank1_sysvar_cache.get_rent(), bank1_cached_rent);
|
||||
assert_eq!(
|
||||
bank1_sysvar_cache.get_slot_hashes(),
|
||||
bank1_cached_slot_hashes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user