Repository navigation
⚡ fix: resolve N+1 Query in Loop in PoliticalSystem.cs - #164
Conversation
- Cached ArchetypeQuery instances inside `PoliticalSystem` to prevent ECS query allocations when searching components. - Extracted the O(N) `FindOfficeholder` method into an O(N) initial pass over the dataset in `ResolveElection`, caching the results into a Dictionary for subsequent O(1) lookups during iteration.
|
👋 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:
store.Query<...>()calls in loops/methods to cachedreadonly ArchetypeQueryfields initialized once in thePoliticalSystemconstructor.FindOfficeholdercall inside theResolveElectionwith an.ToDictionarylookup (specifically utilizingGroupBy(...).ToDictionary(...)to account for potential edge cases with non-unique job assignments).🎯 Why:
Dynamically querying ECS components is expensive and resolving archetype queries dynamically is particularly costly. Furthermore, invoking a query over all agents (the O(N)
FindOfficeholder) within an iterative loop of appointed offices (O(M)) yields an N+1 operation (overall time complexity $O(N \times M)$) which can scale terribly with larger datasets. Caching the ECS queries as well as dictionary mappings effectively reduces the time complexity and removes the N+1 issue.📊 Measured Improvement:
Due to test environments focusing more on functionality correctness, the precise benchmark is theoretically O(N * M) scaled back to O(N + M). Time complexity operations per election cycle scale down dramatically, enabling vastly improved logic loops.
PR created automatically by Jules for task 6659025229058977111 started by @wnj00524