-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
perf: Speedup coins.AmountOf() by removing many regex calls #10021
Conversation
Codecov Report
@@ Coverage Diff @@
## master #10021 +/- ##
=======================================
Coverage 63.56% 63.57%
=======================================
Files 572 572
Lines 53598 53600 +2
=======================================
+ Hits 34072 34075 +3
+ Misses 17582 17581 -1
Partials 1944 1944
|
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.
LGTM
|
||
// AmountOfNoDenomValidation returns the amount of a denom from coins | ||
// without validating the denomination. | ||
func (coins Coins) AmountOfNoDenomValidation(denom string) Int { |
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.
func (coins Coins) AmountOfNoDenomValidation(denom string) Int { | |
func (coins Coins) AmountOfWithoutValidation(denom string) Int { |
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2 | ||
coin := coins[midIdx] | ||
switch { | ||
case denom < coin.Denom: | ||
return coins[:midIdx].AmountOf(denom) | ||
return coins[:midIdx].AmountOfNoDenomValidation(denom) |
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.
return coins[:midIdx].AmountOfNoDenomValidation(denom) | |
return coins[:midIdx].AmountOfWithoutValidation(denom) |
case denom == coin.Denom: | ||
return coin.Amount | ||
default: | ||
return coins[midIdx+1:].AmountOf(denom) | ||
return coins[midIdx+1:].AmountOfNoDenomValidation(denom) |
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.
return coins[midIdx+1:].AmountOfNoDenomValidation(denom) | |
return coins[midIdx+1:].AmountOfWithoutValidation(denom) |
I personally like the name Granted this clearly won't scale if more validations were required, so happy to change the name per your suggestion if you think its much better. |
Do we need to backport this to v0.44 or 0.42? |
might as well |
@ValarDragon can you update the pr, after which the bot will merge it |
Visit https://dashboard.github.orijtech.com?back=0&pr=10021&remote=false&repo=cosmos%2Fcosmos-sdk to see benchmark details. |
@Mergifyio backport release/v0.44.x |
(cherry picked from commit ed35bfd) # Conflicts: # CHANGELOG.md
Command
|
…osmos#10021) (cosmos#10166) * perf: Speedup coins.AmountOf() by removing many regex calls (cosmos#10021) (cherry picked from commit ed35bfd) # Conflicts: # CHANGELOG.md * fix conflict * fix changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
…osmos#10021) (cosmos#10166) * perf: Speedup coins.AmountOf() by removing many regex calls (cosmos#10021) (cherry picked from commit ed35bfd) # Conflicts: # CHANGELOG.md * fix conflict * fix changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
…osmos#10021) (cosmos#10166) * perf: Speedup coins.AmountOf() by removing many regex calls (cosmos#10021) (cherry picked from commit ed35bfd) # Conflicts: # CHANGELOG.md * fix conflict * fix changelog Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Description
Speeds up coins.AmountOf() by removing many regex calls when users hold multiple tokens. I expose the AmountOfNoDenomValidation function publicly, because this is quite useful when you know that the denomination is pre-validated, as we do in usecases within Osmosis where we call this function.
(This function is a notable amount of our current epoch time after CacheKV store fixes at approximately 5%, all spent within the regex matching)
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...
!
to the type prefix if API or client breaking changeCHANGELOG.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.
I have...
!
in the type prefix if API or client breaking change