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 } )
0 commit comments