⚡ Bolt: Optimize GetRackCount with O(1) device lookup#165
Conversation
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.
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 5 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
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
| 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; | ||
| } |
There was a problem hiding this comment.
🟡 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.
💡 What: Replaced
FindObjectsOfType<global::Il2Cpp.Rack>().Lengthwith an O(1) lookup usingIl2Cpp.NetworkMap.instance.GetNumberOfDevices()[2].🎯 Why:
FindObjectsOfTypeis 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 testand observe rack query duration in a Unity profiler session.PR created automatically by Jules for task 17070377016881305945 started by @mleem97