From 51126f1abf36cc8b88a4f12b1a2b0b1e9254cb4c Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 25 Aug 2023 00:36:26 -0600 Subject: [PATCH 1/5] bucket-map: remove unused `mut` --- bucket_map/src/bucket.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bucket_map/src/bucket.rs b/bucket_map/src/bucket.rs index 67e3e829528ba5..e0bcde970e4051 100644 --- a/bucket_map/src/bucket.rs +++ b/bucket_map/src/bucket.rs @@ -183,7 +183,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket { /// 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) fn find_index_entry_mut( - index: &mut BucketStorage>, + index: &BucketStorage>, key: &Pubkey, random: u64, ) -> Result<(Option>, u64), BucketMapError> { @@ -410,7 +410,7 @@ impl<'b, T: Clone + Copy + 'static> Bucket { return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0))); } 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 { elem } else { From 47e2c85f023220a3011d7d92ccad3239ff7e8ac5 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 25 Aug 2023 00:40:29 -0600 Subject: [PATCH 2/5] remove unnecessary explicit `into_iter()` calls --- programs/system/src/system_processor.rs | 14 ++++---------- sdk/src/recent_blockhashes_account.rs | 25 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/programs/system/src/system_processor.rs b/programs/system/src/system_processor.rs index 04962399c0ea86..dc6c1e3dbe9c92 100644 --- a/programs/system/src/system_processor.rs +++ b/programs/system/src/system_processor.rs @@ -621,8 +621,7 @@ mod tests { fn create_default_recent_blockhashes_account() -> AccountSharedData { #[allow(deprecated)] recent_blockhashes_account::create_account_with_data_for_test( - vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES] - .into_iter(), + vec![IterItem(0u64, &Hash::default(), 0); sysvar::recent_blockhashes::MAX_ENTRIES], ) } fn create_default_rent_account() -> AccountSharedData { @@ -1577,8 +1576,7 @@ mod tests { #[allow(deprecated)] let new_recent_blockhashes_account = solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( - vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES] - .into_iter(), + vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES], ); mock_process_instruction( &system_program::id(), @@ -1863,9 +1861,7 @@ mod tests { let blockhash_id = sysvar::recent_blockhashes::id(); #[allow(deprecated)] let new_recent_blockhashes_account = - solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( - vec![].into_iter(), - ); + solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]); process_instruction( &serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(), vec![ @@ -1928,9 +1924,7 @@ mod tests { ); #[allow(deprecated)] let new_recent_blockhashes_account = - solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( - vec![].into_iter(), - ); + solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(vec![]); mock_process_instruction( &system_program::id(), Vec::new(), diff --git a/sdk/src/recent_blockhashes_account.rs b/sdk/src/recent_blockhashes_account.rs index a57b500c2b20b2..4235fc798a0b87 100644 --- a/sdk/src/recent_blockhashes_account.rs +++ b/sdk/src/recent_blockhashes_account.rs @@ -97,7 +97,7 @@ mod tests { #[test] 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::(&account).unwrap(); assert_eq!(recent_blockhashes, RecentBlockhashes::default()); } @@ -106,9 +106,14 @@ mod tests { fn test_create_account_full() { let def_hash = Hash::default(); let def_lamports_per_signature = 0; - let account = create_account_with_data_for_test( - vec![IterItem(0u64, &def_hash, def_lamports_per_signature); MAX_ENTRIES].into_iter(), - ); + let account = create_account_with_data_for_test(vec![ + IterItem( + 0u64, + &def_hash, + def_lamports_per_signature + ); + MAX_ENTRIES + ]); let recent_blockhashes = from_account::(&account).unwrap(); assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); } @@ -117,10 +122,14 @@ mod tests { fn test_create_account_truncate() { let def_hash = Hash::default(); let def_lamports_per_signature = 0; - let account = create_account_with_data_for_test( - vec![IterItem(0u64, &def_hash, def_lamports_per_signature); MAX_ENTRIES + 1] - .into_iter(), - ); + let account = create_account_with_data_for_test(vec![ + IterItem( + 0u64, + &def_hash, + def_lamports_per_signature + ); + MAX_ENTRIES + 1 + ]); let recent_blockhashes = from_account::(&account).unwrap(); assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); } From 65657dc625b6bf55612037ff8c5b3f94610e662c Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 25 Aug 2023 00:44:46 -0600 Subject: [PATCH 3/5] don't default-construct unit-structs --- runtime/src/serde_snapshot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/serde_snapshot.rs b/runtime/src/serde_snapshot.rs index e34e3ab8fbe704..1aa71131fca802 100644 --- a/runtime/src/serde_snapshot.rs +++ b/runtime/src/serde_snapshot.rs @@ -516,7 +516,7 @@ where (SerializableBankAndStorage:: { bank, snapshot_storages: storage, - phantom: std::marker::PhantomData::default(), + phantom: std::marker::PhantomData, }) .serialize(s) } From f9be05cb9f4efd6c350f1669c6458ee10abbff4a Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 25 Aug 2023 00:47:29 -0600 Subject: [PATCH 4/5] prefer `or_default()` to `or_insert_with(T::default())` --- program-runtime/src/loaded_programs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index cf22549fac1cbf..3b6372be4f828e 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -443,7 +443,7 @@ impl LoadedPrograms { key: Pubkey, entry: Arc, ) -> (bool, Arc) { - 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 .iter() .position(|at| at.effective_slot >= entry.effective_slot); From da26c7228b7cedd0b6a4d004f1f2645cd2088576 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 25 Aug 2023 00:50:09 -0600 Subject: [PATCH 5/5] replace "slow zero-filled vec initialization" --- sdk/src/secp256k1_instruction.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/sdk/src/secp256k1_instruction.rs b/sdk/src/secp256k1_instruction.rs index 4fc5958a3db39e..fc7cfaf7456116 100644 --- a/sdk/src/secp256k1_instruction.rs +++ b/sdk/src/secp256k1_instruction.rs @@ -860,15 +860,13 @@ pub fn new_secp256k1_instruction( let signature_arr = signature.serialize(); assert_eq!(signature_arr.len(), SIGNATURE_SERIALIZED_SIZE); - let mut instruction_data = vec![]; - instruction_data.resize( - DATA_START - .saturating_add(eth_pubkey.len()) - .saturating_add(signature_arr.len()) - .saturating_add(message_arr.len()) - .saturating_add(1), - 0, - ); + let instruction_data_len = DATA_START + .saturating_add(eth_pubkey.len()) + .saturating_add(signature_arr.len()) + .saturating_add(message_arr.len()) + .saturating_add(1); + let mut instruction_data = vec![0; instruction_data_len]; + let eth_address_offset = DATA_START; instruction_data[eth_address_offset..eth_address_offset.saturating_add(eth_pubkey.len())] .copy_from_slice(ð_pubkey);