From 504690d4737995db7b28a7c81a49759323c0e083 Mon Sep 17 00:00:00 2001 From: mleem97 <52848568+mleem97@users.noreply.github.com> Date: Sun, 12 Jul 2026 23:52:05 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20GetRackCount=20w?= =?UTF-8?q?ith=20O(1)=20lookup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/PublicApi/Modules/GregFacilityModule.cs | 40 +++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/PublicApi/Modules/GregFacilityModule.cs b/src/PublicApi/Modules/GregFacilityModule.cs index 3d2e40b6..1130059a 100644 --- a/src/PublicApi/Modules/GregFacilityModule.cs +++ b/src/PublicApi/Modules/GregFacilityModule.cs @@ -1,11 +1,29 @@ -using UnityEngine; - -namespace gregCore.PublicApi.Modules; - -public sealed class GregFacilityModule -{ - private readonly GregApiContext _ctx; - internal GregFacilityModule(GregApiContext ctx) => _ctx = ctx; - - public int GetRackCount() => UnityEngine.Object.FindObjectsOfType().Length; -} +using UnityEngine; + +namespace gregCore.PublicApi.Modules; + +public sealed class GregFacilityModule +{ + private readonly GregApiContext _ctx; + internal GregFacilityModule(GregApiContext ctx) => _ctx = ctx; + + // Index 2 represents racks in GetNumberOfDevices array (0: servers, 1: switches) + private const int DEVICE_INDEX_RACKS = 2; + + public int GetRackCount() + { + // Optimization: Use O(1) device counts from NetworkMap instead of O(N) FindObjectsOfType + var networkMap = global::Il2Cpp.NetworkMap.instance; + if (networkMap != null) + { + var counts = networkMap.GetNumberOfDevices(); + if (counts != null && counts.Length > DEVICE_INDEX_RACKS) + { + return counts[DEVICE_INDEX_RACKS]; + } + } + + // Fallback during uninitialized game states + return UnityEngine.Object.FindObjectsOfType().Length; + } +}