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: Refactor Coins/Validate to avoid unnecessary map #14163

Merged
merged 6 commits into from
Dec 6, 2022

Conversation

webmaster128
Copy link
Member

and add a few tests.

I came across this code when cheking some stuff for CosmWasm. While reading the implementation I though the map there is unnecessary. The moment you know a list is sorted, you only need to check that element n and n-1 are not equal for every n > 0 to find duplicates.

I did not do any performance tests here but being able to avoid a hashmap entirely should be helpful if this ends up in some hot path of some codebase.

I would not ship this in a patch release since it most likely creates some unsorted errors in places where you had duplicate errors before. But the docs and tests do not seem to guarantee one error over the other.

Hope this diff does not feel spammy or over optimizing. @ValarDragon said this change matters so I hope it has some value.

Description

Closes: #XXXX


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
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • 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)

@webmaster128 webmaster128 requested a review from a team as a code owner December 5, 2022 19:45
Copy link
Contributor

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

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

TY for adding this!

I think its helpful, NewCoins is called a lot in testing within Osmosis (and at epoch). Lowering the heap pressure here is v valuable!

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.

thank you for the submission. code cleanup is welcome.

@tac0turtle tac0turtle enabled auto-merge (squash) December 6, 2022 11:11
@alexanderbez alexanderbez added the backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release label Dec 6, 2022
@webmaster128
Copy link
Member Author

I tried fixing gofumpt formatting but when I run gofumpt -w ./types I only get changed in files unrelated to this PR:

        geändert:       types/mempool/mempool_test.go
        geändert:       types/mempool/sender_nonce_property_test.go
        geändert:       types/mempool/sender_nonce_test.go

@tac0turtle tac0turtle added the A:automerge Automatically merge PR once all prerequisites pass. label Dec 6, 2022
@amaury1093
Copy link
Contributor

Can we get a quick changelog entry before the merge?

@webmaster128
Copy link
Member Author

I can write one if you want. But you have to stop the merge machinery. Will have to check how this is working here.

@amaury1093 amaury1093 disabled auto-merge December 6, 2022 17:57
@amaury1093 amaury1093 removed the A:automerge Automatically merge PR once all prerequisites pass. label Dec 6, 2022
@webmaster128
Copy link
Member Author

Alright, hope this works. Let me know if you need anything else.

@amaury1093 amaury1093 merged commit 9fd1825 into cosmos:main Dec 6, 2022
mergify bot pushed a commit that referenced this pull request Dec 6, 2022
* Refactor (coins Coins) Validate() to avoid unnecessary map

and add a few tests

* Add CHANGELOG entry

Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
(cherry picked from commit 9fd1825)

# Conflicts:
#	CHANGELOG.md
tac0turtle pushed a commit that referenced this pull request Dec 8, 2022
…) (#14182)

Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.47.x PR scheduled for inclusion in the v0.47's next stable release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants