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
…29336)

* test_utils::create_test_accounts pre-allocates an append vec first

* remove comment
  • Loading branch information
jeffwashington committed Dec 21, 2022
1 parent 0244b01 commit 4a64f6d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
6 changes: 3 additions & 3 deletions accounts-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use {
rayon::prelude::*,
solana_measure::measure::Measure,
solana_runtime::{
accounts::{
accounts::Accounts,
accounts_db::{
test_utils::{create_test_accounts, update_accounts_bench},
Accounts,
AccountShrinkThreshold, CalcAccountsHashDataSource,
},
accounts_db::{AccountShrinkThreshold, CalcAccountsHashDataSource},
accounts_index::AccountSecondaryIndexes,
ancestors::Ancestors,
rent_collector::RentCollector,
Expand Down
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 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
47 changes: 47 additions & 0 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9427,6 +9427,53 @@ 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(),
);
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
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 4a64f6d

Please sign in to comment.