feat: improve memory usage of FFZ badges#7126
Open
Nerixyz wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FFZ badges consume a decent amount of memory in Chatterino. Before this PR, they consumed 18.4 MiB.
We can do two cheap optimizations that cut the use in half:
QVarLengthArrayto store the badges for a user instead ofstd::set.std::setalways allocates, even if there's only one badge. Since most users only have one, we create many small allocations. WithQVarLengthArray<T, N>,Nelements are stored inline. This reduced the memory usage to 11.9 MiB.boost::unordered_flat_mapinstead ofstd::unordered_map. Because of relaxed requirements for the Boost map, it doesn't need to store as much auxiliary data. This reduced the memory usage to 9.3 MiB.A further optimization could be the use of a
boost::container::flat_multimap<std::string, int>. This gets us to 4.6 MiB. I haven't measured the performance of this. We'd have to assume that FFZ returns us unique users per badge. The comparison withQStringfor the lookup can be done by comparingQString < QLatin1StringView(as we know badge IDs are latin1).