Skip to content

⚡ fix: resolve N+1 Query in Loop in PoliticalSystem.cs - #164

Merged
wnj00524 merged 1 commit into
mainfrom
fix/n-plus-one-query-political-system-6659025229058977111
Sep 28, 2026
Merged

wnj00524 merged 1 commit into
mainfrom
fix/n-plus-one-query-political-system-6659025229058977111

Conversation

@google-labs-jules

Copy link
Copy Markdown
Contributor

💡 What:

  • Converted dynamic store.Query<...>() calls in loops/methods to cached readonly ArchetypeQuery fields initialized once in the PoliticalSystem constructor.
  • Replaced the $O(N)$ FindOfficeholder call inside the $O(M)$ appointed offices iteration of ResolveElection with an $O(N)$ preceding .ToDictionary lookup (specifically utilizing GroupBy(...).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

- 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.
@google-labs-jules

Copy link
Copy Markdown
Contributor Author

👋 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.

@wnj00524
wnj00524 merged commit e619665 into main Sep 28, 2026
2 checks passed
@wnj00524
wnj00524 deleted the fix/n-plus-one-query-political-system-6659025229058977111 branch September 28, 2026 08:23
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