Skip to content

Commit

Permalink
Merge pull request #1512 from Miepee/optimize-debug-out
Browse files Browse the repository at this point in the history
Memory and small runtime optimizations
  • Loading branch information
colinator27 authored Nov 11, 2023
2 parents 9922aea + 42cdb90 commit 1beac06
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
37 changes: 24 additions & 13 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,32 @@ public DecompileContext(GlobalDecompileContext globalContext, UndertaleCode code
if (code.ParentEntry != null)
throw new InvalidOperationException("This code block represents a function nested inside " + code.ParentEntry.Name + " - decompile that instead");

if (computeObject && globalContext.Data != null)
if (computeObject && globalContext.Data is not null)
{
// TODO: This is expensive, move it somewhere else as a dictionary
// and have it update when events/objects are modified.
foreach (var obj in globalContext.Data.GameObjects)
foreach (var event_list in obj.Events)
foreach (var subevent in event_list)
foreach (var ev in subevent.Actions)
if (ev.CodeId == code)
{
Object = obj;
goto LoopEnd;
}

// Currently using for loops on purpose, as foreach has memory issues due to IEnumerable
for (int i = 0; i < globalContext.Data.GameObjects.Count; i++)
{
UndertaleGameObject obj = globalContext.Data.GameObjects[i];
for (int j = 0; j < obj.Events.Count; j++)
{
var eventList = obj.Events[j];
for (int k = 0; k < eventList.Count; k++)
{
UndertaleGameObject.Event subEvent = eventList[k];
for (int l = 0; l < subEvent.Actions.Count; l++)
{
UndertaleGameObject.EventAction ev = subEvent.Actions[l];
if (ev.CodeId != code) continue;
Object = obj;
return;
}
}
}
}
}
LoopEnd: return;
}

#region Struct management
Expand Down Expand Up @@ -3464,7 +3475,7 @@ public static Dictionary<Block, List<Block>> ComputeReverseDominators(Dictionary

Block b = blockList[i];

IEnumerable<Block> e;
Block[] e;
if (b.conditionalExit)
{
reverseUse2[0] = b.nextBlockTrue;
Expand Down Expand Up @@ -4043,7 +4054,7 @@ public static void BuildSubFunctionCache(UndertaleData data)
{
throw new TimeoutException("The building cache process hung.\n" +
"The function code entries that didn't manage to decompile:\n" +
String.Join('\n', processingCodeList.Keys) + "\n\n" +
String.Join('\n', processingCodeList.Keys) + "\n\n" +
"You should save the game data (if it's necessary) and re-open the app.\n");
}
}
Expand Down
8 changes: 8 additions & 0 deletions UndertaleModLib/Util/DebugUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -8,6 +9,13 @@ namespace UndertaleModLib.Util
{
public class DebugUtil
{

/// <summary>
/// Asserts that a specified condition is true.
/// </summary>
/// <param name="expr">The condition to assert for.</param>
/// <param name="msg">The message to use if the assertion fails.</param>
/// <exception cref="Exception">Gets thrown if the assertion fails.</exception>
public static void Assert(bool expr, string msg = "Unknown error.")
{
if (expr)
Expand Down
20 changes: 0 additions & 20 deletions UndertaleModLib/Util/DictionaryExtensions.cs

This file was deleted.

0 comments on commit 1beac06

Please sign in to comment.