-
Notifications
You must be signed in to change notification settings - Fork 4.1k
perf(x/bank): Improve performance of GetAllBalances and GetAccountsBalances. #24148
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
Conversation
…ounts with many different denoms.
📝 WalkthroughWalkthroughThe changes introduce performance improvements in the Cosmos SDK’s bank module. Specifically, the keeper methods Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant BaseViewKeeper
Client->>BaseViewKeeper: Call GetAllBalances(account)
BaseViewKeeper->>BaseViewKeeper: Iterate over balances
BaseViewKeeper->>BaseViewKeeper: Append each balance using slice append
BaseViewKeeper-->>Client: Return unsorted balance slice
sequenceDiagram
participant Client
participant BaseViewKeeper
Client->>BaseViewKeeper: Call GetAccountsBalances(accounts)
loop For each account transaction
BaseViewKeeper->>BaseViewKeeper: Check if last entry matches current address
alt Same address
BaseViewKeeper->>BaseViewKeeper: Append new balance to existing account's Coins slice
else New address
BaseViewKeeper->>BaseViewKeeper: Create new accountBalance entry
end
end
BaseViewKeeper-->>Client: Return consolidated account balances
Suggested reviewers
📜 Recent review detailsConfiguration used: .coderabbit.yml 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (2)`**/*.go`: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
`**/*.md`: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"
⏰ Context from checks skipped due to timeout of 90000ms (2)
🔇 Additional comments (5)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
I discovered the need for this because of an account that has over 10k different coins. The |
Do we have tests that ensure that these queries are still deterministic (in terms of ordering?) |
I don't see any unit tests that test these methods specifically. However, They use the collection iterators, though, so the only way they'd be non-deterministic is if those iterators aren't deterministic. The balances key is The |
It appears that I no longer have the ability to push to this branch, so I cannot update it with recent changes. I'm not complaining, just pointing out that that's why this is still out of date. |
Hey @SpicyLemon - could you re-target this PR against |
My ability to push to the repo has been removed so I'll have to recreate this from a fork in order to be able to update it. I'll close this one once I've done that. |
I recreated this PR so that I could update the branch: |
Description
This PR improves the performance of
GetAllBalances
andGetAccountsBalances
. They were usingCoins.Add
in a loop, which becomes prohibitively expensive as the number of denoms grows. Since the info is coming directly out of state, though, we know there are no duplicated denoms, and they'll be in the correct order already. So we can useappend
instead ofCoins.Add
and bypass a whole lot of copying, comparisons and processing.Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
confirmedN/A!
in the type prefix if API or client breaking changeprovided a link to the relevant issue or specificationN/ACHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.
I have...
Summary by CodeRabbit
This update enhances the performance of account balance queries, resulting in faster and more efficient balance retrieval.