diff --git a/.jules/bolt.md b/.jules/bolt.md index d453db43..d6f4ab2e 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-21 - Optimized FindObjectsOfTypeAll in game lookup APIs +**Learning:** `UnityEngine.Resources.FindObjectsOfTypeAll` is heavily used in `FindHandleByStableId` for lookup operations, doing an O(N) lookup. This affects performance when there is a significant amount of devices loaded. +**Action:** Always prefer using global singleton collections managed by the game over calling `FindObjectsOfTypeAll`. Before calling it, perform a fast O(1) dictionary lookup using `Il2Cpp.NetworkMap.instance` collections such as `servers`, `switches`, and `patchPanels` when searching devices by their ID. diff --git a/src/Compatibility/DataCenterModLoader/GameAPI.cs b/src/Compatibility/DataCenterModLoader/GameAPI.cs index 42c6a5cc..a21f23ae 100644 --- a/src/Compatibility/DataCenterModLoader/GameAPI.cs +++ b/src/Compatibility/DataCenterModLoader/GameAPI.cs @@ -1390,6 +1390,14 @@ private ulong FindHandleByStableId(string targetId) { try { + var nm = global::Il2Cpp.NetworkMap.instance; + if (nm != null) + { + try { if (nm.servers != null) { foreach (var kvp in nm.servers) { var srv = kvp.Value; if (srv != null && (srv.ServerID ?? "") == targetId) return (ulong)srv.Pointer.ToInt64(); } } } catch { } + try { if (nm.switches != null) { foreach (var kvp in nm.switches) { var sw = kvp.Value; if (sw != null && (sw.switchId ?? "") == targetId) return (ulong)sw.Pointer.ToInt64(); } } } catch { } + try { if (nm.patchPanels != null) { foreach (var kvp in nm.patchPanels) { var pp = kvp.Value; if (pp != null && (pp.patchPanelId ?? "") == targetId) return (ulong)pp.Pointer.ToInt64(); } } } catch { } + } + foreach (var srv in UnityEngine.Resources.FindObjectsOfTypeAll()) { try