-
Notifications
You must be signed in to change notification settings - Fork 93
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
Do not count "in-market" orders for the order limit #2433 #2456
Conversation
8ab5e22
to
0d0dc43
Compare
43161bd
to
36dc6b3
Compare
36dc6b3
to
f74923b
Compare
pub quote_sell_token_price: f64, | ||
} | ||
|
||
pub async fn user_orders_with_quote( |
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.
There is already a SQL query that counts limit orders. Instead of fetching all this data and counting in the rust code you could have the in-market check already in SQL to only return a single number.
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.
@MartinquaXD how could we do that? we need to precisely operate the f64
so we can consider the limit
order as "out of market" 🤔 do you mean by doing the operation directly in SQL?
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.
Basically reimplement the logic of is_order_outside_market_price()
inside count_limit_orders_by_owner()
. Hopefully there is a way in PSQL to convert the types appropriately for the computation.
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.
@MartinquaXD In my opinion, doing arithmetical operations in PSQL query involving U256 and f64 is really not a good idea (unless it is plain sum, and even so), we are going to lose precision, and we are going to push CPU overhead to the database. What's more, I don't think we will save time, since database CPU is less powerful than the backend CPU, and the rust code will be definitely faster.
I understand your concern of potential long queries in case the user placed many orders, for that please, consider adding the internal class (as I proposed here #2456 (comment)), which will tackle down all the issues. We could also do the queries by pagination in case of long queries.
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.
not a good idea (unless it is plain sum
Interesting. Why do you think so?
we are going to lose precision
This is also the case in the rust code since.fee()
calls U256::from_f64_lossy()
.
I don't think we will save time, since database CPU is less powerful than the backend CPU, and the rust code will be definitely faster.
Computations might be faster in rust but I very much doubt that returning all that data, parsing it, allocating the structs (1 Vec per BigDecimal) and doing the computation in rust will be faster than doing a computation inside the db and returning a single number.
This is not a hill I'm willing to die on (although I disagree) if you feel strongly about keeping the computation out of the DB I'd at least consider fetching the data using a stream instead of call .fetch_all()
to limit the memory consumption when a user has lots of orders.
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.
I guess we would need to benchmark this in order to get a definitive answer. It's true that for accounts with a lot of orders this may become a bottleneck. In theory we could also decide the "in-marketness" of an order when it's inserted into the db (and then simply count based on a flag)?
I'd be curious how PSQL math is less accurate than rust math?
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.
Looks good in general to me. Not too sure how much work it would be to decide the in-marketness at insertion time (we can run a DB migration to update historic orders).
pub quote_sell_token_price: f64, | ||
} | ||
|
||
pub async fn user_orders_with_quote( |
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.
I guess we would need to benchmark this in order to get a definitive answer. It's true that for accounts with a lot of orders this may become a bottleneck. In theory we could also decide the "in-marketness" of an order when it's inserted into the db (and then simply count based on a flag)?
I'd be curious how PSQL math is less accurate than rust math?
@fleupold @MartinquaXD implementation of the same standard IEEE 754 does not guarantee exactly same behavior. For example, the rounding is the same, but when the rounding is done, may differ. The standard doesn't define many corner cases which makes each implementation slightly different. Also the compilation and optimizations impacts it a lot, actually same code of Rust can vary Interesting articles (I actually didn't know many things of those articles, it is quite interesting):
That's why I do not like splitting arithmetic business logic between PSQL and Rust. Well, that's also why I don't like |
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.
Approving since we discussed via DMs that the next task will be to refactor how we store order classes (while getting rid of liquidity orders) which will make the whole discussion obsolete.
@fleupold @MartinquaXD the issue to fully fix it and eliminate the order class, as agreed: #2469 |
Description
Only check for order limits when the orders are class "Limit". I kept the legacy code/behavior, in order not to make a regression breaking change.
Changes
How to test
Fixes #2433