refactor: promote constant; switch to list chunking (#107)

* promote constant; switch to list chunking

* cargo fmt

Co-authored-by: Noah Citron <noah@jeff.org>
This commit is contained in:
sragss 2022-11-11 16:45:08 -08:00 committed by GitHub
parent b9d67e956b
commit 23bb207f1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 19 deletions

View File

@ -0,0 +1 @@
pub const PARALLEL_QUERY_BATCH_SIZE: usize = 20;

View File

@ -22,6 +22,7 @@ use tokio::runtime::Runtime;
use consensus::types::ExecutionPayload; use consensus::types::ExecutionPayload;
use crate::{ use crate::{
constants::PARALLEL_QUERY_BATCH_SIZE,
errors::EvmError, errors::EvmError,
rpc::ExecutionRpc, rpc::ExecutionRpc,
types::{Account, CallOpts}, types::{Account, CallOpts},
@ -146,33 +147,26 @@ impl<'a, R: ExecutionRpc> Evm<'a, R> {
list.push(to_access_entry); list.push(to_access_entry);
list.push(producer_account); list.push(producer_account);
let mut accounts = Vec::new(); let mut account_map = HashMap::new();
let batch_size = 20; for chunk in list.chunks(PARALLEL_QUERY_BATCH_SIZE) {
for i in (0..list.len()).step_by(batch_size) { let account_chunk_futs = chunk.into_iter().map(|account| {
let end = cmp::min(i + batch_size, list.len());
let chunk = &list[i..end];
let account_chunk_futs = chunk.iter().map(|account| {
let addr_fut = futures::future::ready(account.address);
let account_fut = execution.get_account( let account_fut = execution.get_account(
&account.address, &account.address,
Some(account.storage_keys.as_slice()), Some(account.storage_keys.as_slice()),
&payload, &payload,
); );
async move { (addr_fut.await, account_fut.await) } async move { (account.address, account_fut.await) }
}); });
let mut account_chunk = join_all(account_chunk_futs).await; let account_chunk = join_all(account_chunk_futs).await;
accounts.append(&mut account_chunk);
}
let mut account_map = HashMap::new(); account_chunk
accounts.iter().for_each(|account| { .into_iter()
let addr = account.0; .filter(|i| i.1.is_ok())
if let Ok(account) = &account.1 { .for_each(|(key, value)| {
account_map.insert(addr, account.clone()); account_map.insert(key, value.ok().unwrap());
} });
}); }
Ok(account_map) Ok(account_map)
} }

View File

@ -1,3 +1,4 @@
pub mod constants;
pub mod errors; pub mod errors;
pub mod evm; pub mod evm;
pub mod rpc; pub mod rpc;