Skip to content

Commit

Permalink
Add a HashSet to reduce switch memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacky720 committed Aug 5, 2023
1 parent 3e9675d commit e78ca8b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4066,8 +4066,13 @@ private static Block DetermineSwitchEnd(Block start, Block end, Block meetPoint)
return meetPoint;

Queue<Block> blocks = new Queue<Block>();
// Preventing the same block and its children from being queued repeatedly
// becomes increasingly important on large switches. The HashSet should give
// good performance while preventing this type of duplication.
HashSet<Block> usedBlocks = new HashSet<Block>();

blocks.Enqueue(start);
usedBlocks.Add(start);
while (blocks.Count > 0)
{
Block test = blocks.Dequeue();
Expand All @@ -4076,10 +4081,16 @@ private static Block DetermineSwitchEnd(Block start, Block end, Block meetPoint)
return end;
if (test == meetPoint)
return meetPoint;

blocks.Enqueue(test.nextBlockTrue);
if (test.nextBlockTrue != test.nextBlockFalse)
if (!usedBlocks.Contains(test.nextBlockTrue))
{
blocks.Enqueue(test.nextBlockTrue);
usedBlocks.Add(test.nextBlockTrue);
}
if (!usedBlocks.Contains(test.nextBlockFalse))
{
blocks.Enqueue(test.nextBlockFalse);
usedBlocks.Add(test.nextBlockFalse);
}

}

Expand Down

0 comments on commit e78ca8b

Please sign in to comment.