Skip to content

refactor: simplify large functions#2130

Closed
faddat wants to merge 265 commits intocometbft:mainfrom
faddat:faddat/performance-linters
Closed

refactor: simplify large functions#2130
faddat wants to merge 265 commits intocometbft:mainfrom
faddat:faddat/performance-linters

Conversation

@faddat
Copy link
Contributor

@faddat faddat commented Jan 25, 2024

This PR will enable some new linters, and be based off of:

main + #1907

... so #1907 should be merged before this one, because that will greatly reduce the number of changed files from 203 to somewhere in the teens. If at any time it's needed to break any of this into its own PR's, just let me know.

New Linters

  • nestif set to 15
  • cyclomatic complexity via revive set to 71
  • cognitive complexity via revive set to 71

I got (in my opinion) amazing results from this. After the configuration, I was guided to what are more or less known trouble areas in comet, nearly every time.

Ongoing plan

Over time, we can lower the complexity rules and nestif to their defaults, and I think that this would likely have a great effect on the software overall.

Notes

I really liked some of the changes I started to get as I worked with these linters, which are all related to each other to some degree.

This PR takes some of the largest functions in cometbft, and breaks them into smaller pieces.

When I started it, I wasn't too sure, but by using this approach I was definitely able to make changes that seem to:

  • make things more reliable
  • make things easier to understand

Changes unique to this PR

This PR takes anything that the nestif/cyclomaticcomplexity/cognitivecomplexity linters finds to be too complex, and refactors it into individual functions. I wasn't too sure about this course of action when I started, but in a number of places, the results I started to get are (I think) quite convincing.

In places where the actual code has been changed, I'm finding that the reworked style is actually easier to read and think about.

It's likely that I will write out the whole PR, kind of like

And then submit the whole thing as a series of smaller patches.

For example, this technique has led me to poolRoutine, which is indeed a large and complex function. It runs consistently, and it is probably better for many reasons to break it into separate functions.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

TestGenPrivKeySecp256k1
TestClientReplacesPrimaryWithWitnessIfPrimaryIsUnavailable
This reverts commit ba108c6.
rpc/jsonrpc/client/args_test.go,
mempool/clist_mempool_test.go,
crypto/merkle/proof_test.go, and
internal/store/store_test.go
cache_test.go, clist_mempool_test.go,
helpers_test.go, and status_test.go
@faddat faddat changed the title refactor: reduce complexity by hunting complex functions refactor: simplify large functions Jan 31, 2024
@melekes
Copy link
Collaborator

melekes commented Feb 6, 2024

I can see that you've already started breaking this PR into smaller PRs. I think this is the right direction 👍

@melekes melekes closed this Feb 6, 2024
@faddat
Copy link
Contributor Author

faddat commented Feb 10, 2024

If it's no different to you, I would like to reopen this as a draft pull request for tracking purposes.

This PR enables several of the linters that you mentioned in #2274 .

Would you like to go linter by linter or how do you want to do it?

@faddat faddat mentioned this pull request Feb 10, 2024
35 tasks
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.

2 participants