diff --git a/crates/stages/src/stages/tx_lookup.rs b/crates/stages/src/stages/tx_lookup.rs index b804f6e420..a88b925886 100644 --- a/crates/stages/src/stages/tx_lookup.rs +++ b/crates/stages/src/stages/tx_lookup.rs @@ -75,14 +75,17 @@ impl Stage for TransactionLookupStage { let tx_walker = tx_cursor.walk_range(first_block.first_tx_num()..=last_block.last_tx_num())?; - let mut channels = Vec::new(); + let chunk_size = 100_000 / rayon::current_num_threads(); + let mut channels = Vec::with_capacity(chunk_size); + let mut transaction_count = 0; - for chunk in &tx_walker.chunks(100_000 / rayon::current_num_threads()) { + for chunk in &tx_walker.chunks(chunk_size) { let (tx, rx) = mpsc::unbounded_channel(); channels.push(rx); // Note: Unfortunate side-effect of how chunk is designed in itertools (it is not Send) let chunk: Vec<_> = chunk.collect(); + transaction_count += chunk.len(); // closure that will calculate the TxHash let calculate_hash = @@ -105,8 +108,7 @@ impl Stage for TransactionLookupStage { }); } - let mut tx_list = - Vec::with_capacity((last_block.last_tx_num() - first_block.first_tx_num()) as usize); + let mut tx_list = Vec::with_capacity(transaction_count); // Iterate over channels and append the tx hashes to be sorted out later for mut channel in channels { diff --git a/crates/storage/db/src/tables/models/blocks.rs b/crates/storage/db/src/tables/models/blocks.rs index c627343078..c6f80b6e34 100644 --- a/crates/storage/db/src/tables/models/blocks.rs +++ b/crates/storage/db/src/tables/models/blocks.rs @@ -40,6 +40,9 @@ impl StoredBlockBodyIndices { } /// First transaction index. + /// + /// Caution: If the block is empty, this is the number of the first transaction + /// in the next non-empty block. pub fn first_tx_num(&self) -> TxNumber { self.first_tx_num }