Skip to content

Commit 80f1b69

Browse files
authored
Speed up removing old orders and quotes (#4151)
# Description The incremental solvable orders query so far blindly inserted updated orders in to the set of open orders and then ran a filtering step over ALL open orders and quotes separately. This is pretty wasteful since the new/updated orders are significantly fewer than the set of ALL orders and 3 of 4 things we check we only need to check on the new/updated orders. Also we don't have to go over ALL quotes if we just remove the quotes together with the orders. # Changes - determine whether to insert or remove orders based on the new information returned by the incremental DB query - whenever we remove an order we also remove the associated quote to never run do a `.retain()` on ALL quotes - still do a `.retain()` on the open orders to find expired orders as the incremental orders query will not flag those - while we still scan the whole list twice the most complicated checks could be moved to the part that only runs on the updated orders so this `.retain()` still is a lot faster than before. ## How to test Measured performance with tempo Retain orders went from 2.5ms to 283µs and we dropped the 6.6ms from retaining quotes completely Before <img width="1298" height="298" alt="Screenshot 2026-02-13 at 08 13 38" src="https://github.com/user-attachments/assets/fd375cf9-851b-4a67-be36-4dae00c49461" /> After <img width="1333" height="271" alt="Screenshot 2026-02-13 at 08 13 24" src="https://github.com/user-attachments/assets/2feabd4c-64fa-4b49-ab1b-8bedb42d9711" />
1 parent 0c04766 commit 80f1b69

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

  • crates/autopilot/src/infra/persistence

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -612,20 +612,9 @@ impl Persistence {
612612
.to_u64()
613613
.context("latest_settlement_block is not u64")?;
614614

615-
// Blindly insert all new orders into the cache.
615+
// Insert new / updated orders or remove invalidated orders
616+
// and the associated quote.
616617
for (uid, order) in next_orders {
617-
current_orders.insert(uid, order);
618-
}
619-
620-
// Filter out all the invalid orders.
621-
current_orders.retain(|_uid, order| {
622-
let expired = order.data.valid_to < min_valid_to
623-
|| order
624-
.metadata
625-
.ethflow_data
626-
.as_ref()
627-
.is_some_and(|data| data.user_valid_to < i64::from(min_valid_to));
628-
629618
let invalidated = order.metadata.invalidated;
630619
let onchain_error = order
631620
.metadata
@@ -645,10 +634,30 @@ impl Persistence {
645634
}
646635
};
647636

648-
!expired && !invalidated && !onchain_error && !fulfilled
649-
});
637+
if invalidated || onchain_error || fulfilled {
638+
current_orders.remove(&uid);
639+
current_quotes.remove(&uid);
640+
} else {
641+
current_orders.insert(uid, order);
642+
}
643+
}
644+
645+
// Filter out all the expired orders and their quotes.
646+
current_orders.retain(|uid, order| {
647+
let expired = order.data.valid_to < min_valid_to
648+
|| order
649+
.metadata
650+
.ethflow_data
651+
.as_ref()
652+
.is_some_and(|data| data.user_valid_to < i64::from(min_valid_to));
650653

651-
current_quotes.retain(|uid, _| current_orders.contains_key(uid));
654+
if expired {
655+
current_quotes.remove(uid);
656+
false
657+
} else {
658+
true
659+
}
660+
});
652661

653662
{
654663
let _timer = Metrics::get()

0 commit comments

Comments
 (0)