From e78ca8b6a366aa2f3df8f4f7d724cfc89ffa4aad Mon Sep 17 00:00:00 2001 From: Jacky720 <32578221+Jacky720@users.noreply.github.com> Date: Sat, 5 Aug 2023 16:12:09 -0400 Subject: [PATCH] Add a HashSet to reduce switch memory consumption --- UndertaleModLib/Decompiler/Decompiler.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/UndertaleModLib/Decompiler/Decompiler.cs b/UndertaleModLib/Decompiler/Decompiler.cs index 6453f6bb0..e656adee0 100644 --- a/UndertaleModLib/Decompiler/Decompiler.cs +++ b/UndertaleModLib/Decompiler/Decompiler.cs @@ -4066,8 +4066,13 @@ private static Block DetermineSwitchEnd(Block start, Block end, Block meetPoint) return meetPoint; Queue blocks = new Queue(); + // 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 usedBlocks = new HashSet(); blocks.Enqueue(start); + usedBlocks.Add(start); while (blocks.Count > 0) { Block test = blocks.Dequeue(); @@ -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); + } }