⚡ Bolt: Eliminate LINQ Count() enumerator allocation in hot path#41
⚡ Bolt: Eliminate LINQ Count() enumerator allocation in hot path#41lordhippo wants to merge 4 commits into
Conversation
- **What:** Replaced the `Context.OwnRobots.Count(robot => robot.Seen)` LINQ method call in `Knowledge.Update()` with an explicit `foreach` loop and counter. - **Why:** The `Knowledge.Update()` method is part of the AI hot path, running every frame (~100Hz). Using the LINQ `Count(predicate)` method on collections forces a cast to `IEnumerable<T>`, which boxes the underlying struct enumerator, causing a heap allocation on every frame. - **Impact:** Eliminates 1 enumerator boxing allocation per frame per team. At 100Hz with two teams, this saves ~200 allocs/sec from the Gen 0 heap, reducing GC pressure and potential latency spikes in the real-time loop. - **Verification:** Run `dotnet-counters monitor --counters System.Runtime[gen-0-gc-count,alloc-rate]` to observe reduced allocation rates during gameplay. Co-authored-by: lordhippo <5122916+lordhippo@users.noreply.github.com>
|
👋 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. |
- **What:** Replaced the `Context.OwnRobots.Count(robot => robot.Seen)` LINQ method call in `Knowledge.Update()` with an explicit `foreach` loop and counter. - **Why:** The `Knowledge.Update()` method is part of the AI hot path, running every frame (~100Hz). Using the LINQ `Count(predicate)` method on collections forces a cast to `IEnumerable<T>`, which boxes the underlying struct enumerator, causing a heap allocation on every frame. - **Impact:** Eliminates 1 enumerator boxing allocation per frame per team. At 100Hz with two teams, this saves ~200 allocs/sec from the Gen 0 heap, reducing GC pressure and potential latency spikes in the real-time loop. - **Verification:** Run `dotnet-counters monitor --counters System.Runtime[gen-0-gc-count,alloc-rate]` to observe reduced allocation rates during gameplay. - Also fixed several build warnings (unused fields and possible null dereferences in BallPlacement and OurFreekick). Co-authored-by: lordhippo <5122916+lordhippo@users.noreply.github.com>
- **What:** Replaced the `Context.OwnRobots.Count(robot => robot.Seen)` LINQ method call in `Knowledge.Update()` with an explicit `foreach` loop and counter. - **Why:** The `Knowledge.Update()` method is part of the AI hot path, running every frame (~100Hz). Using the LINQ `Count(predicate)` method on collections forces a cast to `IEnumerable<T>`, which boxes the underlying struct enumerator, causing a heap allocation on every frame. - **Impact:** Eliminates 1 enumerator boxing allocation per frame per team. At 100Hz with two teams, this saves ~200 allocs/sec from the Gen 0 heap, reducing GC pressure and potential latency spikes in the real-time loop. - **Verification:** Run `dotnet-counters monitor --counters System.Runtime[gen-0-gc-count,alloc-rate]` to observe reduced allocation rates during gameplay. Co-authored-by: lordhippo <5122916+lordhippo@users.noreply.github.com>
- **What:** Replaced the `Context.OwnRobots.Count(robot => robot.Seen)` LINQ method call in `Knowledge.Update()` with an explicit `foreach` loop and counter. - **Why:** The `Knowledge.Update()` method is part of the AI hot path, running every frame (~100Hz). Using the LINQ `Count(predicate)` method on collections forces a cast to `IEnumerable<T>`, which boxes the underlying struct enumerator, causing a heap allocation on every frame. - **Impact:** Eliminates 1 enumerator boxing allocation per frame per team. At 100Hz with two teams, this saves ~200 allocs/sec from the Gen 0 heap, reducing GC pressure and potential latency spikes in the real-time loop. - **Verification:** Run `dotnet-counters monitor --counters System.Runtime[gen-0-gc-count,alloc-rate]` to observe reduced allocation rates during gameplay. - Also fixed several build warnings (unused fields and possible null dereferences in BallPlacement and OurFreekick). Co-authored-by: lordhippo <5122916+lordhippo@users.noreply.github.com>
💡 What: Replaced the
Context.OwnRobots.Count(robot => robot.Seen)LINQ method call inKnowledge.Update()with an explicitforeachloop and counter. Also documented the learning in.jules/bolt.md.🎯 Why: The
Knowledge.Update()method is part of the AI hot path, running every frame (~100Hz). Using the LINQCount(predicate)method on collections forces a cast toIEnumerable<T>, which boxes the underlying struct enumerator, causing a heap allocation on every single frame. GC pauses are the primary enemy in this real-time loop.📊 Impact: Eliminates 1 enumerator boxing allocation per frame per team. At 100Hz with two teams, this saves ~200 allocs/sec from the Gen 0 heap, reducing GC pressure and potential latency spikes.
🔬 Measurement: Use
dotnet-counters monitor --counters System.Runtime[gen-0-gc-count,alloc-rate]to observe reduced allocation rates during gameplay.PR created automatically by Jules for task 3894145543788865041 started by @lordhippo