Skip to content
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

Optimize Orderbook loading to use sql rows stream directly #1622

Closed
bartekn opened this issue Aug 21, 2019 · 6 comments · Fixed by #4155
Closed

Optimize Orderbook loading to use sql rows stream directly #1622

bartekn opened this issue Aug 21, 2019 · 6 comments · Fixed by #4155
Assignees
Labels

Comments

@bartekn
Copy link
Contributor

bartekn commented Aug 21, 2019

Currently GetAllOffers is used to load all offers from a database into in-memory order book graph. Currently, there are around 33k offers in the public network however, if this number increases in the future this function may require more memory. Instead, we should stream offers from a DB using sql.Rows. We already use this method in several places in Horizon so check them for reference.

@ire-and-curses
Copy link
Member

Offers have gone up 10x; agree this is important.

@sreuland sreuland self-assigned this Dec 21, 2021
@sreuland
Copy link
Contributor

Hello @bartekn , after some review with @tamirms , it looked like sql.Rows.Next() may already be getting invoked. I stepped through offers.go:GetAllOffers to confirm, looks like it does result in call stack that ends up invoking the same sql.Rows.Next() from other sqlx wrappers, the underlying lib/pg driver enables the streaming behavior on that by buffering frames from server for the result set rather than all up front, keeping mem usage down, successive calls to sql.Rows.Next() trigger accepting new frames from server. So, this should already be performing streaming behavior as requested.

@bartekn
Copy link
Contributor Author

bartekn commented Dec 21, 2021

@sreuland it's possible that rows are streamed in sqlx. The problem is all the stream rows are then added to a result []Offer slice in GetAllOffers. We already have a method in db/support.Sesson that can allow us to do it: Query. It returns *sqlx.Rows we can use to iterate on rows one by one. I was thinking we use it already in certain places in Horizon but we don't (maybe these were removed) but we should start using Query here. Let me know if you have further questions.

@sreuland
Copy link
Contributor

Ok, I may have misread on expectation, is the request to address the receiving []Offer slice growing too large after the rows have been scanned in? Just to confirm the call stack currently results in the same iteration of rows:

Screen Shot 2021-12-21 at 3 00 27 PM

iteration of results with sql.Rows.Next() is encapsulated in sqlx.scanAll(), are you requesting to do the iteration on sql.Rows.Next() directly in GetAllOffers instead, not sure of net benefit? Let's try call tomorrow, will get confirmation, thanks!

@bartekn
Copy link
Contributor Author

bartekn commented Dec 22, 2021

I'm suggesting to change GetAllOffers to return sqlx.Rows and them to the graph one by one. This way we won't need to keep all offers in memory at any point in time.

@sreuland
Copy link
Contributor

Ok, got it, the loading of the orderboook graph should be refactored to load direct from sql.Rows iteration and remove the intermediate step of loading the sql.Rows to a slice first. Also, during investigation on this with @tamirms, he mentioned it would likely be worthwhile to do the same refactor for GetAllLiquidityPools as it follows the same graph loading pattern from sql rows. Let me know if this is not accurate, otherwise, I should be able to proceed, thanks!

@sreuland sreuland changed the title GetAllOffers should be streamable Optimize Orderbook loading to use sql rows stream directly Dec 22, 2021
sreuland added a commit to sreuland/go that referenced this issue Jan 3, 2022
sreuland added a commit to sreuland/go that referenced this issue Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants