Skip to content

Commit

Permalink
Fix generator memory leak (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
slxdy authored Dec 14, 2024
1 parent 9d4599d commit f2b2e69
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
5 changes: 1 addition & 4 deletions Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace Il2CppInterop.Generator.Contexts;
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
public class AssemblyRewriteContext
{
// TODO: Dispose
private static readonly Dictionary<ModuleDefinition, RuntimeAssemblyReferences> ImportsMap = new();

public readonly RewriteGlobalContext GlobalContext;

public readonly RuntimeAssemblyReferences Imports;
Expand All @@ -30,7 +27,7 @@ public AssemblyRewriteContext(RewriteGlobalContext globalContext, AssemblyDefini
NewAssembly = newAssembly;
GlobalContext = globalContext;

Imports = ImportsMap.GetOrCreate(newAssembly.ManifestModule!,
Imports = globalContext.ImportsMap.GetOrCreate(newAssembly.ManifestModule!,
mod => new RuntimeAssemblyReferences(mod, globalContext));
}

Expand Down
2 changes: 2 additions & 0 deletions Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class RewriteGlobalContext : IDisposable

internal readonly Dictionary<(object?, string, int), List<TypeDefinition>> RenameGroups = new();

internal readonly Dictionary<ModuleDefinition, RuntimeAssemblyReferences> ImportsMap = new();

public RewriteGlobalContext(GeneratorOptions options, IIl2CppMetadataAccess gameAssemblies,
IMetadataAccess unityAssemblies)
{
Expand Down
4 changes: 2 additions & 2 deletions Il2CppInterop.Generator/Passes/Pass16ScanMethodRefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Il2CppInterop.Generator.Passes;

public static class Pass16ScanMethodRefs
{
public static readonly HashSet<long> NonDeadMethods = new();
public static IDictionary<long, List<XrefInstance>> MapOfCallers = new Dictionary<long, List<XrefInstance>>();
internal static HashSet<long> NonDeadMethods = new();
internal static IDictionary<long, List<XrefInstance>> MapOfCallers = new Dictionary<long, List<XrefInstance>>();

public static void DoPass(RewriteGlobalContext context, GeneratorOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static void DoPass(RewriteGlobalContext context)
}
}

StuffToProcess.Clear();
StuffToProcess.Capacity = 0;

Logger.Instance.LogInformation("IL unstrip statistics: {MethodsSucceeded} successful, {MethodsFailed} failed", methodsSucceeded,
methodsFailed);
}
Expand Down
7 changes: 7 additions & 0 deletions Il2CppInterop.Generator/Runners/InteropAssemblyGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Il2CppInterop.Common;
using Il2CppInterop.Common.XrefScans;
using Il2CppInterop.Generator.Contexts;
using Il2CppInterop.Generator.MetadataAccess;
using Il2CppInterop.Generator.Passes;
Expand Down Expand Up @@ -201,6 +202,12 @@ public void Run(GeneratorOptions options)
Pass91GenerateMethodPointerMap.DoPass(rewriteContext, options);
}

using (new TimingCookie("Clearing static data"))
{
Pass16ScanMethodRefs.MapOfCallers = new Dictionary<long, List<XrefInstance>>();
Pass16ScanMethodRefs.NonDeadMethods = [];
}

Logger.Instance.LogInformation("Done!");

rewriteContext.Dispose();
Expand Down

0 comments on commit f2b2e69

Please sign in to comment.