Skip to content

Commit

Permalink
perf: use Entry to speed up MemoryPool::add_unconfirmed_transaction
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Apr 26, 2023
1 parent a3df870 commit 91856ab
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions node/consensus/src/memory_pool/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use std::collections::hash_map::Entry;

impl<N: Network> MemoryPool<N> {
/// Returns `true` if the given unconfirmed transaction exists in the memory pool.
pub fn contains_unconfirmed_transaction(&self, transaction_id: N::TransactionID) -> bool {
Expand Down Expand Up @@ -74,14 +76,14 @@ impl<N: Network> MemoryPool<N> {
let mut unconfirmed_transactions = self.unconfirmed_transactions.write();

// Ensure the transaction does not already exist in the memory pool.
match !unconfirmed_transactions.contains_key(&transaction.id()) {
true => {
match unconfirmed_transactions.entry(transaction.id()) {
Entry::Vacant(entry) => {
// Add the transaction to the memory pool.
unconfirmed_transactions.insert(transaction.id(), transaction.clone());
entry.insert(transaction.clone());
debug!("✉️ Added transaction '{}' to the memory pool", transaction.id());
true
}
false => {
Entry::Occupied(_) => {
trace!("Transaction '{}' already exists in memory pool", transaction.id());
false
}
Expand Down

0 comments on commit 91856ab

Please sign in to comment.