Skip to content

perf: Refactor Coins/Validate to avoid unnecessary map#14163

Merged
amaury1093 merged 6 commits into
cosmos:mainfrom
webmaster128:remove-seen-map
Dec 6, 2022
Merged

perf: Refactor Coins/Validate to avoid unnecessary map#14163
amaury1093 merged 6 commits into
cosmos:mainfrom
webmaster128:remove-seen-map

Conversation

@webmaster128
Copy link
Copy Markdown
Contributor

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 December 5, 2022 19:45
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@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
@webmaster128
Copy link
Copy Markdown
Contributor 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

@amaury1093
Copy link
Copy Markdown
Contributor

Can we get a quick changelog entry before the merge?

@webmaster128
Copy link
Copy Markdown
Contributor 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.

@webmaster128
Copy link
Copy Markdown
Contributor 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants