Skip to content

reduce realloc memory #231

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
@@ -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()
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;
@@ -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;
@@ -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);
8 changes: 6 additions & 2 deletions crates/rbuilder/src/backtest/redistribute/mod.rs
Original file line number Diff line number Diff line change
@@ -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));
@@ -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,
Original file line number Diff line number Diff line change
@@ -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);
@@ -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);
}
1 change: 1 addition & 0 deletions crates/rbuilder/src/building/block_orders/mod.rs
Original file line number Diff line number Diff line change
@@ -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)?
Original file line number Diff line number Diff line change
@@ -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(),
Original file line number Diff line number Diff line change
@@ -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)]
@@ -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<_>>();
Original file line number Diff line number Diff line change
@@ -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));
4 changes: 2 additions & 2 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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)?);
}
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion crates/rbuilder/src/live_builder/simulation/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion crates/rbuilder/src/primitives/order_builder.rs
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion crates/rbuilder/src/primitives/test_data_generator.rs
Original file line number Diff line number Diff line change
@@ -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 {
8 changes: 5 additions & 3 deletions crates/rbuilder/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
@@ -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();
2 changes: 1 addition & 1 deletion crates/rbuilder/src/validation_api_client.rs
Original file line number Diff line number Diff line change
@@ -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()?)));
}