perf: use par_sort on TxLookup stage (#2137)

This commit is contained in:
joshieDo
2023-04-11 21:56:54 +08:00
committed by GitHub
parent ccf540ea8d
commit 0a34f28bfb

View File

@@ -2,6 +2,7 @@ use crate::{
exec_or_return, ExecAction, ExecInput, ExecOutput, Stage, StageError, StageId, UnwindInput,
UnwindOutput,
};
use rayon::prelude::*;
use reth_db::{
cursor::{DbCursorRO, DbCursorRW},
database::Database,
@@ -62,20 +63,26 @@ impl<DB: Database> Stage<DB> for TransactionLookupStage {
// Walk over block bodies within a specified range.
let bodies = block_meta_cursor.walk_range(start_block..=end_block)?;
// Collect transactions for each body
let mut tx_list = vec![];
let mut total_transactions = 0;
let mut tx_ranges = Vec::with_capacity((end_block - start_block) as usize);
for block_meta_entry in bodies {
let (_, block_meta) = block_meta_entry?;
let transactions = tx_cursor.walk_range(block_meta.tx_num_range())?;
total_transactions += block_meta.tx_count;
tx_ranges.push(block_meta.tx_num_range());
}
for tx_entry in transactions {
// Collect transactions for each body
let mut tx_list = Vec::with_capacity(total_transactions as usize);
for tx_num_range in tx_ranges {
for tx_entry in tx_cursor.walk_range(tx_num_range)? {
let (id, transaction) = tx_entry?;
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));
tx_list.par_sort_unstable_by(|txa, txb| txa.0.cmp(&txb.0));
let mut txhash_cursor = tx.cursor_write::<tables::TxHashNumber>()?;