Skip to content

⚡ Bolt: Optimize GetRackCount to use O(1) array lookup instead of O(N) FindObjectsOfType#183

Open
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-facility-racks-7108022040667288696
Open

⚡ Bolt: Optimize GetRackCount to use O(1) array lookup instead of O(N) FindObjectsOfType#183
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-facility-racks-7108022040667288696

Conversation

@mleem97

@mleem97 mleem97 commented Jul 17, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced FindObjectsOfType<Il2Cpp.Rack>() in GregFacilityModule.GetRackCount() with an O(1) lookup using NetworkMap.instance.GetNumberOfDevices(). Added a fallback in case the singleton is uninitialized.
🎯 Why: FindObjectsOfType is an O(N) operation that iterates over all objects in the scene. As the data center grows, frequent polling of this method creates a massive bottleneck. Fetching the precomputed count from NetworkMap.instance eliminates this overhead.
📊 Impact: Reduces O(N) hierarchy traversal to an O(1) array access, significantly reducing main-thread CPU overhead and IL2CPP GC pressure when querying rack counts.
🔬 Measurement: Can be verified by measuring CPU frame time during dense factory scenes when API methods polling rack counts are executed.


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

- Use `NetworkMap.instance.GetNumberOfDevices()` instead of `FindObjectsOfType<Rack>()`
- Re-add null checks and bounds checking to ensure safety
- Provide fallback if the fast path is unavailable
@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 PR successfully optimizes GetRackCount from O(N) to O(1) using NetworkMap.instance, which is a positive performance change. While Codacy results are up to standards, the implementation introduces a brittle dependency on a hardcoded index (2). This magic number should be replaced with a symbolic constant or enum if available in the Il2Cpp namespace. Furthermore, the PR lacks unit tests to verify the new conditional logic and the correctness of the fallback mechanism.

About this PR

  • No unit or integration tests were included to verify the new O(1) path or the fallback logic. Given the complexity increase (+3) in GregFacilityModule.cs and the dependency on a specific array index, automated verification is recommended to ensure reliability across different game states.

Test suggestions

  • Verify O(1) path returns correct rack count from NetworkMap index 2
  • Verify fallback to FindObjectsOfType when NetworkMap.instance is null
  • Verify fallback to FindObjectsOfType when counts array is null or insufficient length
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify O(1) path returns correct rack count from NetworkMap index 2
2. Verify fallback to FindObjectsOfType when NetworkMap.instance is null
3. Verify fallback to FindObjectsOfType when counts array is null or insufficient length

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

private readonly GregApiContext _ctx;
internal GregFacilityModule(GregApiContext ctx) => _ctx = ctx;

private const int DEVICE_INDEX_RACKS = 2;

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 optimization to O(1) is a significant improvement, but relying on a magic index '2' (DEVICE_INDEX_RACKS) is a maintenance risk. It depends on the internal ordering of the NetworkMap array. Check if 'global::Il2Cpp.NetworkMap' provides an enum or constant for device indices to replace this hardcoded value.

See Complexity in Codacy

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