Skip to content

Commit 0522208

Browse files
committed
make account compilation take an iter
1 parent e04923a commit 0522208

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

harness/src/compile_accounts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub struct CompiledAccounts {
2121
pub transaction_accounts: Vec<TransactionAccount>,
2222
}
2323

24-
pub fn compile_accounts(
24+
pub fn compile_accounts<'a>(
2525
instruction: &Instruction,
26-
accounts: &[(Pubkey, Account)],
26+
accounts: impl Iterator<Item = &'a (Pubkey, Account)>,
2727
loader_key: Pubkey,
2828
) -> CompiledAccounts {
2929
let stub_out_program_account = move || {

harness/src/fuzz/firedancer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn build_fixture_context(
7373
instruction_accounts,
7474
transaction_accounts,
7575
..
76-
} = compile_accounts(instruction, accounts, loader_key);
76+
} = compile_accounts(instruction, accounts.iter(), loader_key);
7777

7878
let accounts = transaction_accounts
7979
.into_iter()

harness/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ impl Mollusk {
792792
program_id_index,
793793
instruction_accounts,
794794
transaction_accounts,
795-
} = crate::compile_accounts::compile_accounts(instruction, accounts, loader_key);
795+
} = crate::compile_accounts::compile_accounts(instruction, accounts.iter(), loader_key);
796796

797797
self.process_instruction_inner(
798798
instruction,
@@ -830,7 +830,7 @@ impl Mollusk {
830830
program_id_index,
831831
instruction_accounts,
832832
transaction_accounts,
833-
} = crate::compile_accounts::compile_accounts(instruction, accounts, loader_key);
833+
} = crate::compile_accounts::compile_accounts(instruction, accounts.iter(), loader_key);
834834

835835
let this_result = self.process_instruction_inner(
836836
instruction,
@@ -884,7 +884,7 @@ impl Mollusk {
884884
program_id_index,
885885
instruction_accounts,
886886
transaction_accounts,
887-
} = crate::compile_accounts::compile_accounts(instruction, accounts, loader_key);
887+
} = crate::compile_accounts::compile_accounts(instruction, accounts.iter(), loader_key);
888888

889889
let result = self.process_instruction_inner(
890890
instruction,
@@ -943,7 +943,7 @@ impl Mollusk {
943943
program_id_index,
944944
instruction_accounts,
945945
transaction_accounts,
946-
} = crate::compile_accounts::compile_accounts(instruction, accounts, loader_key);
946+
} = crate::compile_accounts::compile_accounts(instruction, accounts.iter(), loader_key);
947947

948948
let this_result = self.process_instruction_inner(
949949
instruction,

keys/src/accounts.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use {
77
solana_instruction::Instruction,
88
solana_pubkey::Pubkey,
99
solana_transaction_context::{IndexOfAccount, InstructionAccount, TransactionAccount},
10+
std::collections::HashMap,
1011
};
1112

1213
// Helper struct to avoid cloning instruction data.
@@ -57,12 +58,21 @@ pub fn compile_instruction_accounts(
5758
.collect()
5859
}
5960

60-
pub fn compile_transaction_accounts_for_instruction(
61+
pub fn compile_transaction_accounts_for_instruction<'a>(
6162
key_map: &KeyMap,
6263
instruction: &Instruction,
63-
accounts: &[(Pubkey, Account)],
64+
accounts: impl Iterator<Item = &'a (Pubkey, Account)>,
6465
stub_out_program_account: Option<Box<dyn Fn() -> Account>>,
6566
) -> Vec<TransactionAccount> {
67+
let len = key_map.len();
68+
let mut by_key: HashMap<Pubkey, AccountSharedData> = HashMap::with_capacity(len);
69+
70+
for (key, account) in accounts {
71+
if key_map.contains_key(key) {
72+
by_key.insert(*key, AccountSharedData::from(account.clone()));
73+
}
74+
}
75+
6676
key_map
6777
.keys()
6878
.map(|key| {
@@ -71,22 +81,29 @@ pub fn compile_transaction_accounts_for_instruction(
7181
return (*key, stub_out_program_account().into());
7282
}
7383
}
74-
let account = accounts
75-
.iter()
76-
.find(|(k, _)| k == key)
77-
.map(|(_, account)| AccountSharedData::from(account.clone()))
84+
let account = by_key
85+
.remove(key)
7886
.or_panic_with(MolluskError::AccountMissing(key));
7987
(*key, account)
8088
})
8189
.collect()
8290
}
8391

84-
pub fn compile_transaction_accounts(
92+
pub fn compile_transaction_accounts<'a>(
8593
key_map: &KeyMap,
8694
instructions: &[Instruction],
87-
accounts: &[(Pubkey, Account)],
95+
accounts: impl Iterator<Item = &'a (Pubkey, Account)>,
8896
stub_out_program_account: Option<Box<dyn Fn() -> Account>>,
8997
) -> Vec<TransactionAccount> {
98+
let len = key_map.len();
99+
let mut by_key: HashMap<Pubkey, AccountSharedData> = HashMap::with_capacity(len);
100+
101+
for (key, account) in accounts {
102+
if key_map.contains_key(key) {
103+
by_key.insert(*key, AccountSharedData::from(account.clone()));
104+
}
105+
}
106+
90107
key_map
91108
.keys()
92109
.map(|key| {
@@ -95,10 +112,8 @@ pub fn compile_transaction_accounts(
95112
return (*key, stub_out_program_account().into());
96113
}
97114
}
98-
let account = accounts
99-
.iter()
100-
.find(|(k, _)| k == key)
101-
.map(|(_, account)| AccountSharedData::from(account.clone()))
115+
let account = by_key
116+
.remove(key)
102117
.or_panic_with(MolluskError::AccountMissing(key));
103118
(*key, account)
104119
})

keys/src/keys.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ impl KeyMap {
181181
pub fn position(&self, key: &Pubkey) -> Option<usize> {
182182
self.map.keys().position(|k| k == key)
183183
}
184+
185+
/// Return whether or not the map contains a key.
186+
pub fn contains_key(&self, key: &Pubkey) -> bool {
187+
self.map.contains_key(key)
188+
}
189+
190+
/// Get the length of the key map.
191+
pub fn len(&self) -> usize {
192+
self.map.len()
193+
}
194+
195+
/// Return whether or not the key map is empty.
196+
pub fn is_empty(&self) -> bool {
197+
self.map.is_empty()
198+
}
184199
}
185200

186201
#[cfg(test)]

0 commit comments

Comments
 (0)