Skip to content

Commit a2aff43

Browse files
committed
Wrap orders and quotes in Arc to make clones cheap
1 parent 4bbb8ef commit a2aff43

File tree

5 files changed

+221
-174
lines changed

5 files changed

+221
-174
lines changed

crates/autopilot/src/boundary/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ pub use {
2222
},
2323
shared::order_validation::{Amounts, is_order_outside_market_price},
2424
};
25-
use {crate::domain, ethrpc::Web3, std::collections::HashMap, url::Url};
25+
use {
26+
crate::domain,
27+
ethrpc::Web3,
28+
std::{collections::HashMap, sync::Arc},
29+
url::Url,
30+
};
2631

2732
pub mod events;
2833
pub mod order;
@@ -33,8 +38,8 @@ pub fn web3_client(ethrpc: &Url, ethrpc_args: &shared::ethrpc::Arguments) -> Web
3338
}
3439

3540
pub struct SolvableOrders {
36-
pub orders: HashMap<domain::OrderUid, model::order::Order>,
37-
pub quotes: HashMap<domain::OrderUid, domain::Quote>,
41+
pub orders: HashMap<domain::OrderUid, Arc<model::order::Order>>,
42+
pub quotes: HashMap<domain::OrderUid, Arc<domain::Quote>>,
3843
pub latest_settlement_block: u64,
3944
/// Used as a checkpoint - meaning at this point in time
4045
/// **at least** the stored orders were present in the system.

crates/autopilot/src/database/auction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use {
1111
event_storing_helpers::{create_db_search_parameters, create_quote_row},
1212
order_quoting::{QuoteData, QuoteSearchParameters, QuoteStoring},
1313
},
14-
std::{collections::HashMap, ops::DerefMut},
14+
std::{collections::HashMap, ops::DerefMut, sync::Arc},
1515
};
1616

1717
#[async_trait::async_trait]
@@ -75,11 +75,11 @@ impl Postgres {
7575
sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
7676
.execute(ex.deref_mut())
7777
.await?;
78-
let orders: HashMap<domain::OrderUid, Order> =
78+
let orders: HashMap<domain::OrderUid, Arc<Order>> =
7979
database::orders::solvable_orders(&mut ex, i64::from(min_valid_to))
8080
.map(|result| match result {
8181
Ok(order) => full_order_into_model_order(order)
82-
.map(|order| (domain::OrderUid(order.metadata.uid.0), order)),
82+
.map(|order| (domain::OrderUid(order.metadata.uid.0), Arc::new(order))),
8383
Err(err) => Err(anyhow::Error::from(err)),
8484
})
8585
.try_collect()

crates/autopilot/src/database/quotes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use {
88
database::byte_array::ByteArray,
99
shared::maintenance::Maintaining,
1010
sqlx::types::chrono::{DateTime, Utc},
11-
std::collections::HashMap,
11+
std::{collections::HashMap, sync::Arc},
1212
};
1313

1414
impl Postgres {
@@ -29,7 +29,7 @@ impl Postgres {
2929
pub async fn read_quotes(
3030
&self,
3131
orders: impl Iterator<Item = &domain::OrderUid>,
32-
) -> Result<HashMap<domain::OrderUid, domain::Quote>, sqlx::Error> {
32+
) -> Result<HashMap<domain::OrderUid, Arc<domain::Quote>>, sqlx::Error> {
3333
let _timer = super::Metrics::get()
3434
.database_queries
3535
.with_label_values(&["read_quotes"])
@@ -47,7 +47,7 @@ impl Postgres {
4747
tracing::warn!(?order_uid, ?err, "failed to convert quote from db")
4848
})
4949
.ok()
50-
.map(|quote| (order_uid, quote))
50+
.map(|quote| (order_uid, Arc::new(quote)))
5151
})
5252
.collect();
5353

crates/autopilot/src/infra/persistence/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,11 @@ impl Persistence {
507507
.iter()
508508
.cloned()
509509
.map(|policy| {
510-
dto::fee_policy::try_into_domain(policy, quotes.get(order))
511-
.map_err(|err| error::Auction::InvalidFeePolicy(err, *order))
510+
dto::fee_policy::try_into_domain(
511+
policy,
512+
quotes.get(order).map(|v| &**v),
513+
)
514+
.map_err(|err| error::Auction::InvalidFeePolicy(err, *order))
512515
})
513516
.collect::<Result<Vec<_>, _>>()?,
514517
None => vec![],
@@ -541,8 +544,8 @@ impl Persistence {
541544
/// order creation timestamp, and minimum validity period.
542545
pub async fn solvable_orders_after(
543546
&self,
544-
mut current_orders: HashMap<domain::OrderUid, model::order::Order>,
545-
mut current_quotes: HashMap<domain::OrderUid, domain::Quote>,
547+
mut current_orders: HashMap<domain::OrderUid, Arc<model::order::Order>>,
548+
mut current_quotes: HashMap<domain::OrderUid, Arc<domain::Quote>>,
546549
after_timestamp: DateTime<Utc>,
547550
after_block: u64,
548551
min_valid_to: u32,
@@ -570,7 +573,7 @@ impl Persistence {
570573

571574
// Fetch the orders that were updated after the given block and were created or
572575
// cancelled after the given timestamp.
573-
let next_orders: HashMap<domain::OrderUid, model::order::Order> = {
576+
let next_orders: HashMap<domain::OrderUid, Arc<model::order::Order>> = {
574577
let _timer = Metrics::get()
575578
.database_queries
576579
.with_label_values(&["open_orders_after"])
@@ -583,7 +586,7 @@ impl Persistence {
583586
)
584587
.map(|result| match result {
585588
Ok(order) => full_order_into_model_order(order)
586-
.map(|order| (domain::OrderUid(order.metadata.uid.0), order)),
589+
.map(|order| (domain::OrderUid(order.metadata.uid.0), Arc::new(order))),
587590
Err(err) => Err(anyhow::Error::from(err)),
588591
})
589592
.try_collect()
@@ -657,7 +660,7 @@ impl Persistence {
657660
let order_uid = domain::OrderUid(quote.order_uid.0);
658661
match dto::quote::into_domain(quote) {
659662
Ok(quote) => {
660-
current_quotes.insert(order_uid, quote);
663+
current_quotes.insert(order_uid, Arc::new(quote));
661664
}
662665
Err(err) => tracing::warn!(?order_uid, ?err, "failed to convert quote from db"),
663666
}

0 commit comments

Comments
 (0)