Skip to content

⚡ Bolt: Optimize Game View Generation#28

Open
WeixuanZ wants to merge 3 commits intomasterfrom
bolt-optimize-view-logic-6533005148669956648
Open

⚡ Bolt: Optimize Game View Generation#28
WeixuanZ wants to merge 3 commits intomasterfrom
bolt-optimize-view-logic-6533005148669956648

Conversation

@WeixuanZ
Copy link
Owner

@WeixuanZ WeixuanZ commented Feb 7, 2026

⚡ Bolt Optimization: Removed redundant Pydantic schema creation in critical game loop.

💡 What:
Rewrote Game.get_view_for_player in backend/app/models/game.py to construct the filtered GameStateSchema directly from the internal GameState domain model, bypassing the intermediate self.to_schema() call.

🎯 Why:
Previously, broadcasting the game state to N players involved:

  1. Calling to_schema() (Creating 1 full GameStateSchema + N PlayerSchema objects).
  2. Iterating over that full schema to create N filtered PlayerSchema objects for each of the N players.
    Total overhead: N * (N + N) allocations.
    This redundant step added unnecessary CPU and memory pressure during high-frequency broadcasts (like timer ticks or phase changes).

📊 Impact:
Reduces Pydantic object creation overhead by ~50% per broadcast cycle.
Eliminates one full iteration over the player list per view generation.

🔬 Measurement:
Verified by running existing backend tests (python -m pytest backend/tests), specifically test_game_service.py which validates view filtering logic. All tests passed.


PR created automatically by Jules for task 6533005148669956648 started by @WeixuanZ

Refactors `Game.get_view_for_player` to build the `GameStateSchema` directly from internal state (`self.players`, etc.) instead of calling `self.to_schema()` first.

This optimization avoids creating a full intermediate `GameStateSchema` (and associated N `PlayerSchema` objects) for every filtered view generation. In a broadcast to N players, this reduces Pydantic object allocations from O(2N^2) to O(N^2) and eliminates the overhead of the initial full serialization.

Verification:
- Existing backend tests passed (`backend/tests/test_game_service.py` covers this logic).
- Logic maintains field correctness including `witch_has_heal` etc.

Co-authored-by: WeixuanZ <39925558+WeixuanZ@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

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

google-labs-jules bot and others added 2 commits February 7, 2026 12:53
Refactors `Game.get_view_for_player` to build the `GameStateSchema` directly from internal state (`self.players`, etc.) instead of calling `self.to_schema()` first.

This optimization avoids creating a full intermediate `GameStateSchema` (and associated N `PlayerSchema` objects) for every filtered view generation. In a broadcast to N players, this reduces Pydantic object allocations from O(2N^2) to O(N^2) and eliminates the overhead of the initial full serialization.

Additionally addresses `ruff` linting issues:
- SIM102: Consolidated nested if statements in `get_view_for_player`.
- UP042: Upgraded `(str, Enum)` inheritance to `StrEnum` (Python 3.11+) in `app/schemas/game.py` and `app/schemas/socket.py`.

Verification:
- Existing backend tests passed (`backend/tests/test_game_service.py` covers this logic).
- Logic maintains field correctness including `witch_has_heal` etc.
- `ruff check .` passes.

Co-authored-by: WeixuanZ <39925558+WeixuanZ@users.noreply.github.com>
Refactors `Game.get_view_for_player` to build the `GameStateSchema` directly from internal state (`self.players`, etc.) instead of calling `self.to_schema()` first.

This optimization avoids creating a full intermediate `GameStateSchema` (and associated N `PlayerSchema` objects) for every filtered view generation. In a broadcast to N players, this reduces Pydantic object allocations from O(2N^2) to O(N^2) and eliminates the overhead of the initial full serialization.

Additionally addresses `ruff` linting and formatting issues:
- SIM102: Consolidated nested if statements in `get_view_for_player`.
- UP042: Upgraded `(str, Enum)` inheritance to `StrEnum` (Python 3.11+) in `app/schemas/game.py` and `app/schemas/socket.py`.
- Formatted `backend/app/models/game.py` with `ruff format`.

Verification:
- Existing backend tests passed (`backend/tests/test_game_service.py` covers this logic).
- Logic maintains field correctness including `witch_has_heal` etc.
- `ruff check .` and `ruff format .` pass.

Co-authored-by: WeixuanZ <39925558+WeixuanZ@users.noreply.github.com>
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