diff --git a/crates/storage/db/benches/criterion.rs b/crates/storage/db/benches/criterion.rs index 54dca69b21..cdbd19f4d7 100644 --- a/crates/storage/db/benches/criterion.rs +++ b/crates/storage/db/benches/criterion.rs @@ -1,4 +1,6 @@ #![allow(missing_docs)] +mod integer_keys; + use criterion::{ black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; diff --git a/crates/storage/db/benches/integer_keys.rs b/crates/storage/db/benches/integer_keys.rs index 9155d29036..894d0a4fbe 100644 --- a/crates/storage/db/benches/integer_keys.rs +++ b/crates/storage/db/benches/integer_keys.rs @@ -13,8 +13,9 @@ use reth_db::{ cursor::DbCursorRW, is_database_empty, mdbx::DatabaseArguments, + models::TxNumberLe, test_utils::{TempDatabase, ERROR_DB_CREATION}, - DatabaseEnvKind, DatabaseError, TransactionBlocks, + DatabaseEnvKind, DatabaseError, RawTable, TransactionBlocks, TransactionBlocks2, }; use reth_libmdbx::{DatabaseFlags, MaxReadTransactionDuration}; use std::collections::HashSet; @@ -77,10 +78,10 @@ pub fn integer_keys(c: &mut Criterion) { size, false, ); - measure_table_insertion::( + measure_table_insertion::( &mut group, - preload.clone(), - unsorted_input.clone(), + preload.clone().into_iter().map(|(k, v)| (TxNumberLe(k), v)).collect(), + unsorted_input.clone().into_iter().map(|(k, v)| (TxNumberLe(k), v)).collect(), size, true, ); @@ -107,8 +108,8 @@ fn measure_table_insertion( let bench_db_path = Path::new(BENCH_DB_PATH); let scenarios: Vec<(fn(_, _) -> _, &str)> = vec![ - // (append::, "append_all"), - // (append::, "append_input"), + (append::, "append_all"), + (append::, "append_input"), (insert::, "insert_unsorted"), (insert::, "insert_sorted"), (put::, "put_unsorted"), @@ -141,7 +142,7 @@ fn measure_table_insertion( .unwrap(); tx.create_db( - Some("TransactionBlocks"), + Some(if use_ints { "TransactionBlocks2" } else { "TransactionBlocks" }), if use_ints { DatabaseFlags::INTEGER_KEY } else { DatabaseFlags::default() }, ) .map_err(|e| DatabaseError::CreateTable(e.into())) diff --git a/crates/storage/db/src/tables/mod.rs b/crates/storage/db/src/tables/mod.rs index 3f3c6bf176..4ff1fe8d29 100644 --- a/crates/storage/db/src/tables/mod.rs +++ b/crates/storage/db/src/tables/mod.rs @@ -44,6 +44,8 @@ use reth_primitives::{ }; use std::fmt; +use self::models::TxNumberLe; + /// Enum for the types of tables present in libmdbx. #[derive(Debug, PartialEq, Copy, Clone)] pub enum TableType { @@ -269,12 +271,13 @@ tables! { table Transactions; /// Stores the mapping of the transaction hash to the transaction number. - table TransactionHashNumbers; + // table TransactionHashNumbers; /// Stores the mapping of transaction number to the blocks number. /// /// The key is the highest transaction ID in the block. table TransactionBlocks; + table TransactionBlocks2; /// Canonical only Stores transaction receipts. table Receipts; diff --git a/crates/storage/db/src/tables/models/mod.rs b/crates/storage/db/src/tables/models/mod.rs index 5c671ac6b9..1171897dbc 100644 --- a/crates/storage/db/src/tables/models/mod.rs +++ b/crates/storage/db/src/tables/models/mod.rs @@ -47,6 +47,36 @@ macro_rules! impl_uints { impl_uints!(u64, u32, u16, u8); +#[derive( + Clone, + Copy, + PartialEq, + Eq, + PartialOrd, + Ord, + Default, + Hash, + Debug, + serde::Serialize, + serde::Deserialize, +)] +#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary, proptest_derive::Arbitrary))] +pub struct TxNumberLe(pub u64); + +impl Encode for TxNumberLe { + type Encoded = [u8; std::mem::size_of::()]; + + fn encode(self) -> Self::Encoded { + self.0.to_le_bytes() + } +} + +impl Decode for TxNumberLe { + fn decode>(value: B) -> Result { + Ok(Self(u64::from_le_bytes(value.as_ref().try_into().map_err(|_| DatabaseError::Decode)?))) + } +} + impl Encode for Vec { type Encoded = Vec;