Skip to content
Open
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
## 2025-05-21 - Optimized GetRackCount calls (FindObjectsOfType)
**Learning:** Using `UnityEngine.Object.FindObjectsOfType<Rack>` to simply get the rack count is an O(N) operation over all objects, creating unnecessary GC pressure and CPU overhead, especially as the data center grows.
**Action:** Optimized `GetRackCount` implementation in `GameHooks.cs` by using the game-managed O(1) singleton `Il2Cpp.NetworkMap.instance.GetNumberOfDevices()` (index 2 for racks), providing a fallback to `FindObjectsOfType` only during uninitialized states.
## 2025-05-23 - Optimizing Harmony Patch Method Sizes
**Learning:** Bulky Harmony patch methods (especially Prefixes) cause poor performance on heavily hit code paths because the JIT compiler declines to inline them due to their large IL size (e.g. from complex event emission and string formatting).
**Action:** Move bulky logic (logging, event emission, etc.) out of the hot path into separate helper methods decorated with `[MethodImpl(MethodImplOptions.NoInlining)]`. This drastically reduces the IL size of the fast path, encouraging the JIT compiler to inline the patch method and improving overall execution speed.
8 changes: 7 additions & 1 deletion src/GameLayer/Patches/Networking/CablePositionsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@
}
while (Interlocked.CompareExchange(ref _nextCableId, baseId + 1, current) != current);

MelonLogger.Msg($"[CablePatch] Cable ID counter set to {baseId + 1}");
try { LogBaseIdUpdate(baseId + 1); } catch { }

Check warning on line 70 in src/GameLayer/Patches/Networking/CablePositionsPatch.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/GameLayer/Patches/Networking/CablePositionsPatch.cs#L70

Handle the exception or explain in a comment why it can be ignored.
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void LogBaseIdUpdate(int newId)
{
MelonLogger.Msg($"[CablePatch] Cable ID counter set to {newId}");
}

public static int PeekNextId() => _nextCableId;
Expand Down
28 changes: 24 additions & 4 deletions src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ namespace gregCore.Infrastructure.Scripting.Lua.Modules;

public static class LuaServerModule
{
// ⚡ Bolt: Optimize hardware entity lookups by returning a list from O(1) NetworkMap instead of O(N) Unity scene query
private static System.Collections.Generic.List<Il2Cpp.Server> GetServers()
{
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.servers != null)
{
var list = new System.Collections.Generic.List<Il2Cpp.Server>(nm.servers.Count);
foreach (var kvp in nm.servers)
{
list.Add(kvp.Value);
}
return list;
}

var arr = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var fallback = new System.Collections.Generic.List<Il2Cpp.Server>(arr.Length);
foreach (var s in arr) fallback.Add(s);
return fallback;
}

public static void Register(Table greg, Script script, string modId)
{
var serverTable = new Table(script);
Expand All @@ -21,7 +41,7 @@ public static void Register(Table greg, Script script, string modId)
{
try
{
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var servers = GetServers();
var result = new Table(script);
int i = 1;
foreach (var s in servers)
Expand Down Expand Up @@ -62,7 +82,7 @@ public static void Register(Table greg, Script script, string modId)
{
return nm.servers.Count;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var servers = GetServers();
return servers?.Count ?? 0;
}
catch { return 0; }
Expand All @@ -80,7 +100,7 @@ public static void Register(Table greg, Script script, string modId)
{
try
{
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var servers = GetServers();
foreach (var s in servers)
{
try
Expand All @@ -104,7 +124,7 @@ public static void Register(Table greg, Script script, string modId)
try
{
int repaired = 0;
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var servers = GetServers();
foreach (var s in servers)
{
try
Expand Down
Loading