From 3a1eeee812d3acf82f70f4c4cce65be56db21787 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 29 Aug 2023 10:40:19 -0700 Subject: [PATCH] perf: no need to keep track of replaced txs (#4394) --- crates/transaction-pool/src/blobstore/mem.rs | 29 ++++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/crates/transaction-pool/src/blobstore/mem.rs b/crates/transaction-pool/src/blobstore/mem.rs index 187b9026f0..352ecae615 100644 --- a/crates/transaction-pool/src/blobstore/mem.rs +++ b/crates/transaction-pool/src/blobstore/mem.rs @@ -21,22 +21,16 @@ struct InMemoryBlobStoreInner { } impl InMemoryBlobStoreInner { + #[inline] fn add_size(&self, add: usize) { self.data_size.fetch_add(add, std::sync::atomic::Ordering::Relaxed); } + #[inline] fn sub_size(&self, sub: usize) { self.data_size.fetch_sub(sub, std::sync::atomic::Ordering::Relaxed); } - fn update_size(&self, add: usize, sub: usize) { - if add > sub { - self.add_size(add - sub); - } else { - self.sub_size(sub - add); - } - } - fn update_len(&self, len: usize) { self.num_blobs.store(len, std::sync::atomic::Ordering::Relaxed); } @@ -45,8 +39,7 @@ impl InMemoryBlobStoreInner { impl BlobStore for InMemoryBlobStore { fn insert(&self, tx: H256, data: BlobTransactionSidecar) -> Result<(), BlobStoreError> { let mut store = self.inner.store.write(); - let (add, sub) = insert_size(&mut store, tx, data); - self.inner.update_size(add, sub); + self.inner.add_size(insert_size(&mut store, tx, data)); self.inner.update_len(store.len()); Ok(()) } @@ -57,13 +50,11 @@ impl BlobStore for InMemoryBlobStore { } let mut store = self.inner.store.write(); let mut total_add = 0; - let mut total_sub = 0; for (tx, data) in txs { - let (add, sub) = insert_size(&mut store, tx, data); + let add = insert_size(&mut store, tx, data); total_add += add; - total_sub += sub; } - self.inner.update_size(total_add, total_sub); + self.inner.add_size(total_add); self.inner.update_len(store.len()); Ok(()) } @@ -126,14 +117,16 @@ fn remove_size(store: &mut HashMap, tx: &H256) -> store.remove(tx).map(|rem| rem.size()).unwrap_or_default() } -/// Inserts the given blob into the store and returns the size of the blob that was (added,removed) +/// Inserts the given blob into the store and returns the size of the blob that was added +/// +/// We don't need to handle the size updates for replacements because transactions are unique. #[inline] fn insert_size( store: &mut HashMap, tx: H256, blob: BlobTransactionSidecar, -) -> (usize, usize) { +) -> usize { let add = blob.size(); - let sub = store.insert(tx, blob).map(|rem| rem.size()).unwrap_or_default(); - (add, sub) + store.insert(tx, blob).map(|rem| rem.size()); + add }