diff --git a/.jules/bolt.md b/.jules/bolt.md index d453db43..280133ae 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -24,3 +24,6 @@ ## 2025-05-21 - Optimized GetRackCount calls (FindObjectsOfType) **Learning:** Using `UnityEngine.Object.FindObjectsOfType` 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. diff --git a/src/GameLayer/Patches/Networking/CablePositionsPatch.cs b/src/GameLayer/Patches/Networking/CablePositionsPatch.cs index 84c3f50c..4b05230c 100644 --- a/src/GameLayer/Patches/Networking/CablePositionsPatch.cs +++ b/src/GameLayer/Patches/Networking/CablePositionsPatch.cs @@ -67,7 +67,13 @@ public static void SetBaseId(int baseId) } while (Interlocked.CompareExchange(ref _nextCableId, baseId + 1, current) != current); - MelonLogger.Msg($"[CablePatch] Cable ID counter set to {baseId + 1}"); + try { LogBaseIdUpdate(baseId + 1); } catch { } + } + + [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; diff --git a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs index 17bd8abb..d85dc24b 100644 --- a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs +++ b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs @@ -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 GetServers() + { + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null) + { + var list = new System.Collections.Generic.List(nm.servers.Count); + foreach (var kvp in nm.servers) + { + list.Add(kvp.Value); + } + return list; + } + + var arr = UnityEngine.Object.FindObjectsOfType(); + var fallback = new System.Collections.Generic.List(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); @@ -21,7 +41,7 @@ public static void Register(Table greg, Script script, string modId) { try { - var servers = UnityEngine.Object.FindObjectsOfType(); + var servers = GetServers(); var result = new Table(script); int i = 1; foreach (var s in servers) @@ -62,7 +82,7 @@ public static void Register(Table greg, Script script, string modId) { return nm.servers.Count; } - var servers = UnityEngine.Object.FindObjectsOfType(); + var servers = GetServers(); return servers?.Count ?? 0; } catch { return 0; } @@ -80,7 +100,7 @@ public static void Register(Table greg, Script script, string modId) { try { - var servers = UnityEngine.Object.FindObjectsOfType(); + var servers = GetServers(); foreach (var s in servers) { try @@ -104,7 +124,7 @@ public static void Register(Table greg, Script script, string modId) try { int repaired = 0; - var servers = UnityEngine.Object.FindObjectsOfType(); + var servers = GetServers(); foreach (var s in servers) { try