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
Trim memory usage of various collections. #21383
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general. Input validation missing while reading external files.
Any idea what amount of memory it saves?
These implementations are often backed by a Dictionary, and tend to live a long time after being loaded. Ensure TrimExcess is called on the backing dictionaries to reduce the long term memory usage. In some cases, we can also preallocate the dictionary size for efficiency.
As the SpriteCache is used as a one-shot operation in practise, holding on to the capacity of backing collections is not required. Memory usage can be reduced by allowing the capacity to be reset after the SpriteCache has resolved items. - Once LoadReservations is called, reset the reservation dictionaries so their backing collections can be reclaimed. - When ResolveSprites is called, shrink the resolved dictionary as resolutions take place.
When LoadFromManifest is called, trim the various backing collections. These backing collections tend to live a long time without further modifications.
For indexed PNGs, we only need to allocate a palette large enough to accommodate the number of indexed colours in the image. For images that don't use all 256 colours, this slightly reduces the memory usage for these images.
1c6bb4a
to
abe5048
Compare
I took a more precise measurement, 0.3mb for the RA mod, 0.35mb for tdhd. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
These changes target a series of classes that load data into a Dictionary/List/Array and then live a long time without modifying the backing collection after that. We can realize some memory savings by shrinking the capacity of the backing collections so that they are not holding onto unused capacity longer than required.
Collectively this saves about 0.3 megabyte of memory, which isn't a lot, but the changes are quite simple.