Perf: Store deserialized sysvars in the sysvars cache (#22455)
* Perf: Store deserialized sysvars in sysvars cache * add bench
This commit is contained in:
@@ -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