General cleanup (Part 2) (#32998)

* bucket-map: remove unused `mut`

* remove unnecessary explicit `into_iter()` calls

* don't default-construct unit-structs

* prefer `or_default()` to `or_insert_with(T::default())`

* replace "slow zero-filled vec initialization"

---------

Co-authored-by: Trent Nelson <trent@solana.com>
This commit is contained in:
Alexander Meißner 2023-08-25 19:07:38 +02:00 committed by GitHub
parent 9070d780a3
commit 29bbda0c11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 31 deletions

View File

@ -183,7 +183,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
/// if entry does not exist, return just the index of an empty entry appropriate for this key /// if entry does not exist, return just the index of an empty entry appropriate for this key
/// returns (existing entry, index of the found or empty entry) /// returns (existing entry, index of the found or empty entry)
fn find_index_entry_mut( fn find_index_entry_mut(
index: &mut BucketStorage<IndexBucket<T>>, index: &BucketStorage<IndexBucket<T>>,
key: &Pubkey, key: &Pubkey,
random: u64, random: u64,
) -> Result<(Option<IndexEntryPlaceInBucket<T>>, u64), BucketMapError> { ) -> Result<(Option<IndexEntryPlaceInBucket<T>>, u64), BucketMapError> {
@ -410,7 +410,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0))); return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0)));
} }
let max_search = self.index.max_search(); let max_search = self.index.max_search();
let (elem, elem_ix) = Self::find_index_entry_mut(&mut self.index, key, self.random)?; let (elem, elem_ix) = Self::find_index_entry_mut(&self.index, key, self.random)?;
let elem = if let Some(elem) = elem { let elem = if let Some(elem) = elem {
elem elem
} else { } else {

View File

@ -443,7 +443,7 @@ impl LoadedPrograms {
key: Pubkey, key: Pubkey,
entry: Arc<LoadedProgram>, entry: Arc<LoadedProgram>,
) -> (bool, Arc<LoadedProgram>) { ) -> (bool, Arc<LoadedProgram>) {
let second_level = self.entries.entry(key).or_insert_with(Vec::new); let second_level = self.entries.entry(key).or_default();
let index = second_level let index = second_level
.iter() .iter()
.position(|at| at.effective_slot >= entry.effective_slot); .position(|at| at.effective_slot >= entry.effective_slot);

View File

@ -621,8 +621,7 @@ mod tests {
fn create_default_recent_blockhashes_account() -> AccountSharedData { fn create_default_recent_blockhashes_account() -> AccountSharedData {
#[allow(deprecated)] #[allow(deprecated)]
recent_blockhashes_account::create_account_with_data_for_test( recent_blockhashes_account::create_account_with_data_for_test(
vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES] vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES],
.into_iter(),
) )
} }
fn create_default_rent_account() -> AccountSharedData { fn create_default_rent_account() -> AccountSharedData {
@ -1577,8 +1576,7 @@ mod tests {
#[allow(deprecated)] #[allow(deprecated)]
let new_recent_blockhashes_account = let new_recent_blockhashes_account =
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES] vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES],
.into_iter(),
); );
mock_process_instruction( mock_process_instruction(
&system_program::id(), &system_program::id(),
@ -1863,9 +1861,7 @@ mod tests {
let blockhash_id = sysvar::recent_blockhashes::id(); let blockhash_id = sysvar::recent_blockhashes::id();
#[allow(deprecated)] #[allow(deprecated)]
let new_recent_blockhashes_account = let new_recent_blockhashes_account =
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]);
vec![].into_iter(),
);
process_instruction( process_instruction(
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(), &serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
vec![ vec![
@ -1928,9 +1924,7 @@ mod tests {
); );
#[allow(deprecated)] #[allow(deprecated)]
let new_recent_blockhashes_account = let new_recent_blockhashes_account =
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]);
vec![].into_iter(),
);
mock_process_instruction( mock_process_instruction(
&system_program::id(), &system_program::id(),
Vec::new(), Vec::new(),

View File

@ -516,7 +516,7 @@ where
(SerializableBankAndStorage::<newer::Context> { (SerializableBankAndStorage::<newer::Context> {
bank, bank,
snapshot_storages: storage, snapshot_storages: storage,
phantom: std::marker::PhantomData::default(), phantom: std::marker::PhantomData,
}) })
.serialize(s) .serialize(s)
} }

View File

@ -97,7 +97,7 @@ mod tests {
#[test] #[test]
fn test_create_account_empty() { fn test_create_account_empty() {
let account = create_account_with_data_for_test(vec![].into_iter()); let account = create_account_with_data_for_test(vec![]);
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap(); let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
assert_eq!(recent_blockhashes, RecentBlockhashes::default()); assert_eq!(recent_blockhashes, RecentBlockhashes::default());
} }
@ -106,9 +106,14 @@ mod tests {
fn test_create_account_full() { fn test_create_account_full() {
let def_hash = Hash::default(); let def_hash = Hash::default();
let def_lamports_per_signature = 0; let def_lamports_per_signature = 0;
let account = create_account_with_data_for_test( let account = create_account_with_data_for_test(vec![
vec![IterItem(0u64, &def_hash, def_lamports_per_signature); MAX_ENTRIES].into_iter(), IterItem(
); 0u64,
&def_hash,
def_lamports_per_signature
);
MAX_ENTRIES
]);
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap(); let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
} }
@ -117,10 +122,14 @@ mod tests {
fn test_create_account_truncate() { fn test_create_account_truncate() {
let def_hash = Hash::default(); let def_hash = Hash::default();
let def_lamports_per_signature = 0; let def_lamports_per_signature = 0;
let account = create_account_with_data_for_test( let account = create_account_with_data_for_test(vec![
vec![IterItem(0u64, &def_hash, def_lamports_per_signature); MAX_ENTRIES + 1] IterItem(
.into_iter(), 0u64,
); &def_hash,
def_lamports_per_signature
);
MAX_ENTRIES + 1
]);
let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap(); let recent_blockhashes = from_account::<RecentBlockhashes, _>(&account).unwrap();
assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); assert_eq!(recent_blockhashes.len(), MAX_ENTRIES);
} }

View File

@ -860,15 +860,13 @@ pub fn new_secp256k1_instruction(
let signature_arr = signature.serialize(); let signature_arr = signature.serialize();
assert_eq!(signature_arr.len(), SIGNATURE_SERIALIZED_SIZE); assert_eq!(signature_arr.len(), SIGNATURE_SERIALIZED_SIZE);
let mut instruction_data = vec![]; let instruction_data_len = DATA_START
instruction_data.resize( .saturating_add(eth_pubkey.len())
DATA_START .saturating_add(signature_arr.len())
.saturating_add(eth_pubkey.len()) .saturating_add(message_arr.len())
.saturating_add(signature_arr.len()) .saturating_add(1);
.saturating_add(message_arr.len()) let mut instruction_data = vec![0; instruction_data_len];
.saturating_add(1),
0,
);
let eth_address_offset = DATA_START; let eth_address_offset = DATA_START;
instruction_data[eth_address_offset..eth_address_offset.saturating_add(eth_pubkey.len())] instruction_data[eth_address_offset..eth_address_offset.saturating_add(eth_pubkey.len())]
.copy_from_slice(&eth_pubkey); .copy_from_slice(&eth_pubkey);