Trim memory usage of IReadOnlyPackage implementations.

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.
This commit is contained in:
RoosterDragon
2024-04-01 14:29:37 +01:00
committed by Gustas
parent ed5c7bb836
commit a4bb58007f
6 changed files with 23 additions and 11 deletions

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Cnc.FileSystem
{
readonly Stream s;
readonly Dictionary<string, (uint Offset, int Length)> contents = new();
readonly Dictionary<string, (uint Offset, int Length)> contents;
public MegFile(Stream s, string filename)
{
@@ -84,6 +84,7 @@ namespace OpenRA.Mods.Cnc.FileSystem
throw new Exception("File name table in .meg file inconsistent");
// Now we load each file entry and associated info
contents = new Dictionary<string, (uint Offset, int Length)>((int)numFiles);
for (var i = 0; i < numFiles; i++)
{
// Ignore flags, crc, index
@@ -94,6 +95,8 @@ namespace OpenRA.Mods.Cnc.FileSystem
contents[filenames[nameIndex]] = (offset, (int)size);
}
contents.TrimExcess();
if (s.Position != headerSize)
throw new Exception("Expected to be at data start offset");
}