From da7185e2ede7fdf7656fdf3eda2a4255328991aa Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 16 Sep 2021 23:09:11 +0000 Subject: [PATCH] rpc: performance fix for getProgramAccounts (backport #19941) (#19949) * rpc: performance fix for getProgramAccounts (#19941) * rpc: performance fix for getProgramAccounts The accounts were gradually pushed into a vector, which produced significant slowdowns for very large responses. * rpc: rewrite loops using iterators Co-authored-by: Christian Kamm (cherry picked from commit f1bbf1d8b09256b4baa4753c27b61dae4e7908a7) # Conflicts: # core/src/rpc.rs * fix conflicts * Fix conflicts Co-authored-by: Christian Kamm Co-authored-by: Justin Starry Co-authored-by: Tyera Eulberg --- core/src/rpc.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/core/src/rpc.rs b/core/src/rpc.rs index 8c83ea6911f76c..b6532064359e30 100644 --- a/core/src/rpc.rs +++ b/core/src/rpc.rs @@ -328,18 +328,15 @@ impl JsonRpcRequestProcessor { pubkeys: Vec, config: Option, ) -> Result>>> { - let mut accounts: Vec> = vec![]; - let config = config.unwrap_or_default(); let bank = self.bank(config.commitment); let encoding = config.encoding.unwrap_or(UiAccountEncoding::Base64); check_slice_and_encoding(&encoding, config.data_slice.is_some())?; - for pubkey in pubkeys { - let response_account = - get_encoded_account(&bank, &pubkey, encoding, config.data_slice)?; - accounts.push(response_account) - } + let accounts = pubkeys + .into_iter() + .map(|pubkey| get_encoded_account(&bank, &pubkey, encoding, config.data_slice)) + .collect::>>()?; Ok(new_response(&bank, accounts)) } @@ -379,17 +376,19 @@ impl JsonRpcRequestProcessor { } else { keyed_accounts .into_iter() - .map(|(pubkey, account)| RpcKeyedAccount { - pubkey: pubkey.to_string(), - account: UiAccount::encode( - &pubkey, - &account, - encoding, - None, - data_slice_config, - ), + .map(|(pubkey, account)| { + Ok(RpcKeyedAccount { + pubkey: pubkey.to_string(), + account: UiAccount::encode( + &pubkey, + &account, + encoding, + None, + data_slice_config, + ), + }) }) - .collect() + .collect::>>()? }; Ok(result).map(|result| match with_context { true => OptionalContext::Context(new_response(&bank, result)), @@ -2804,10 +2803,10 @@ pub mod rpc_full { max_multiple_accounts ))); } - let mut pubkeys: Vec = vec![]; - for pubkey_str in pubkey_strs { - pubkeys.push(verify_pubkey(&pubkey_str)?); - } + let pubkeys = pubkey_strs + .into_iter() + .map(|pubkey_str| verify_pubkey(&pubkey_str)) + .collect::>>()?; meta.get_multiple_accounts(pubkeys, config) }