Skip to content

⚡ Bolt: Optimize GregFacilityModule.GetRackCount using NetworkMap#177

Open
mleem97 wants to merge 1 commit into
mainfrom
perf/optimize-rack-count-17838429299308485425
Open

⚡ Bolt: Optimize GregFacilityModule.GetRackCount using NetworkMap#177
mleem97 wants to merge 1 commit into
mainfrom
perf/optimize-rack-count-17838429299308485425

Conversation

@mleem97

@mleem97 mleem97 commented Jul 13, 2026

Copy link
Copy Markdown
Owner

💡 What: Optimize GregFacilityModule.GetRackCount() to use NetworkMap.instance.GetNumberOfDevices() instead of FindObjectsOfType<Rack>().

🎯 Why: UnityEngine.Object.FindObjectsOfType is O(N) over all active loaded objects, which can cause frame drops and GC spikes when called frequently. The network map maintains this count internally and allows O(1) lookups.

📊 Impact: Turns an O(N) allocation-heavy lookup into an O(1) array access. Massively reduces overhead when called on frame updates or frequently during gameplay.

🔬 Measurement: Check the CPU usage using a profiler during calls to GetRackCount(). It should now take practically no time and cause 0 byte allocations.


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

This PR optimizes the GetRackCount method by replacing an $O(N)$ FindObjectsOfType search with a direct lookup in the NetworkMap device count cache. This is a significant performance improvement for large facility maps.

While the implementation includes necessary defensive checks for the NetworkMap instance and its data, the reliance on a hardcoded index (index 2) creates a brittle dependency on the internal data structure of the NetworkMap. Additionally, although the optimization is logically sound, the PR lacks unit tests to verify that the fallback logic correctly handles null instances or undersized arrays. Codacy reports that the PR is up to standards, though cyclomatic complexity has increased slightly (+3) due to the new defensive branching.

About this PR

  • The PR lacks unit or integration tests to verify the new optimization and its fallback mechanisms. Given the importance of facility metrics, we should ensure the fallback to FindObjectsOfType works as intended when NetworkMap is unavailable or returns unexpected data.
  • The optimization relies on a hardcoded index (2) for retrieving rack counts. This creates a brittle dependency on the internal implementation of Il2Cpp.NetworkMap. If the order of device types in the network map changes, this logic will break silently or return incorrect data.

Test suggestions

  • Verify GetRackCount returns index 2 from NetworkMap when the instance and data are valid.
  • Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
  • Verify GetRackCount falls back to FindObjectsOfType when GetNumberOfDevices returns null.
  • Verify GetRackCount falls back to FindObjectsOfType when the returned array length is less than 3.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify GetRackCount returns index 2 from NetworkMap when the instance and data are valid.
2. Verify GetRackCount falls back to FindObjectsOfType when NetworkMap.instance is null.
3. Verify GetRackCount falls back to FindObjectsOfType when GetNumberOfDevices returns null.
4. Verify GetRackCount falls back to FindObjectsOfType when the returned array length is less than 3.

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

Comment on lines +15 to +22
if (global::Il2Cpp.NetworkMap.instance != null)
{
var counts = global::Il2Cpp.NetworkMap.instance.GetNumberOfDevices();
if (counts != null && counts.Length > DEVICE_INDEX_RACKS)
{
return counts[DEVICE_INDEX_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.

⚪ LOW RISK

Suggestion: The instance property is accessed multiple times, which can be inefficient in IL2CPP/Unity environments where singleton accessors often involve native lookups. Localizing the reference clarifies the logic and reduces overhead.

Suggested change
if (global::Il2Cpp.NetworkMap.instance != null)
{
var counts = global::Il2Cpp.NetworkMap.instance.GetNumberOfDevices();
if (counts != null && counts.Length > DEVICE_INDEX_RACKS)
{
return counts[DEVICE_INDEX_RACKS];
}
}
var map = global::Il2Cpp.NetworkMap.instance;
if (map != null)
{
var counts = map.GetNumberOfDevices();
if (counts != null && counts.Length > DEVICE_INDEX_RACKS)
{
return counts[DEVICE_INDEX_RACKS];
}
}

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