diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aab68a53..4d8d26bfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (store) [\#10741](https://github.com/cosmos/cosmos-sdk/pull/10741) Significantly speedup iterator creation after delete heavy workloads. Significantly improves IBC migration times. +* (types) [\#10076](https://github.com/cosmos/cosmos-sdk/pull/10076) Significantly speedup and lower allocations for `Coins.String()`. ### Bug Fixes diff --git a/types/coin.go b/types/coin.go index 81afb173e..d4d22642d 100644 --- a/types/coin.go +++ b/types/coin.go @@ -34,7 +34,7 @@ func NewInt64Coin(denom string, amount int64) Coin { // String provides a human-readable representation of a coin func (coin Coin) String() string { - return fmt.Sprintf("%v%v", coin.Amount, coin.Denom) + return fmt.Sprintf("%v%s", coin.Amount, coin.Denom) } // Validate returns an error if the Coin has a negative amount or if @@ -190,13 +190,18 @@ func (coins Coins) MarshalJSON() ([]byte, error) { func (coins Coins) String() string { if len(coins) == 0 { return "" + } else if len(coins) == 1 { + return coins[0].String() } - out := "" - for _, coin := range coins { - out += fmt.Sprintf("%v,", coin.String()) + // Build the string with a string builder + var out strings.Builder + for _, coin := range coins[:len(coins)-1] { + out.WriteString(coin.String()) + out.WriteByte(',') } - return out[:len(out)-1] + out.WriteString(coins[len(coins)-1].String()) + return out.String() } // Validate checks that the Coins are sorted, have positive amount, with a valid and unique