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

perf: Speedup coins.AmountOf() by removing many regex calls #10021

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (deps) [\#9956](https://github.com/cosmos/cosmos-sdk/pull/9956) Bump Tendermint to [v0.34.12](https://github.com/tendermint/tendermint/releases/tag/v0.34.12).
* (cli) [\#9856](https://github.com/cosmos/cosmos-sdk/pull/9856) Overwrite `--sequence` and `--account-number` flags with default flag values when used with `offline=false` in `sign-batch` command.
* (types) [\#10021](https://github.com/cosmos/cosmos-sdk/pull/10021) Speedup coins.AmountOf(), by removing many intermittent regex calls.

### Bug Fixes

Expand Down
10 changes: 8 additions & 2 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,12 @@ func (coins Coins) Empty() bool {
// AmountOf returns the amount of a denom from coins
func (coins Coins) AmountOf(denom string) Int {
mustValidateDenom(denom)
return coins.AmountOfNoDenomValidation(denom)
}

// AmountOfNoDenomValidation returns the amount of a denom from coins
// without validating the denomination.
func (coins Coins) AmountOfNoDenomValidation(denom string) Int {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func (coins Coins) AmountOfNoDenomValidation(denom string) Int {
func (coins Coins) AmountOfWithoutValidation(denom string) Int {

switch len(coins) {
case 0:
return ZeroInt()
Expand All @@ -530,15 +535,16 @@ func (coins Coins) AmountOf(denom string) Int {
return ZeroInt()

default:
// Binary search the amount of coins remaining
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return coins[midIdx+1:].AmountOfNoDenomValidation(denom)
return coins[midIdx+1:].AmountOfWithoutValidation(denom)

}
}
}
Expand Down