Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_utils::create_test_accounts pre-allocates an append vec first #29336

Merged
merged 2 commits into from
Dec 21, 2022
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
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>(
}
}

/// 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
49 changes: 48 additions & 1 deletion runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9459,6 +9459,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 Expand Up @@ -16212,7 +16259,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