Skip to content

Commit

Permalink
chore: reduce realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Oct 28, 2024
1 parent 112fb17 commit 4b41e75
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ fn print_onchain_block_data(
orders: &[Order],
block_data: &BlockData,
) {
let mut executed_orders = Vec::new();
let mut executed_orders = Vec::with_capacity(tx_sim_results.len());

let txs_to_idx: HashMap<_, _> = tx_sim_results
.iter()
Expand Down
12 changes: 5 additions & 7 deletions crates/rbuilder/src/backtest/fetch/flashbots_db.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::backtest::BuiltBlockData;
use crate::primitives::OrderId;
use crate::{
backtest::{
fetch::data_source::{BlockRef, DataSource, DatasourceData},
OrdersWithTimestamp,
BuiltBlockData, OrdersWithTimestamp,
},
primitives::{
serialize::{RawBundle, RawOrder, RawShareBundle, TxEncoding},
Order, SimValue,
Order, OrderId, SimValue,
},
};
use alloy_primitives::I256;
Expand All @@ -19,8 +17,7 @@ use bigdecimal::{
use eyre::WrapErr;
use reth_primitives::{Bytes, B256, U256, U64};
use sqlx::postgres::PgPool;
use std::collections::HashSet;
use std::{ops::Mul, str::FromStr};
use std::{collections::HashSet, ops::Mul, str::FromStr};
use time::{OffsetDateTime, PrimitiveDateTime};
use tracing::trace;
use uuid::Uuid;
Expand Down Expand Up @@ -333,7 +330,8 @@ impl RelayDB {
.fetch_all(&self.pool)
.await?;

let mut included_orders = Vec::new();
let mut included_orders =
Vec::with_capacity(included_bundles.len() + included_sbundles.len());
for (bundle_uuid,) in included_bundles {
let order_id = OrderId::Bundle(bundle_uuid);
included_orders.push(order_id);
Expand Down
8 changes: 6 additions & 2 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ where
})
.collect::<Vec<_>>();

let mut simplified_orders = Vec::new();
let mut simplified_orders = Vec::with_capacity(included_orders_available.len());

for available_order in included_orders_available {
simplified_orders.push(SimplifiedOrder::new_from_order(&available_order.order));
Expand Down Expand Up @@ -880,7 +880,11 @@ fn calc_inclusion_change(
exclusion_result: &ExclusionResult,
included_before: &[(OrderId, U256)],
) -> Vec<OrderInclusionChange> {
let mut result = Vec::new();
let mut result = Vec::with_capacity(
exclusion_result.new_orders_included.len()
+ exclusion_result.new_orders_failed.len()
+ exclusion_result.orders_profit_changed.len(),
);
for (id, profit_after) in &exclusion_result.new_orders_included {
result.push((
*id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ pub fn calculate_redistribution(data: RedistributionCalculator) -> Redistributio
}

let mut total_value_redistributed = U256::ZERO;
let mut redistribution_entity_result = Vec::new();
let mut redistribution_entity_result = Vec::with_capacity(n);
for i in 0..n {
let mut order_id_vector = Vec::new();
let mut order_contrib_vector = Vec::new();
let mut order_id_vector = Vec::with_capacity(data.identity_data[i].included_orders.len());
let mut order_contrib_vector =
Vec::with_capacity(data.identity_data[i].included_orders.len());
for landed_order in &data.identity_data[i].included_orders {
order_id_vector.push(landed_order.id);
order_contrib_vector.push(landed_order.realized_value);
Expand Down Expand Up @@ -230,7 +231,7 @@ fn split_value(value: U256, split_vector: &[U256]) -> Vec<U256> {
if total_split.is_zero() {
return split_vector.iter().map(|_| U256::ZERO).collect();
}
let mut result = Vec::new();
let mut result = Vec::with_capacity(split_vector.len());
for split in split_vector {
result.push((value * split) / total_split);
}
Expand Down
1 change: 1 addition & 0 deletions crates/rbuilder/src/building/block_orders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub fn block_orders_from_sim_orders(
) -> ProviderResult<BlockOrders> {
let mut onchain_nonces = vec![];
for order in sim_orders {
onchain_nonces.reserve_exact(order.order.nonces().len());
for nonce in order.order.nonces() {
let value = state_provider
.account_nonce(nonce.address)?
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/building/block_orders/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<TestedSinkType: SimulatedOrderSink> TestContext<TestedSinkType> {
/// tx is the same in all backruns
pub fn create_multiple_sbundle_tx_br(&mut self, sbundle_count: usize) -> Vec<ShareBundle> {
let tx = self.create_share_bundle_tx_bundle(TxRevertBehavior::AllowedExcluded);
let mut res = Vec::new();
let mut res = Vec::with_capacity(sbundle_count);
for _ in 0..sbundle_count {
let body = vec![
tx.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use std::sync::Arc;
use tokio_util::sync::CancellationToken;
use tracing::trace;

use super::simulation_cache::{CachedSimulationState, SharedSimulationCache};
use super::{Algorithm, ConflictTask, ResolutionResult};
use super::{
simulation_cache::{CachedSimulationState, SharedSimulationCache},
Algorithm, ConflictTask, ResolutionResult,
};

use crate::building::{BlockBuildingContext, BlockState, PartialBlock};
use crate::building::{ExecutionError, ExecutionResult};
use crate::primitives::{OrderId, SimulatedOrder};
use crate::{
building::{BlockBuildingContext, BlockState, ExecutionError, ExecutionResult, PartialBlock},
primitives::{OrderId, SimulatedOrder},
};

/// Context for resolving conflicts in merging tasks.
#[derive(Debug)]
Expand Down Expand Up @@ -352,7 +355,7 @@ fn generate_sequences_of_orders_to_try(task: &ConflictTask) -> Vec<Vec<usize>> {
///
/// A vector of randomly generated sequences of order indices.
fn generate_random_permutations(task: &ConflictTask, seed: u64, count: usize) -> Vec<Vec<usize>> {
let mut sequences_of_orders = vec![];
let mut sequences_of_orders = Vec::with_capacity(count);

let order_group = &task.group;
let mut indexes = (0..order_group.orders.len()).collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
}
return Ok(Vec::new());
}
let mut res = Vec::new();
let mut res = Vec::with_capacity(new_block.saturating_sub(self.block_number) as usize);
for block_number in self.block_number + 1..=new_block {
let block_info = self.get_block_info(block_number)?;
res.push(block_info.as_landed_block_info(&self.builder_addr));
Expand Down
4 changes: 2 additions & 2 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ use reth_provider::{
};
use serde::Deserialize;
use serde_with::{serde_as, OneOrMany};
use std::fmt::Debug;
use std::{
fmt::Debug,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl L1Config {
}

pub fn create_relays(&self) -> eyre::Result<Vec<MevBoostRelay>> {
let mut results = Vec::new();
let mut results = Vec::with_capacity(self.relays.len());
for relay in &self.relays {
results.push(MevBoostRelay::from_config(relay)?);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl PayloadSourceMuxer {
cancellation: CancellationToken,
) -> Self {
let (sender, receiver) = mpsc::unbounded_channel();
let mut join_handles: Vec<JoinHandle<()>> = Vec::new();
let mut join_handles: Vec<JoinHandle<()>> = Vec::with_capacity(cls.len());
for cl in cls {
let sender = sender.clone();
let cancellation = cancellation.clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/live_builder/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
current_contexts: Arc::new(Mutex::new(CurrentSimulationContexts {
contexts: HashMap::default(),
})),
worker_threads: Vec::new(),
worker_threads: Vec::with_capacity(num_workers),
};
for i in 0..num_workers {
let ctx = Arc::clone(&result.current_contexts);
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/primitives/order_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl BundleBuilder {

fn build(self) -> Bundle {
let mut reverting_tx_hashes = Vec::new();
let mut txs = Vec::new();
let mut txs = Vec::with_capacity(self.txs.len());
for (tx_with_blobs, opt) in self.txs {
if opt {
reverting_tx_hashes.push(tx_with_blobs.tx.hash);
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/primitives/test_data_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TestDataGenerator {
replacement_data: Option<BundleReplacementData>,
) -> Bundle {
let mut reverting_tx_hashes = Vec::new();
let mut txs = Vec::new();
let mut txs = Vec::with_capacity(txs_info.len());
for tx_info in txs_info {
let tx1 = self.create_tx_with_blobs_nonce(tx_info.nonce.clone());
if tx_info.optional {
Expand Down
8 changes: 5 additions & 3 deletions crates/rbuilder/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use alloy_primitives::{Address, Sign, I256, U256};
use alloy_provider::RootProvider;
use alloy_transport::BoxTransport;

use crate::primitives::serialize::{RawTx, TxEncoding};
use crate::primitives::TransactionSignedEcRecoveredWithBlobs;
use crate::primitives::{
serialize::{RawTx, TxEncoding},
TransactionSignedEcRecoveredWithBlobs,
};
use alloy_consensus::TxEnvelope;
use alloy_eips::eip2718::Encodable2718;
pub use noncer::{NonceCache, NonceCacheRef};
Expand Down Expand Up @@ -228,7 +230,7 @@ pub fn find_suggested_fee_recipient(
pub fn extract_onchain_block_txs(
onchain_block: &alloy_rpc_types::Block,
) -> eyre::Result<Vec<TransactionSignedEcRecoveredWithBlobs>> {
let mut result = Vec::new();
let mut result = Vec::with_capacity(onchain_block.transactions.len());
for tx in onchain_block.transactions.clone().into_transactions() {
let tx_envelope: TxEnvelope = tx.try_into()?;
let encoded = tx_envelope.encoded_2718();
Expand Down
2 changes: 1 addition & 1 deletion crates/rbuilder/src/validation_api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Debug for ValidationError {

impl ValidationAPIClient {
pub fn new(urls: &[&str]) -> eyre::Result<Self> {
let mut providers = Vec::new();
let mut providers = Vec::with_capacity(urls.len());
for url in urls {
providers.push(Arc::new(http_provider(url.parse()?)));
}
Expand Down

0 comments on commit 4b41e75

Please sign in to comment.