Skip to content

Commit

Permalink
perf: random changes (paradigmxyz#5795)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Dec 16, 2023
1 parent bfe12ce commit 27caf7b
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 230 deletions.
2 changes: 1 addition & 1 deletion crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ mod tests {
.with_pending_blocks((block2.number + 1, HashSet::new()))
.assert(&tree);

assert!(tree.make_canonical(&block1a_hash).is_ok());
assert_matches!(tree.make_canonical(&block1a_hash), Ok(_));
// Trie state:
// b2a b2 (side chain)
// | /
Expand Down
27 changes: 12 additions & 15 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,18 @@ impl Receipts {

/// Retrieves gas spent by transactions as a vector of tuples (transaction index, gas used).
pub fn gas_spent_by_tx(&self) -> Result<Vec<(u64, u64)>, PruneSegmentError> {
self.last()
.map(|block_r| {
block_r
.iter()
.enumerate()
.map(|(id, tx_r)| {
if let Some(receipt) = tx_r.as_ref() {
Ok((id as u64, receipt.cumulative_gas_used))
} else {
Err(PruneSegmentError::ReceiptsPruned)
}
})
.collect::<Result<Vec<_>, PruneSegmentError>>()
})
.unwrap_or(Ok(vec![]))
let Some(block_r) = self.last() else {
return Ok(vec![]);
};
let mut out = Vec::with_capacity(block_r.len());
for (id, tx_r) in block_r.iter().enumerate() {
if let Some(receipt) = tx_r.as_ref() {
out.push((id as u64, receipt.cumulative_gas_used));
} else {
return Err(PruneSegmentError::ReceiptsPruned);
}
}
Ok(out)
}
}

Expand Down
20 changes: 7 additions & 13 deletions crates/storage/db/src/abstraction/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ where
}
}

impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator for Walker<'cursor, T, CURSOR> {
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> Iterator for Walker<'cursor, T, CURSOR> {
type Item = Result<TableRow<T>, DatabaseError>;
fn next(&mut self) -> Option<Self::Item> {
let start = self.start.take();
Expand Down Expand Up @@ -227,9 +227,7 @@ impl<'cursor, T: Table, CURSOR: DbCursorRW<T> + DbCursorRO<T>> ReverseWalker<'cu
}
}

impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator
for ReverseWalker<'cursor, T, CURSOR>
{
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> Iterator for ReverseWalker<'cursor, T, CURSOR> {
type Item = Result<TableRow<T>, DatabaseError>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -270,10 +268,9 @@ where
}
}

impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator
for RangeWalker<'cursor, T, CURSOR>
{
impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> Iterator for RangeWalker<'cursor, T, CURSOR> {
type Item = Result<TableRow<T>, DatabaseError>;

fn next(&mut self) -> Option<Self::Item> {
if self.is_done {
return None
Expand All @@ -292,11 +289,10 @@ impl<'cursor, T: Table, CURSOR: DbCursorRO<T>> std::iter::Iterator
}
},
Some(res @ Err(_)) => Some(res),
None if matches!(self.end_key, Bound::Unbounded) => {
self.is_done = true;
None => {
self.is_done = matches!(self.end_key, Bound::Unbounded);
None
}
_ => None,
}
}
}
Expand Down Expand Up @@ -361,9 +357,7 @@ impl<'cursor, T: DupSort, CURSOR: DbCursorRW<T> + DbDupCursorRO<T>> DupWalker<'c
}
}

impl<'cursor, T: DupSort, CURSOR: DbDupCursorRO<T>> std::iter::Iterator
for DupWalker<'cursor, T, CURSOR>
{
impl<'cursor, T: DupSort, CURSOR: DbDupCursorRO<T>> Iterator for DupWalker<'cursor, T, CURSOR> {
type Item = Result<TableRow<T>, DatabaseError>;
fn next(&mut self) -> Option<Self::Item> {
let start = self.start.take();
Expand Down
55 changes: 27 additions & 28 deletions crates/storage/db/src/implementation/mdbx/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//! Cursor wrapper for libmdbx-sys.

use reth_interfaces::db::{DatabaseWriteError, DatabaseWriteOperation};
use std::{borrow::Cow, collections::Bound, marker::PhantomData, ops::RangeBounds};

use crate::{
common::{PairResult, ValueOnlyResult},
cursor::{
DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, RangeWalker,
ReverseWalker, Walker,
},
metrics::{Operation, OperationMetrics},
table::{Compress, DupSort, Encode, Table},
table::{Compress, Decode, Decompress, DupSort, Encode, Table},
tables::utils::*,
DatabaseError,
};
use reth_interfaces::db::{DatabaseWriteError, DatabaseWriteOperation};
use reth_libmdbx::{self, Error as MDBXError, TransactionKind, WriteFlags, RO, RW};
use std::{borrow::Cow, collections::Bound, marker::PhantomData, ops::RangeBounds};

/// Read only Cursor.
pub type CursorRO<T> = Cursor<RO, T>;
Expand Down Expand Up @@ -56,12 +55,17 @@ impl<K: TransactionKind, T: Table> Cursor<K, T> {
}
}

/// Takes `(key, value)` from the database and decodes it appropriately.
#[macro_export]
macro_rules! decode {
($v:expr) => {
$v.map_err(|e| $crate::DatabaseError::Read(e.into()))?.map(decoder::<T>).transpose()
};
/// Decodes a `(key, value)` pair from the database.
#[allow(clippy::type_complexity)]
pub fn decode<T>(
res: Result<Option<(Cow<'_, [u8]>, Cow<'_, [u8]>)>, impl Into<i32>>,
) -> PairResult<T>
where
T: Table,
T::Key: Decode,
T::Value: Decompress,
{
res.map_err(|e| DatabaseError::Read(e.into()))?.map(decoder::<T>).transpose()
}

/// Some types don't support compression (eg. B256), and we don't want to be copying them to the
Expand All @@ -80,39 +84,36 @@ macro_rules! compress_to_buf_or_ref {

impl<K: TransactionKind, T: Table> DbCursorRO<T> for Cursor<K, T> {
fn first(&mut self) -> PairResult<T> {
decode!(self.inner.first())
decode::<T>(self.inner.first())
}

fn seek_exact(&mut self, key: <T as Table>::Key) -> PairResult<T> {
decode!(self.inner.set_key(key.encode().as_ref()))
decode::<T>(self.inner.set_key(key.encode().as_ref()))
}

fn seek(&mut self, key: <T as Table>::Key) -> PairResult<T> {
decode!(self.inner.set_range(key.encode().as_ref()))
decode::<T>(self.inner.set_range(key.encode().as_ref()))
}

fn next(&mut self) -> PairResult<T> {
decode!(self.inner.next())
decode::<T>(self.inner.next())
}

fn prev(&mut self) -> PairResult<T> {
decode!(self.inner.prev())
decode::<T>(self.inner.prev())
}

fn last(&mut self) -> PairResult<T> {
decode!(self.inner.last())
decode::<T>(self.inner.last())
}

fn current(&mut self) -> PairResult<T> {
decode!(self.inner.get_current())
decode::<T>(self.inner.get_current())
}

fn walk(&mut self, start_key: Option<T::Key>) -> Result<Walker<'_, T, Self>, DatabaseError> {
let start = if let Some(start_key) = start_key {
self.inner
.set_range(start_key.encode().as_ref())
.map_err(|e| DatabaseError::Read(e.into()))?
.map(decoder::<T>)
decode::<T>(self.inner.set_range(start_key.encode().as_ref())).transpose()
} else {
self.first().transpose()
};
Expand All @@ -130,10 +131,8 @@ impl<K: TransactionKind, T: Table> DbCursorRO<T> for Cursor<K, T> {
unreachable!("Rust doesn't allow for Bound::Excluded in starting bounds");
}
Bound::Unbounded => self.inner.first(),
}
.map_err(|e| DatabaseError::Read(e.into()))?
.map(decoder::<T>);

};
let start = decode::<T>(start).transpose();
Ok(RangeWalker::new(self, start, range.end_bound().cloned()))
}

Expand All @@ -142,7 +141,7 @@ impl<K: TransactionKind, T: Table> DbCursorRO<T> for Cursor<K, T> {
start_key: Option<T::Key>,
) -> Result<ReverseWalker<'_, T, Self>, DatabaseError> {
let start = if let Some(start_key) = start_key {
decode!(self.inner.set_range(start_key.encode().as_ref()))
decode::<T>(self.inner.set_range(start_key.encode().as_ref()))
} else {
self.last()
}
Expand All @@ -155,12 +154,12 @@ impl<K: TransactionKind, T: Table> DbCursorRO<T> for Cursor<K, T> {
impl<K: TransactionKind, T: DupSort> DbDupCursorRO<T> for Cursor<K, T> {
/// Returns the next `(key, value)` pair of a DUPSORT table.
fn next_dup(&mut self) -> PairResult<T> {
decode!(self.inner.next_dup())
decode::<T>(self.inner.next_dup())
}

/// Returns the next `(key, value)` pair skipping the duplicates.
fn next_no_dup(&mut self) -> PairResult<T> {
decode!(self.inner.next_nodup())
decode::<T>(self.inner.next_nodup())
}

/// Returns the next `value` of a duplicate `key`.
Expand Down
Loading

0 comments on commit 27caf7b

Please sign in to comment.