Skip to content

Commit

Permalink
test_utils::create_test_accounts pre-allocates an append vec first
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Dec 20, 2022
1 parent 1105619 commit 664077b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
4 changes: 2 additions & 2 deletions runtime/benches/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use {
rand::Rng,
rayon::iter::{IntoParallelRefIterator, ParallelIterator},
solana_runtime::{
accounts::{test_utils::create_test_accounts, AccountAddressFilter, Accounts},
accounts_db::AccountShrinkThreshold,
accounts::{AccountAddressFilter, Accounts},
accounts_db::{test_utils::create_test_accounts, AccountShrinkThreshold},
accounts_index::{AccountSecondaryIndexes, ScanConfig},
ancestors::Ancestors,
bank::*,
Expand Down
31 changes: 0 additions & 31 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use {
DashMap,
},
log::*,
rand::{thread_rng, Rng},
solana_address_lookup_table_program::{error::AddressLookupError, state::AddressLookupTable},
solana_program_runtime::compute_budget::ComputeBudget,
solana_sdk::{
Expand Down Expand Up @@ -1462,36 +1461,6 @@ fn prepare_if_nonce_account<'a>(
}
}

/// A set of utility functions used for testing and benchmarking
pub mod test_utils {
use super::*;

pub fn create_test_accounts(
accounts: &Accounts,
pubkeys: &mut Vec<Pubkey>,
num: usize,
slot: Slot,
) {
for t in 0..num {
let pubkey = solana_sdk::pubkey::new_rand();
let account =
AccountSharedData::new((t + 1) as u64, 0, AccountSharedData::default().owner());
accounts.store_slow_uncached(slot, &pubkey, &account);
pubkeys.push(pubkey);
}
}

// Only used by bench, not safe to call otherwise accounts can conflict with the
// accounts cache!
pub fn update_accounts_bench(accounts: &Accounts, pubkeys: &[Pubkey], slot: u64) {
for pubkey in pubkeys {
let amount = thread_rng().gen_range(0, 10);
let account = AccountSharedData::new(amount, 0, AccountSharedData::default().owner());
accounts.store_slow_uncached(slot, pubkey, &account);
}
}
}

#[cfg(test)]
mod tests {
use {
Expand Down
60 changes: 59 additions & 1 deletion runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9427,6 +9427,64 @@ impl AccountsDb {
}
}

/// A set of utility functions used for testing and benchmarking
pub mod test_utils {
use {
super::*,
crate::{accounts::Accounts, append_vec::aligned_stored_size},
};

pub fn create_test_accounts(
accounts: &Accounts,
pubkeys: &mut Vec<Pubkey>,
num: usize,
slot: Slot,
) {
let data_size = 0;
if accounts.accounts_db.storage.get_slot_stores(slot).is_none() {
let bytes_required = num * aligned_stored_size(data_size);
// allocate an append vec for this slot that can hold all the test accounts. This prevents us from creating more than 1 append vec for this slot.
_ = accounts.accounts_db.create_and_insert_store(
slot,
bytes_required as u64,
"create_test_accounts",
);
}

for t in 0..num {
let pubkey = solana_sdk::pubkey::new_rand();
let account = AccountSharedData::new(
(t + 1) as u64,
data_size,
AccountSharedData::default().owner(),
);
/*
if we have to change to using the cache
accounts.accounts_db.store_cached(
(
slot,
&[(&pubkey, &account)][..],
crate::accounts_db::INCLUDE_SLOT_IN_HASH_TESTS,
),
None,
);
*/
accounts.store_slow_uncached(slot, &pubkey, &account);
pubkeys.push(pubkey);
}
}

// Only used by bench, not safe to call otherwise accounts can conflict with the
// accounts cache!
pub fn update_accounts_bench(accounts: &Accounts, pubkeys: &[Pubkey], slot: u64) {
for pubkey in pubkeys {
let amount = thread_rng().gen_range(0, 10);
let account = AccountSharedData::new(amount, 0, AccountSharedData::default().owner());
accounts.store_slow_uncached(slot, pubkey, &account);
}
}
}

#[cfg(test)]
pub mod tests {
use {
Expand Down Expand Up @@ -16180,7 +16238,7 @@ pub mod tests {
}

/// callers use to call store_uncached. But, this is not allowed anymore.
fn store_for_tests(&self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)]) {
pub fn store_for_tests(&self, slot: Slot, accounts: &[(&Pubkey, &AccountSharedData)]) {
self.store(
(slot, accounts, INCLUDE_SLOT_IN_HASH_TESTS),
true,
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/serde_snapshot/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use {
super::*,
crate::{
account_storage::AccountStorageMap,
accounts::{test_utils::create_test_accounts, Accounts},
accounts_db::{get_temp_accounts_paths, AccountShrinkThreshold},
accounts::Accounts,
accounts_db::{
get_temp_accounts_paths, test_utils::create_test_accounts, AccountShrinkThreshold,
},
accounts_hash::AccountsHash,
append_vec::AppendVec,
bank::{Bank, BankTestConfig},
Expand Down

0 comments on commit 664077b

Please sign in to comment.