⚡ Bolt: Optimize GetRackCount to use O(1) array lookup instead of O(N) FindObjectsOfType#183
⚡ Bolt: Optimize GetRackCount to use O(1) array lookup instead of O(N) FindObjectsOfType#183mleem97 wants to merge 1 commit into
Conversation
- 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
|
👋 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 | 0 |
| 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 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; |
There was a problem hiding this comment.
🟡 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.
💡 What: Replaced
FindObjectsOfType<Il2Cpp.Rack>()inGregFacilityModule.GetRackCount()with an O(1) lookup usingNetworkMap.instance.GetNumberOfDevices(). Added a fallback in case the singleton is uninitialized.🎯 Why:
FindObjectsOfTypeis 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 fromNetworkMap.instanceeliminates 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