Skip to content

⚡ Bolt: Optimize CPU move selection to be non-blocking#15

Open
Horus0305 wants to merge 1 commit intomainfrom
bolt/optimize-cpu-move-async-10175184459672933676
Open

⚡ Bolt: Optimize CPU move selection to be non-blocking#15
Horus0305 wants to merge 1 commit intomainfrom
bolt/optimize-cpu-move-async-10175184459672933676

Conversation

@Horus0305
Copy link
Copy Markdown
Owner

💡 What:
Wrapped the synchronous cpu_pick_move calls in backend/realtime/cpu.py with asyncio.to_thread. This offloads the database-intensive CPU strategy logic to a separate thread.

🎯 Why:
The cpu_pick_move function performs synchronous database queries (fetching user patterns, match history) which block the main asyncio event loop. In a real-time WebSocket server, this blocking causes latency spikes for all connected clients whenever a CPU makes a move.

📊 Impact:
Eliminates event loop blocking during CPU moves. This ensures the server remains responsive to other events (pings, human moves) even when complex CPU strategy calculations are running.

🔬 Measurement:
Verified using test_cpu_async_verify.py (which mocked the behavior and asserted async execution) and by running the existing test suite backend.test_game_engine (which passed after a minor fix to a mock function signature).


PR created automatically by Jules for task 10175184459672933676 started by @Horus0305

- Wrapped `cpu_pick_move` calls in `backend/realtime/cpu.py` with `asyncio.to_thread` to prevent blocking the event loop.
- Fixed `fixed_do_toss` mock signature in `backend/test_game_engine.py` to fix unrelated test failure.

Co-authored-by: Horus0305 <98160215+Horus0305@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

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

Copilot AI review requested due to automatic review settings February 21, 2026 17:18
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
very-important-study-material Ready Ready Preview, Comment Feb 21, 2026 5:18pm

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes CPU move selection in the real-time WebSocket server by offloading blocking database operations to a thread pool using asyncio.to_thread(). The cpu_pick_move function performs synchronous database queries to fetch user patterns and match history, which previously blocked the asyncio event loop and caused latency spikes for all connected clients. By running these operations in separate threads, the event loop remains responsive to other concurrent events.

Changes:

  • Wrapped all cpu_pick_move calls with asyncio.to_thread() to prevent event loop blocking
  • Fixed test mock function signature to match the actual do_toss method signature

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
CricketGame/backend/realtime/cpu.py Wrapped all four cpu_pick_move invocations (in maybe_cpu_move and auto_play_cpu_match) with asyncio.to_thread() to offload blocking database operations to a thread pool
CricketGame/backend/test_game_engine.py Fixed mock function fixed_do_toss signature to accept optional caller parameter, matching the actual Match.do_toss method signature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI added a commit that referenced this pull request Feb 21, 2026
Co-authored-by: Horus0305 <98160215+Horus0305@users.noreply.github.com>
Horus0305 added a commit that referenced this pull request Feb 21, 2026
Merge PRs #14, #15, #16: auth fix, async CPU moves, lobby a11y
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.

2 participants