Skip to content

Speed up removing old orders and quotes#4151

Merged
MartinquaXD merged 2 commits intomainfrom
update-orders-smarter
Feb 13, 2026
Merged

Speed up removing old orders and quotes#4151
MartinquaXD merged 2 commits intomainfrom
update-orders-smarter

Conversation

@MartinquaXD
Copy link
Contributor

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
Screenshot 2026-02-13 at 08 13 38

After
Screenshot 2026-02-13 at 08 13 24

@MartinquaXD MartinquaXD requested a review from a team as a code owner February 13, 2026 08:16
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

No critical issues found

.is_some_and(|data| data.user_valid_to < i64::from(min_valid_to));

current_quotes.retain(|uid, _| current_orders.contains_key(uid));
if expired {
Copy link
Contributor

@squadgazzz squadgazzz Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I am following. Previously the filter was !expired && !invalidated && !onchain_error && !fulfilled for both current and next orders. With this change, this query works only for next orders, and current orders are only checked for expiration. Is that correct? Did I miss something, or do we now ignore the !invalidated && !onchain_error && !fulfilled filter for the current orders?

Copy link
Contributor Author

@MartinquaXD MartinquaXD Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only filter reason that can change without the DB query telling us about it is expired that's why that's the only check we always run on the entire set of orders.
expired, invalidated, and fulfilled can only change if the DB gives us new data about the orders (e.g. fulfilled can only change when we index a new trade event) which is why we only have to run those checks on the small subset of orders we get from the DB.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense, thanks!

@MartinquaXD MartinquaXD added this pull request to the merge queue Feb 13, 2026
Merged via the queue into main with commit 80f1b69 Feb 13, 2026
19 checks passed
@MartinquaXD MartinquaXD deleted the update-orders-smarter branch February 13, 2026 14:25
@github-actions github-actions bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants