Skip to content

Commit

Permalink
Memory Optimization to not resize a dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Miepee committed Jun 10, 2024
1 parent 4e832cd commit 68111e3
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions UndertaleModLib/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,25 @@ public void Setup(bool redoAssets = false)

private void MakeAssetDictionary()
{
// Clear the dictionary first and set the worst case max size so that we don't resize it over and over
assetIds.Clear();
int maxSize = 0;
maxSize += Data?.GameObjects?.Count ?? 0;
maxSize += Data?.Sprites?.Count ?? 0;
maxSize += Data?.Sounds?.Count ?? 0;
maxSize += Data?.Backgrounds?.Count ?? 0;
maxSize += Data?.Paths?.Count ?? 0;
maxSize += Data?.Fonts?.Count ?? 0;
maxSize += Data?.Timelines?.Count ?? 0;
maxSize += Data?.Scripts?.Count ?? 0;
maxSize += Data?.Shaders?.Count ?? 0;
maxSize += Data?.Rooms?.Count ?? 0;
maxSize += Data?.AudioGroups?.Count ?? 0;
maxSize += Data?.AnimationCurves?.Count ?? 0;
maxSize += Data?.Sequences?.Count ?? 0;
maxSize += Data?.ParticleSystems?.Count ?? 0;
assetIds.EnsureCapacity(maxSize);

AddAssetsFromList(Data?.GameObjects, AssetRefType.Object);
AddAssetsFromList(Data?.Sprites, AssetRefType.Sprite);
AddAssetsFromList(Data?.Sounds, AssetRefType.Sound);
Expand Down

0 comments on commit 68111e3

Please sign in to comment.