Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory and small runtime optimizations #1512

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading