Skip to content

⚡ Bolt: Optimize GetRackCount with O(1) lookup#188

Open
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-rack-count-15336014292552156500
Open

⚡ Bolt: Optimize GetRackCount with O(1) lookup#188
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-rack-count-15336014292552156500

Conversation

@mleem97

@mleem97 mleem97 commented Jul 18, 2026

Copy link
Copy Markdown
Owner

💡 What:
Replaced the FindObjectsOfType<Il2Cpp.Rack>() call in GregFacilityModule.GetRackCount() with an O(1) lookup using Il2Cpp.NetworkMap.instance.GetNumberOfDevices()[2], preserving a graceful fallback to FindObjectsOfType when the NetworkMap singleton is null or the array is out of bounds.

🎯 Why:
FindObjectsOfType is an O(N) operation that traverses the entire Unity object hierarchy, which causes significant performance overhead and GC pressure when polled frequently or used in large scenes.

📊 Impact:
Transforms an O(N) heap-allocating hierarchy traversal into an O(1) array access for initialized game states, drastically reducing CPU time and memory allocation when the method is invoked.

🔬 Measurement:
Profile the CPU and memory allocations during gameplay while repeatedly polling GetRackCount(). The FindObjectsOfType overhead should be eliminated, with near-zero allocation and execution time.


PR created automatically by Jules for task 15336014292552156500 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

Not up to standards ⛔

🔴 Issues 1 high

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
Security 1 high

View in Codacy

🟢 Metrics 5 complexity · 0 duplication

Metric Results
Complexity 5
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 PR optimizes the GetRackCount method by implementing an O(1) lookup via NetworkMap. While this improves performance, the PR is currently not up to standards due to a significant increase in code complexity (+5) and risks associated with error handling.

A major concern is the silent swallowing of all exceptions in the new optimized path. This hides potential integration failures with the underlying game engine and prevents maintainers from knowing if the fallback mechanism is being triggered due to a breaking change in the NetworkMap structure. Furthermore, the implementation relies on a magic number index which introduces fragility. Unit tests for the fallback scenarios and exception handling are currently missing and are required to ensure the robustness of this optimization.

About this PR

  • The changes have increased the complexity of GregFacilityModule.cs by 5 points. Consider if the fallback logic can be simplified or extracted to maintain module readability.

Test suggestions

  • Verify GetRackCount returns the value at index 2 of GetNumberOfDevices when NetworkMap is valid.
  • Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
  • Verify GetRackCount falls back to FindObjectsOfType when GetNumberOfDevices returns an array with length <= 2.
  • Verify the method handles exceptions within the optimized path by triggering the fallback.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify GetRackCount returns the value at index 2 of GetNumberOfDevices when NetworkMap is valid.
2. Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
3. Verify GetRackCount falls back to FindObjectsOfType when GetNumberOfDevices returns an array with length <= 2.
4. Verify the method handles exceptions within the optimized path by triggering the fallback.

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

}
}
}
catch { }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 HIGH RISK

Swallowing all exceptions silently can hide critical interop failures or unexpected nulls. Since this block attempts an O(1) optimization, failing silently prevents identifying why the fallback (O(N)) is being used if the optimization breaks. Consider logging the exception or catching only specific, expected exception types to ensure that unexpected errors are still visible during development.

Refactor the catch block to log the exception or include a comment explaining why it is safe to ignore all exceptions during the NetworkMap lookup.

var arr = nm.GetNumberOfDevices();
if (arr != null && arr.Length > 2)
{
return arr[2]; // index 2 represents racks

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

Suggestion: The magic number 2 should be replaced with a named constant (e.g., RackDeviceIndex) to improve readability and ensure the purpose of this index is clear for future maintainers.

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