Skip to content

Commit

Permalink
Optimized BalanceManager::clone_and_subtract_not_approved_data()
Browse files Browse the repository at this point in the history
It's no needed vector of `OrderRef` now and can work with any iterator. That helps to not allocating order's vector for calling this method.
  • Loading branch information
MichaelWats0n committed Nov 16, 2022
1 parent 94e9759 commit e91ffde
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
48 changes: 23 additions & 25 deletions core/src/balance/manager/balance_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl BalanceManager {

pub fn clone_and_subtract_not_approved_data(
this: Arc<Mutex<Self>>,
orders: Option<Vec<OrderRef>>,
orders: Option<&mut dyn Iterator<Item = &'_ OrderRef>>,
) -> Result<Arc<Mutex<BalanceManager>>> {
let balance_manager = Self::custom_clone(this);

Expand All @@ -463,40 +463,38 @@ impl BalanceManager {
.map(|(id, reservation)| (*id, reservation.clone()))
.collect();

let orders_to_subtract = orders.unwrap_or_default();

let mut applied_orders = HashSet::new();
for order in orders_to_subtract {
let (is_finished, order_type, client_order_id, reservation_id, status) =
order.fn_ref(|x| {
if let Some(orders_to_subtract) = orders {
let mut applied_orders = HashSet::new();
for order in orders_to_subtract {
let (order_type, client_order_id, reservation_id, status) = order.fn_ref(|x| {
(
x.props.is_finished(),
x.header.order_type,
x.header.client_order_id.clone(),
x.header.reservation_id,
x.props.status,
)
});

if is_finished || status == OrderStatus::Creating {
continue;
}

if order_type == OrderType::Market {
bail!("Clone doesn't support market orders because we need to know the price")
}
if status.is_finished() || status == OrderStatus::Creating {
continue;
}

if applied_orders.insert(client_order_id.clone()) {
let reservation_id = match reservation_id {
Some(reservation_id) => reservation_id,
None => continue,
};
if order_type == OrderType::Market {
bail!("Clone doesn't support market orders because we need to know the price")
}

bm_locked.unreserve_by_client_order_id(
reservation_id,
client_order_id,
order.amount(),
)?
if applied_orders.insert(client_order_id.clone()) {
let reservation_id = match reservation_id {
Some(reservation_id) => reservation_id,
None => continue,
};

bm_locked.unreserve_by_client_order_id(
reservation_id,
client_order_id,
order.amount(),
)?
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/balance/manager/tests/balance_manager_derivative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down Expand Up @@ -1976,7 +1976,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down
10 changes: 5 additions & 5 deletions core/src/balance/manager/tests/balance_manager_ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3164,7 +3164,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down Expand Up @@ -3268,7 +3268,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down Expand Up @@ -3386,7 +3386,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down Expand Up @@ -3957,7 +3957,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref]),
Some(&mut vec![order_ref].iter()),
)
.expect("in test");

Expand Down Expand Up @@ -4096,7 +4096,7 @@ mod tests {
.as_ref()
.expect("in test")
.clone(),
Some(vec![order_ref_1, order_ref_2, order_ref_3]),
Some(&mut vec![order_ref_1, order_ref_2, order_ref_3].iter()),
)
.expect("in test");

Expand Down
2 changes: 1 addition & 1 deletion examples/strategies/src/example_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl ExampleStrategy {

let balance_manager = BalanceManager::clone_and_subtract_not_approved_data(
self.engine_context.balance_manager.clone(),
Some(orders),
Some(&mut orders.iter()),
)
.expect("ExampleStrategy::calc_trading_context_by_side: failed to clone and subtract not approved data for BalanceManager");

Expand Down

0 comments on commit e91ffde

Please sign in to comment.