-
Notifications
You must be signed in to change notification settings - Fork 178
refactor: reduce allocations in the sim task #842
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
prestwich
wants to merge
1
commit into
flashbots:develop
Choose a base branch
from
prestwich:pretwich/reduce-clones
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
β16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,13 @@ use rbuilder_primitives::{Order, OrderId, SimulatedOrder}; | |
| use reth_errors::ProviderError; | ||
| use reth_provider::StateProvider; | ||
| use std::{ | ||
| borrow::Cow, | ||
| cmp::{max, min, Ordering}, | ||
| collections::hash_map::Entry, | ||
| sync::Arc, | ||
| time::{Duration, Instant}, | ||
| }; | ||
| use tracing::{error, trace}; | ||
| use tracing::{error, instrument, trace}; | ||
|
|
||
| #[derive(Debug)] | ||
| #[allow(clippy::large_enum_variant)] | ||
|
|
@@ -41,7 +42,7 @@ pub struct OrderSimResultWithGas { | |
| pub gas_used: u64, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct NonceKey { | ||
| pub address: Address, | ||
| pub nonce: u64, | ||
|
|
@@ -226,11 +227,12 @@ impl SimTree { | |
| result: SimulatedResult, | ||
| ) -> Result<(), ProviderError> { | ||
| self.sims.insert(result.id, result.clone()); | ||
|
|
||
| let mut orders_ready = Vec::new(); | ||
| if result.nonces_after.len() == 1 { | ||
| let updated_nonce = result.nonces_after.first().unwrap().clone(); | ||
| let updated_nonce = result.nonces_after.first().unwrap(); | ||
|
|
||
| match self.sims_that_update_one_nonce.entry(updated_nonce.clone()) { | ||
| match self.sims_that_update_one_nonce.entry(*updated_nonce) { | ||
| Entry::Occupied(mut entry) => { | ||
| let current_sim_profit = { | ||
| let sim_id = entry.get_mut(); | ||
|
|
@@ -255,7 +257,7 @@ impl SimTree { | |
| Entry::Vacant(entry) => { | ||
| entry.insert(result.id); | ||
|
|
||
| if let Some(pending_orders) = self.pending_nonces.remove(&updated_nonce) { | ||
| if let Some(pending_orders) = self.pending_nonces.remove(updated_nonce) { | ||
| for order in pending_orders { | ||
| match self.pending_orders.entry(order) { | ||
| Entry::Occupied(mut entry) => { | ||
|
|
@@ -364,8 +366,8 @@ where | |
| let start_time = Instant::now(); | ||
| let mut block_state = BlockState::new_arc(state_for_sim); | ||
| let sim_result = simulate_order( | ||
| sim_task.parents.clone(), | ||
| sim_task.order.clone(), | ||
| &sim_task.parents, | ||
| Cow::Borrowed(&sim_task.order), | ||
| ctx, | ||
| &mut local_ctx, | ||
| &mut block_state, | ||
|
|
@@ -412,9 +414,10 @@ where | |
| } | ||
|
|
||
| /// Prepares context (fork + tracer) and calls simulate_order_using_fork | ||
| #[instrument(skip_all, level = "debug", fields(order = ?order.id()))] | ||
| pub fn simulate_order( | ||
| parent_orders: Vec<Order>, | ||
| order: Order, | ||
| parent_orders: &[Order], | ||
| order: Cow<'_, Order>, | ||
| ctx: &BlockBuildingContext, | ||
| local_ctx: &mut ThreadBlockBuildingContext, | ||
| state: &mut BlockState, | ||
|
|
@@ -434,8 +437,8 @@ pub fn simulate_order( | |
|
|
||
| /// Simulates order (including parent (those needed to reach proper nonces) orders) using a precreated fork | ||
| pub fn simulate_order_using_fork<Tracer: SimulationTracer>( | ||
| parent_orders: Vec<Order>, | ||
| order: Order, | ||
| parent_orders: &[Order], | ||
| order: Cow<'_, Order>, | ||
| fork: &mut PartialBlockFork<'_, '_, '_, '_, Tracer, NullPartialBlockForkExecutionTracer>, | ||
| mempool_tx_detector: &MempoolTxsDetector, | ||
| ) -> Result<OrderSimResult, CriticalCommitOrderError> { | ||
|
|
@@ -446,7 +449,7 @@ pub fn simulate_order_using_fork<Tracer: SimulationTracer>( | |
| // not change from batching. | ||
| let combined_refunds = std::collections::HashMap::default(); | ||
| for parent in parent_orders { | ||
| let result = fork.commit_order(&parent, space_state, true, &combined_refunds)?; | ||
| let result = fork.commit_order(parent, space_state, true, &combined_refunds)?; | ||
| match result { | ||
| Ok(res) => { | ||
| space_state.use_space(res.space_used); | ||
|
|
@@ -472,7 +475,7 @@ pub fn simulate_order_using_fork<Tracer: SimulationTracer>( | |
| let new_nonces = res.nonces_updated.into_iter().collect::<Vec<_>>(); | ||
| Ok(OrderSimResult::Success( | ||
| Arc::new(SimulatedOrder { | ||
| order, | ||
| order: order.into_owned(), | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using the Cow allows us to defer the clone to here. This means the clone is eliminated in the (presumably common) case that simulation fails |
||
| sim_value, | ||
| used_state_trace: res.used_state_trace, | ||
| }), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using the reference allows us to not clone the parents, saving a potentially very large allocation