-
-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize GetRackCount with O(1) lookup #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<global::Il2Cpp.Rack>().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<global::Il2Cpp.Rack>().Length; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Suggestion: The fallback |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 MEDIUM RISK
Suggestion: The use of a hardcoded index 2 for DEVICE_INDEX_RACKS is brittle. If the game's internal NetworkMap structure changes, this logic will return incorrect device counts without a runtime crash. Search the global::Il2Cpp namespace for an Enum or constants related to NetworkMap.GetNumberOfDevices indices and refactor to use a named constant if available.
See Complexity in Codacy