make it work for both benches

This commit is contained in:
Roman Krasiuk
2024-03-04 22:56:05 +01:00
parent 0251519a90
commit 1825c98363
4 changed files with 44 additions and 8 deletions

View File

@@ -1,4 +1,6 @@
#![allow(missing_docs)]
mod integer_keys;
use criterion::{
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};

View File

@@ -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::<TransactionBlocks>(
measure_table_insertion::<TransactionBlocks2>(
&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<T>(
let bench_db_path = Path::new(BENCH_DB_PATH);
let scenarios: Vec<(fn(_, _) -> _, &str)> = vec![
// (append::<T>, "append_all"),
// (append::<T>, "append_input"),
(append::<T>, "append_all"),
(append::<T>, "append_input"),
(insert::<T>, "insert_unsorted"),
(insert::<T>, "insert_sorted"),
(put::<T>, "put_unsorted"),
@@ -141,7 +142,7 @@ fn measure_table_insertion<T>(
.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()))

View File

@@ -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<Key = TxNumber, Value = TransactionSignedNoHash>;
/// Stores the mapping of the transaction hash to the transaction number.
table TransactionHashNumbers<Key = TxHash, Value = TxNumber>;
// table TransactionHashNumbers<Key = TxHash, Value = TxNumber>;
/// Stores the mapping of transaction number to the blocks number.
///
/// The key is the highest transaction ID in the block.
table TransactionBlocks<Key = TxNumber, Value = BlockNumber>;
table TransactionBlocks2<Key = TxNumberLe, Value = BlockNumber>;
/// Canonical only Stores transaction receipts.
table Receipts<Key = TxNumber, Value = Receipt>;

View File

@@ -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::<u64>()];
fn encode(self) -> Self::Encoded {
self.0.to_le_bytes()
}
}
impl Decode for TxNumberLe {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
Ok(Self(u64::from_le_bytes(value.as_ref().try_into().map_err(|_| DatabaseError::Decode)?)))
}
}
impl Encode for Vec<u8> {
type Encoded = Vec<u8>;