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

Conversation

ValarDragon
Copy link
Contributor

@ValarDragon ValarDragon commented Aug 28, 2021

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

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification - inline
  • included the necessary unit and integration tests - Already covered
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code - docs are improved overall imo
  • updated the relevant documentation or specification
  • 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 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)

@codecov
Copy link

codecov bot commented Aug 28, 2021

Codecov Report

Merging #10021 (17f65a8) into master (994f2d6) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           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           
Impacted Files Coverage Δ
types/coin.go 93.57% <100.00%> (+0.03%) ⬆️
crypto/keys/internal/ecdsa/privkey.go 84.21% <0.00%> (+1.75%) ⬆️

Copy link
Member

@tac0turtle tac0turtle left a 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 {
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 {

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)

@ValarDragon
Copy link
Contributor Author

I personally like the name AmountOfNoDenomValidation more since its in the title of what validation its skipping. (So its easier to know whether or not you can use it)

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.

@tac0turtle tac0turtle requested a review from fedekunze August 31, 2021 13:00
@tac0turtle tac0turtle added T: Performance Performance improvements C:Types labels Aug 31, 2021
@alexanderbez
Copy link
Contributor

Do we need to backport this to v0.44 or 0.42?

@tac0turtle
Copy link
Member

Do we need to backport this to v0.44 or 0.42?

might as well

@tac0turtle tac0turtle added A:automerge Automatically merge PR once all prerequisites pass. backport/0.44.x labels Sep 2, 2021
@tac0turtle
Copy link
Member

@ValarDragon can you update the pr, after which the bot will merge it

@alexanderbez alexanderbez merged commit ed35bfd into cosmos:master Sep 9, 2021
@orijbot
Copy link

orijbot commented Sep 9, 2021

@robert-zaremba
Copy link
Collaborator

@Mergifyio backport release/v0.44.x

mergify bot pushed a commit that referenced this pull request Sep 16, 2021
(cherry picked from commit ed35bfd)

# Conflicts:
#	CHANGELOG.md
@mergify
Copy link
Contributor

mergify bot commented Sep 16, 2021

Command backport release/v0.44.x: success

Backports have been created

robert-zaremba added a commit that referenced this pull request Sep 16, 2021
…10021) (#10166)

* perf: Speedup coins.AmountOf() by removing many regex calls (#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>
evan-forbes pushed a commit to evan-forbes/cosmos-sdk that referenced this pull request Oct 12, 2021
…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>
evan-forbes pushed a commit to evan-forbes/cosmos-sdk that referenced this pull request Nov 1, 2021
…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>
JeancarloBarrios pushed a commit to agoric-labs/cosmos-sdk that referenced this pull request Sep 28, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass. C:Types T: Performance Performance improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants