Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(stages): Adds benchmark to TransactionLookupStage #1130

Merged
merged 27 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c7bafce
add arbitrary to sealedblock and sealedheader
joshieDo Feb 1, 2023
3781c48
put stages::test_utils behind a feature flag instead
joshieDo Feb 1, 2023
30557c2
add txlookup stage benchmark wip
joshieDo Feb 1, 2023
969fbe2
add unwind to bench setup
joshieDo Feb 2, 2023
78e679d
use bincode instead for block vectors
joshieDo Feb 2, 2023
dec4409
add Clone to TxLookupStage
joshieDo Feb 2, 2023
1bf00c5
append if possible on TransactionLookupStage
joshieDo Feb 2, 2023
ac5ecc8
add README.md
joshieDo Feb 2, 2023
98b95c6
Merge remote-tracking branch 'origin/main' into joshie/txlookup
joshieDo Feb 2, 2023
547d7d7
sort first
joshieDo Feb 4, 2023
186d4bd
Merge remote-tracking branch 'origin/main' into joshie/txlookup
joshieDo Feb 4, 2023
0100cea
add db hash_keys benchmark
joshieDo Feb 4, 2023
e73b459
clippy complexity
joshieDo Feb 4, 2023
b684a5e
perf: reuse secp256k1 context during sig recovery
gakonst Feb 4, 2023
eff66a5
feat: allow instantiating TestTransaction from path
gakonst Feb 4, 2023
e973fbc
bench: refactor and include sender recovery
gakonst Feb 4, 2023
ae897b2
Merge branch 'main' into joshie/txlookup
gakonst Feb 5, 2023
17c446d
Merge remote-tracking branch 'origin/main' into joshie/txlookup
joshieDo Feb 5, 2023
2dc7259
change comment location
joshieDo Feb 5, 2023
93cceba
use default warm up time for criterion
joshieDo Feb 5, 2023
6a06a05
add batches and table stats/sizes to hash_keys benchmark
joshieDo Feb 5, 2023
0c3dcdd
add description to benchmark
joshieDo Feb 5, 2023
b6cf12c
append_all and append_size
joshieDo Feb 6, 2023
f922c5c
manually impl arbitrary for SealedHeader
joshieDo Feb 8, 2023
229fd91
should append more idiomatic
joshieDo Feb 8, 2023
87bfce3
Update crates/stages/src/stages/tx_lookup.rs
joshieDo Feb 8, 2023
c6a77d4
comment clippy
joshieDo Feb 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/reth/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl Command {
BlockTransitionIndex,
TxTransitionIndex,
SyncStage,
TxHashNumber,
Transactions
]);
}
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ impl Deref for Block {
}

/// Sealed Ethereum full block.
#[derive(Debug, Clone, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
#[derive_arbitrary(rlp, 10)]
#[derive(
Debug, Clone, PartialEq, Eq, Default, RlpEncodable, RlpDecodable, Serialize, Deserialize,
)]
pub struct SealedBlock {
/// Locked block header.
pub header: SealedHeader,
Expand Down
31 changes: 29 additions & 2 deletions crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use bytes::{BufMut, BytesMut};
use ethers_core::types::{Block, H256 as EthersH256, H64};
use reth_codecs::{derive_arbitrary, main_codec, Compact};
use reth_codecs::{add_arbitrary_tests, derive_arbitrary, main_codec, Compact};
use reth_rlp::{length_of_length, Decodable, Encodable};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
Expand Down Expand Up @@ -209,14 +209,41 @@ impl Decodable for Header {

/// A [`Header`] that is sealed at a precalculated hash, use [`SealedHeader::unseal()`] if you want
/// to modify header.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[add_arbitrary_tests(rlp)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SealedHeader {
/// Locked Header fields.
header: Header,
/// Locked Header hash.
hash: BlockHash,
}

#[cfg(any(test, feature = "arbitrary"))]
impl proptest::arbitrary::Arbitrary for SealedHeader {
type Parameters = ();
type Strategy = proptest::strategy::BoxedStrategy<SealedHeader>;

fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
use proptest::prelude::{any, Strategy};

any::<(Header, BlockHash)>()
.prop_map(move |(header, _)| {
let hash = header.hash_slow();
SealedHeader { header, hash }
})
.boxed()
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for SealedHeader {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let header = Header::arbitrary(u)?;
let hash = header.hash_slow();
Ok(SealedHeader { header, hash })
}
}

impl From<Block<EthersH256>> for SealedHeader {
fn from(block: Block<EthersH256>) -> Self {
let header = Header {
Expand Down
6 changes: 2 additions & 4 deletions crates/stages/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ criterion_main!(benches);

fn senders(c: &mut Criterion) {
let mut group = c.benchmark_group("Stages");
group.measurement_time(std::time::Duration::from_millis(2000));
group.warm_up_time(std::time::Duration::from_millis(2000));

// don't need to run each stage for that many times
group.sample_size(10);

Expand All @@ -33,8 +32,7 @@ fn senders(c: &mut Criterion) {

fn tx_lookup(c: &mut Criterion) {
let mut group = c.benchmark_group("Stages");
group.measurement_time(std::time::Duration::from_millis(2000));
group.warm_up_time(std::time::Duration::from_millis(2000));

// don't need to run each stage for that many times
group.sample_size(10);

Expand Down
27 changes: 25 additions & 2 deletions crates/stages/src/stages/tx_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,37 @@ impl<DB: Database> Stage<DB> for TransactionLookupStage {
.unwrap_or_default()
});

// Collect tranasctions for each body and insert the reverse lookup for hash -> tx_id.
// Collect transactions for each body
let mut tx_list = vec![];
for body_entry in bodies {
let (_, body) = body_entry?;
let transactions = tx_cursor.walk(body.start_tx_id)?.take(body.tx_count as usize);

for tx_entry in transactions {
let (id, transaction) = tx_entry?;
tx.put::<tables::TxHashNumber>(transaction.hash(), id)?;
tx_list.push((transaction.hash(), id));
}
}

// Sort before inserting the reverse lookup for hash -> tx_id.
tx_list.sort_by(|txa, txb| txa.0.cmp(&txb.0));

let mut txhash_cursor = tx.cursor_write::<tables::TxHashNumber>()?;

// If the last inserted element in the database is smaller than the first in our set, then
// we can just append into the DB. This probably only ever happens during sync, on
// the first table insertion.
let append = tx_list
.first()
.zip(txhash_cursor.last()?)
.map(|((first, _), (last, _))| &last < first)
.unwrap_or_default();

for (tx_hash, id) in tx_list {
if append {
txhash_cursor.append(tx_hash, id)?;
} else {
txhash_cursor.insert(tx_hash, id)?;
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/storage/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ arbitrary = [
"dep:proptest-derive",
]

[[bench]]
name = "hash_keys"
harness = false

[[bench]]
name = "criterion"
harness = false
Expand Down
Loading