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

Trim memory usage of various collections. #21383

Merged
merged 4 commits into from Apr 6, 2024

Conversation

RoosterDragon
Copy link
Member

@RoosterDragon RoosterDragon commented Apr 1, 2024

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.

Copy link
Contributor

@anvilvapre anvilvapre left a 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?

OpenRA.Game/FileFormats/Png.cs Show resolved Hide resolved
OpenRA.Mods.Cnc/FileSystem/BigFile.cs Show resolved Hide resolved
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.
@RoosterDragon
Copy link
Member Author

Any idea what amount of memory it saves?

I took a more precise measurement, 0.3mb for the RA mod, 0.35mb for tdhd.

Copy link
Member

@PunkPun PunkPun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PunkPun PunkPun merged commit 7859b91 into OpenRA:bleed Apr 6, 2024
3 checks passed
@PunkPun
Copy link
Member

PunkPun commented Apr 6, 2024

changelog

@RoosterDragon RoosterDragon deleted the trim-collections branch April 6, 2024 09:10
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.

None yet

3 participants