Transaction pool: Adds benchmark and improves performance (#9958)

* Yep

* Make it compile

* Make the benchmark work

* Some stuff

* Optimize transaction pool `BestIterator`

* Some docs

* Fix more warnings

* Fix compilation

* FMT
This commit is contained in:
Bastian Köcher
2021-10-08 10:02:25 +02:00
committed by GitHub
parent b965d49a6a
commit 451413f47e
9 changed files with 415 additions and 65 deletions
@@ -31,7 +31,7 @@ use sp_runtime::{traits::Member, transaction_validity::TransactionTag as Tag};
use super::{
base_pool::Transaction,
future::WaitingTransaction,
tracked_map::{self, ReadOnlyTrackedMap, TrackedMap},
tracked_map::{self, TrackedMap},
};
/// An in-pool transaction reference.
@@ -162,7 +162,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
/// skipped.
pub fn get(&self) -> BestIterator<Hash, Ex> {
BestIterator {
all: self.ready.clone(),
all: self.ready.clone_map(),
best: self.best.clone(),
awaiting: Default::default(),
invalid: Default::default(),
@@ -484,7 +484,7 @@ impl<Hash: hash::Hash + Member + Serialize, Ex> ReadyTransactions<Hash, Ex> {
/// Iterator of ready transactions ordered by priority.
pub struct BestIterator<Hash, Ex> {
all: ReadOnlyTrackedMap<Hash, ReadyTx<Hash, Ex>>,
all: HashMap<Hash, ReadyTx<Hash, Ex>>,
awaiting: HashMap<Hash, (usize, TransactionRef<Hash, Ex>)>,
best: BTreeSet<TransactionRef<Hash, Ex>>,
invalid: HashSet<Hash>,
@@ -519,7 +519,7 @@ impl<Hash: hash::Hash + Member, Ex> BestIterator<Hash, Ex> {
/// When given transaction is not in the pool it has no effect.
/// When invoked on a fully drained iterator it has no effect either.
pub fn report_invalid(&mut self, tx: &Arc<Transaction<Hash, Ex>>) {
if let Some(to_report) = self.all.read().get(&tx.hash) {
if let Some(to_report) = self.all.get(&tx.hash) {
debug!(
target: "txpool",
"[{:?}] Reported as invalid. Will skip sub-chains while iterating.",
@@ -551,8 +551,7 @@ impl<Hash: hash::Hash + Member, Ex> Iterator for BestIterator<Hash, Ex> {
continue
}
let next = self.all.read().get(hash).cloned();
let ready = match next {
let ready = match self.all.get(&hash).cloned() {
Some(ready) => ready,
// The transaction is not in all, maybe it was removed in the meantime?
None => continue,
@@ -567,7 +566,6 @@ impl<Hash: hash::Hash + Member, Ex> Iterator for BestIterator<Hash, Ex> {
// then get from the pool
} else {
self.all
.read()
.get(hash)
.map(|next| (next.requires_offset + 1, next.transaction.clone()))
};
@@ -57,11 +57,6 @@ impl<K, V> TrackedMap<K, V> {
std::cmp::max(self.bytes.load(AtomicOrdering::Relaxed), 0) as usize
}
/// Read-only clone of the interior.
pub fn clone(&self) -> ReadOnlyTrackedMap<K, V> {
ReadOnlyTrackedMap(self.index.clone())
}
/// Lock map for read.
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess { inner_guard: self.index.read() }
@@ -77,18 +72,10 @@ impl<K, V> TrackedMap<K, V> {
}
}
/// Read-only access to map.
///
/// The only thing can be done is .read().
pub struct ReadOnlyTrackedMap<K, V>(Arc<RwLock<HashMap<K, V>>>);
impl<K, V> ReadOnlyTrackedMap<K, V>
where
K: Eq + std::hash::Hash,
{
/// Lock map for read.
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess { inner_guard: self.0.read() }
impl<K: Clone, V: Clone> TrackedMap<K, V> {
/// Clone the inner map.
pub fn clone_map(&self) -> HashMap<K, V> {
self.index.read().clone()
}
}