From bc1c8c75ec95c11426bf13364c6db0d740b111d4 Mon Sep 17 00:00:00 2001 From: Godmode Galactus Date: Tue, 26 Nov 2024 09:28:17 +0100 Subject: [PATCH] Some more optimizations by avoiding coping --- .../src/account_data_by_commitment.rs | 4 +-- account_storage/src/inmemory_account_store.rs | 5 ++-- common/src/account_filter.rs | 28 +++++++++++++------ run_clippy_fmt.sh | 4 +-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/account_storage/src/account_data_by_commitment.rs b/account_storage/src/account_data_by_commitment.rs index 25897f2..13fe67c 100644 --- a/account_storage/src/account_data_by_commitment.rs +++ b/account_storage/src/account_data_by_commitment.rs @@ -65,9 +65,7 @@ impl AccountDataByCommitment { Commitment::Finalized => self.finalized_account.as_ref(), }; - let Some(account_data) = account_data else { - return None; - }; + let account_data = account_data?; // check size filter first before decompressing for filter in filters { diff --git a/account_storage/src/inmemory_account_store.rs b/account_storage/src/inmemory_account_store.rs index 93937a2..04b7f74 100644 --- a/account_storage/src/inmemory_account_store.rs +++ b/account_storage/src/inmemory_account_store.rs @@ -313,13 +313,13 @@ impl AccountStorageInterface for InmemoryAccountStore { let mut return_vec = vec![]; let store_read_lk = self.accounts_store.read().unwrap(); - let mut retry_list = BTreeSet::new(); + let mut retry_list = Vec::with_capacity(128); // optimization to avoid locking for each account for program_account_index in lk.read().unwrap().iter() { let lk_on_account = match store_read_lk[*program_account_index].try_read() { Ok(lk) => lk, Err(_) => { - retry_list.insert(*program_account_index); + retry_list.push(*program_account_index); continue; } }; @@ -331,7 +331,6 @@ impl AccountStorageInterface for InmemoryAccountStore { &account_filters, ); } - // retry for the accounts which were locked for retry_account_index in retry_list { let lk_on_account = store_read_lk[retry_account_index].read().unwrap(); diff --git a/common/src/account_filter.rs b/common/src/account_filter.rs index 4e6ad49..9f07602 100644 --- a/common/src/account_filter.rs +++ b/common/src/account_filter.rs @@ -34,15 +34,27 @@ impl MemcmpFilter { } pub fn bytes_match(&self, data: &[u8]) -> bool { - let bytes = self.bytes(); - let offset = self.offset as usize; - if offset > data.len() { - return false; + // optimize for bytes / avoid copying + if let MemcmpFilterData::Bytes(bytes) = &self.data { + let offset = self.offset as usize; + if offset > data.len() { + return false; + } + if data[offset..].len() < bytes.len() { + return false; + } + data[offset..offset + bytes.len()] == bytes[..] + } else { + let bytes = self.bytes(); + let offset = self.offset as usize; + if offset > data.len() { + return false; + } + if data[offset..].len() < bytes.len() { + return false; + } + data[offset..offset + bytes.len()] == bytes[..] } - if data[offset..].len() < bytes.len() { - return false; - } - data[offset..offset + bytes.len()] == bytes[..] } } diff --git a/run_clippy_fmt.sh b/run_clippy_fmt.sh index 4b1c613..70904ff 100755 --- a/run_clippy_fmt.sh +++ b/run_clippy_fmt.sh @@ -1,2 +1,2 @@ -cargo +nightly-2023-10-05 fmt --all -cargo +nightly-2023-10-05 clippy --locked --workspace --all-targets -- -D warnings \ No newline at end of file +cargo +1.78.0 fmt --all +cargo +1.78.0 clippy --locked --workspace --all-targets -- -D warnings \ No newline at end of file