Skip to content

Commit

Permalink
Core: Add a "low-memory mode" that attempts to reduce memory usage
Browse files Browse the repository at this point in the history
More work to help on #306
  • Loading branch information
SamboyCoding committed Jun 30, 2024
1 parent 9749347 commit 4943b34
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Cpp2IL.Core/Cpp2IlApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ namespace Cpp2IL.Core
public static class Cpp2IlApi
{
public static ApplicationAnalysisContext? CurrentAppContext;

public static Cpp2IlRuntimeArgs? RuntimeOptions;

internal static bool LowMemoryMode => RuntimeOptions?.LowMemoryMode ?? false;

[RequiresUnreferencedCode("Plugins are loaded dynamically.")]
public static void Init(string pluginsDir = "Plugins")
Expand Down
4 changes: 3 additions & 1 deletion Cpp2IL.Core/Cpp2IlRuntimeArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public class Cpp2IlRuntimeArgs

public Cpp2IlOutputFormat? OutputFormat;
public string OutputRootDirectory = null!;

public bool LowMemoryMode;
}
}
}
6 changes: 6 additions & 0 deletions Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)

m.ReleaseAnalysisData();
}

if(Cpp2IlApi.LowMemoryMode)
GC.Collect();
}

foreach (var assemblyAnalysisContext in appContext.Assemblies)
Expand Down Expand Up @@ -169,6 +172,9 @@ private static void InjectAttribute(ApplicationAnalysisContext appContext)
AttributeInjectionUtils.AddOneParameterAttribute(m, callsUnknownMethodsAttributeInfo, unknownCallCount);
}
}

if(Cpp2IlApi.LowMemoryMode)
GC.Collect();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public override void Process(ApplicationAnalysisContext appContext, Action<int,
{
AnalyzeMethod(appContext, m, nativeMethodInfoStack);
}

if(Cpp2IlApi.LowMemoryMode)
GC.Collect();
}

if(Cpp2IlApi.LowMemoryMode)
GC.Collect();

while (nativeMethodInfoStack.Count > 0)
{
(var address, var isVoid) = nativeMethodInfoStack.Pop();
Expand Down
5 changes: 4 additions & 1 deletion Cpp2IL/CommandLineArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class CommandLineArgs

[Option("verbose", HelpText = "Enable Verbose Logging.")]
public bool Verbose { get; set; }

[Option("low-memory-mode", HelpText = "Enable Low Memory Mode. This will attempt to reduce memory usage at the cost of performance.")]
public bool LowMemoryMode { get; set; }

[Option("wasm-framework-file", HelpText = "Path to the wasm *.framework.js file. Only needed if your binary is a WASM file. If provided, it can be used to remap obfuscated dynCall function names in order to correct method pointers.")]
public string? WasmFrameworkFilePath { get; set; }
Expand All @@ -71,4 +74,4 @@ internal bool AreForceOptionsValid
}
}
}
}
}
21 changes: 19 additions & 2 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ private static Cpp2IlRuntimeArgs GetRuntimeOptionsFromCommandLine(string[] comma

result.OutputRootDirectory = options.OutputRootDir;

result.LowMemoryMode = options.LowMemoryMode;

// if(string.IsNullOrEmpty(options.OutputFormatId))
// throw new SoftException("No output format specified, so nothing to do!");

Expand Down Expand Up @@ -522,7 +524,11 @@ public static int Main(string[] args)
try
{
var runtimeArgs = GetRuntimeOptionsFromCommandLine(args);


if(runtimeArgs.LowMemoryMode)
//Force an early collection for all the zip shenanigans we may have just done
GC.Collect();

return MainWithArgs(runtimeArgs);
}
catch (SoftException e)
Expand Down Expand Up @@ -554,12 +560,14 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)
{
if (!runtimeArgs.Valid)
throw new SoftException("Arguments have Valid = false");

Cpp2IlApi.RuntimeOptions = runtimeArgs;

var executionStart = DateTime.Now;

runtimeArgs.OutputFormat?.OnOutputFormatSelected();

GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
GCSettings.LatencyMode = runtimeArgs.LowMemoryMode ? GCLatencyMode.Interactive : GCLatencyMode.SustainedLowLatency;

if (runtimeArgs.WasmFrameworkJsFile != null)
try
Expand All @@ -578,6 +586,9 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)
WasmFile.RemappedDynCallFunctions = null;

Cpp2IlApi.InitializeLibCpp2Il(runtimeArgs.PathToAssembly, runtimeArgs.PathToMetadata, runtimeArgs.UnityVersion);

if(runtimeArgs.LowMemoryMode)
GC.Collect();

foreach (var (key, value) in runtimeArgs.ProcessingLayerConfigurationOptions)
Cpp2IlApi.CurrentAppContext.PutExtraData(key, value);
Expand All @@ -596,6 +607,9 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)

if (runtimeArgs.OutputFormat != null)
{
if (runtimeArgs.LowMemoryMode)
GC.Collect();

Logger.InfoNewline($"Outputting as {runtimeArgs.OutputFormat.OutputFormatName} to {runtimeArgs.OutputRootDirectory}...");
runtimeArgs.OutputFormat.DoOutput(Cpp2IlApi.CurrentAppContext, runtimeArgs.OutputRootDirectory);
Logger.InfoNewline($"Finished outputting in {(DateTime.Now - outputStart).TotalMilliseconds}ms");
Expand Down Expand Up @@ -641,6 +655,9 @@ private static void RunProcessingLayers(Cpp2IlRuntimeArgs runtimeArgs, Action<Cp
}
#endif

if (runtimeArgs.LowMemoryMode)
GC.Collect();

Logger.InfoNewline($" {processingLayer.Name} finished in {(DateTime.Now - processorStart).TotalMilliseconds}ms");
}
}
Expand Down

0 comments on commit 4943b34

Please sign in to comment.