Skip to content

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

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

⚡ Bolt: Optimize GetRackCount with O(1) device lookup#165
mleem97 wants to merge 1 commit into
mainfrom
bolt/optimize-getrackcount-17070377016881305945

Conversation

@mleem97

@mleem97 mleem97 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced FindObjectsOfType<global::Il2Cpp.Rack>().Length with an O(1) lookup using Il2Cpp.NetworkMap.instance.GetNumberOfDevices()[2].
🎯 Why: FindObjectsOfType is an O(N) operation that iterates over all objects in the scene, which creates a noticeable CPU overhead and potential GC allocations during queries.
📊 Impact: Reduces CPU time for rack count queries from ~O(N) to O(1), preventing potential frame drops or hitching.
🔬 Measurement: Verify tests pass via dotnet test and observe rack query duration in a Unity profiler session.


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

Replaced `FindObjectsOfType<global::Il2Cpp.Rack>().Length` with an O(1) lookup using `Il2Cpp.NetworkMap.instance.GetNumberOfDevices()[2]`.
`FindObjectsOfType` is an O(N) operation that iterates over all objects in the scene, which creates a noticeable CPU overhead and potential GC allocations during queries.
Reduces CPU time for rack count queries from ~O(N) to O(1), preventing potential frame drops or hitching.
@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 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 successfully implements the O(1) optimization for GetRackCount by leveraging NetworkMap.instance, aligning with the core requirements. However, this optimization introduces a behavioral change: the original FindObjectsOfType method only counts active objects in the hierarchy, whereas NetworkMap typically tracks all registered entities regardless of their active state.

Additionally, GregFacilityModule.cs has been identified as an uncovered complex file following a complexity increase of 5. The lack of unit tests for the new branching logic and the fallback mechanisms makes this a high-risk change. While the PR is technically 'up to standards' according to Codacy, the discrepancy in object counting and the absence of verification for the fallback paths should be addressed before merging.

About this PR

  • The mapping of index 2 to 'Racks' is hardcoded based on external Il2Cpp knowledge. This represents a fragile dependency; if the underlying game structure or the GetNumberOfDevices() array layout changes, the module may return incorrect data or trigger unnecessary fallbacks without warning.

Test suggestions

  • Missing recommended test scenario: Verify GetRackCount returns index 2 from GetNumberOfDevices when NetworkMap instance is valid and array length > 2
  • Missing recommended test scenario: Verify fallback to FindObjectsOfType when NetworkMap.instance is null
  • Missing recommended test scenario: Verify fallback to FindObjectsOfType when GetNumberOfDevices returns an array with length <= 2
  • Missing recommended test scenario: Verify fallback to FindObjectsOfType when GetNumberOfDevices returns null
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Missing recommended test scenario: Verify GetRackCount returns index 2 from GetNumberOfDevices when NetworkMap instance is valid and array length > 2
2. Missing recommended test scenario: Verify fallback to FindObjectsOfType when NetworkMap.instance is null
3. Missing recommended test scenario: Verify fallback to FindObjectsOfType when GetNumberOfDevices returns an array with length <= 2
4. Missing recommended test scenario: Verify fallback to FindObjectsOfType when GetNumberOfDevices returns null

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

Comment on lines +20 to +28
var deviceCounts = global::Il2Cpp.NetworkMap.instance.GetNumberOfDevices();
if (deviceCounts != null && deviceCounts.Length > DEVICE_INDEX_RACKS)
{
return deviceCounts[DEVICE_INDEX_RACKS];
}
}

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 behavioral change: FindObjectsOfType<T>() (the fallback) excludes inactive objects, whereas NetworkMap.instance.GetNumberOfDevices() typically includes all registered entities. This discrepancy will cause GetRackCount() to return different results than the original implementation when inactive racks are present. Furthermore, the optimization path introduces a dependency on a specific array layout and increases method complexity by 5. Given that this module is currently an uncovered complex file, unit tests are required to verify the optimized path and the safety of the FindObjectsOfType fallback.

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