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

Reduce memory usage #2

Merged
merged 5 commits into from
Jun 20, 2024
Merged

Conversation

PotatoesFall
Copy link

@PotatoesFall PotatoesFall commented Jun 4, 2024

Hi!

Hope it's cool if I made a PR, I really liked this tool but I noticed it keeps entropy information about all lines for a given file in memory and does a huge sort at the end.

Proposed change

I propose this change, which means we never keep more lines in memory than necessary.

The Entropies struct keeps the top n lines sorted in a slice.

Testing with a medium sized repository, I noticed the old version got all the way up to 1G memory consumption, after the change it doesn't even show up in my top 100 memory consumption programs.

Execution time

While I didn't see an improvement in execution time for my testing, this version does get rid of the large sort towards the end.

If we want to cut down execution time in the future, it may be wise to make an individual list per file and/or per directory (as before), and merge these together when done with each file/directory, to reduce locking on the main Entropies struct.

For very large values of -top, we could possibly also get very small performance gains using a max heap vs a slice, but that's probably premature optimization at this point.

@EwenQuim
Copy link
Owner

EwenQuim commented Jun 4, 2024

Thanks for your contribution!

I'll look at it tomorrow ;)

main.go Outdated Show resolved Hide resolved
@EwenQuim EwenQuim force-pushed the master branch 3 times, most recently from 5272ed5 to 975e612 Compare June 5, 2024 12:05
Currently, any change in the source code requires a rerun of `go mod download`. This commit moves that step earlier so that only changes in the dependencies require a rebuild of that layer.
@EwenQuim
Copy link
Owner

Can you please resolve conflicts? Thanks!

@EwenQuim EwenQuim changed the base branch from master to reduce-memory-usage June 20, 2024 18:15
@EwenQuim EwenQuim merged commit b885946 into EwenQuim:reduce-memory-usage Jun 20, 2024
2 checks passed
@EwenQuim
Copy link
Owner

That is some very high quality PR @PotatoesFall, thank you!

Some tests needed to be updated though, I did it and merged it.

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.

4 participants