Skip to content
Closed
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-21 - Optimized FindObjectsOfTypeAll in game lookup APIs
**Learning:** `UnityEngine.Resources.FindObjectsOfTypeAll<T>` 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<T>`. 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.
8 changes: 8 additions & 0 deletions src/Compatibility/DataCenterModLoader/GameAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,14 @@
{
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 { }

Check warning on line 1396 in src/Compatibility/DataCenterModLoader/GameAPI.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Compatibility/DataCenterModLoader/GameAPI.cs#L1396

Handle the exception or explain in a comment why it can be ignored.
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 { }

Check warning on line 1397 in src/Compatibility/DataCenterModLoader/GameAPI.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Compatibility/DataCenterModLoader/GameAPI.cs#L1397

Handle the exception or explain in a comment why it can be ignored.
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 { }

Check warning on line 1398 in src/Compatibility/DataCenterModLoader/GameAPI.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Compatibility/DataCenterModLoader/GameAPI.cs#L1398

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

foreach (var srv in UnityEngine.Resources.FindObjectsOfTypeAll<Server>())
{
try
Expand Down
Loading