Skip to content

⚡ Bolt: Optimize GregFacilityModule.GetRackCount#169

Open
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-getrackcount-7642963042621636757
Open

⚡ Bolt: Optimize GregFacilityModule.GetRackCount#169
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-getrackcount-7642963042621636757

Conversation

@mleem97

@mleem97 mleem97 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced expensive FindObjectsOfType<global::Il2Cpp.Rack>() call with Il2Cpp.NetworkMap.instance.GetNumberOfDevices()[2] with fallback.
🎯 Why: FindObjectsOfType is O(N) over all objects in the scene and expensive to call. Using the O(1) game-managed device count array avoids large CPU usage.
📊 Impact: Significantly improves performance of rack count queries by avoiding full scene traversal.
🔬 Measurement: Verify load and run in-game to see fewer GC spikes and faster response when calling GetRackCount().


PR created automatically by Jules for task 7642963042621636757 started by @mleem97

@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

The optimization of GetRackCount improves performance by moving from a scene-wide search to an O(1) lookup. However, the implementation introduces a potential behavioral discrepancy: NetworkMap.GetNumberOfDevices() may include all registered entities (including inactive or pooled ones), whereas the fallback FindObjectsOfType<T>() strictly returns active objects. This inconsistency could lead to unexpected behavior in consumers of this Public API depending on which logic path is executed.

Furthermore, while the performance gain is noted, the complexity of this method has increased to handle the new safety branches. The file is currently flagged as high-risk due to this increased complexity combined with a total lack of test coverage for the new logic paths. Adding unit tests is essential to ensure the reliability of this Public API component.

Test suggestions

  • Verify GetRackCount returns the correct value from index 2 of the device array when NetworkMap is active.
  • Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
  • Verify GetRackCount falls back to FindObjectsOfType when the device array length is less than or equal to the rack index (2).
  • Unit test the logic branches for null checks and array length validation to ensure zero regression in the Public API.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify GetRackCount returns the correct value from index 2 of the device array when NetworkMap is active.
2. Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
3. Verify GetRackCount falls back to FindObjectsOfType when the device array length is less than or equal to the rack index (2).
4. Unit test the logic branches for null checks and array length validation to ensure zero regression in the Public API.

TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback

Comment on lines +13 to +26
public int GetRackCount()
{
// Fast O(1) path
if (global::Il2Cpp.NetworkMap.instance != null)
{
var arr = global::Il2Cpp.NetworkMap.instance.GetNumberOfDevices();
if (arr != null && arr.Length > DEVICE_INDEX_RACKS)
{
return arr[DEVICE_INDEX_RACKS];
}
}
// Slow fallback
return UnityEngine.Object.FindObjectsOfType<global::Il2Cpp.Rack>().Length;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

This optimization introduces a potential behavioral discrepancy and lacks test coverage. The new fast-path using NetworkMap often includes all registered entities (regardless of active state), whereas the original FindObjectsOfType fallback only counts active objects. This inconsistency can break logic that depends on an accurate count of active-only racks. Additionally, the complexity of this method has increased due to the new safety checks, but this logic is currently uncovered.

Consider verifying if GetNumberOfDevices() matches the active-only semantics of the original implementation. You should also add unit tests to verify: 1) The fast-path return value, 2) The fallback when NetworkMap.instance is null, and 3) The fallback when the device array length is insufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant