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

This commit is contained in:
Dev Ojha
2021-09-09 08:58:24 -04:00
committed by GitHub
parent eb0113e4bc
commit ed35bfdf52
2 changed files with 9 additions and 2 deletions
+8 -2
View File
@@ -522,7 +522,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 {
switch len(coins) {
case 0:
return ZeroInt()
@@ -535,15 +540,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)
case denom == coin.Denom:
return coin.Amount
default:
return coins[midIdx+1:].AmountOf(denom)
return coins[midIdx+1:].AmountOfNoDenomValidation(denom)
}
}
}