Skip to content

Commit

Permalink
perf: Improve the speed of coins.String() (backport cosmos#10076) (co…
Browse files Browse the repository at this point in the history
…smos#10850)

* perf: Improve the speed of coins.String() (cosmos#10076)

<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Speedup coins.String() and coin.String()

In my local benchmarks, this >2x improves the time for balances with 1 coin, and further improves speed for strings with many balances. I did not benchmark on the two coin usecase

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification - n/a
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - covered by existing
- [ ] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - seems sufficiently clear
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### 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...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 744c85b)

# Conflicts:
#	CHANGELOG.md

* changelog

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Co-authored-by: Federico Kunze Küllmer <federico.kunze94@gmail.com>
  • Loading branch information
3 people authored and JimLarson committed Jul 7, 2022
1 parent 09174ce commit efcf4b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit efcf4b8

Please sign in to comment.