Skip to content

Conversation

@WeixuanZ
Copy link
Owner

@WeixuanZ WeixuanZ commented Feb 1, 2026

Optimized broadcast_filtered_game_states in websocket_manager.py to use asyncio.gather for concurrent message sending.

What:

  • Refactored broadcast_filtered_game_states to create a list of async tasks for each connected player.
  • Encapsulated per-player sending logic (getting view + sending message + error handling) into an inner send_to_one helper function.
  • Executed all tasks concurrently using asyncio.gather.

Why:

  • The previous implementation iterated sequentially over active connections. With network latency (even small), the total time to broadcast to all players grew linearly with the number of players.
  • For 500 players with 10ms latency, the last player would receive the update 5 seconds after the first player.
  • This optimization decouples broadcast time from the number of players (mostly), limiting it to the maximum single-client latency + processing overhead.

Measured Improvement:

  • Baseline: ~6.0060 seconds (500 clients, 10ms delay)
  • Optimized: ~0.1219 seconds (500 clients, 10ms delay)
  • Speedup: ~50x improvement in total broadcast time.

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

Optimized `broadcast_filtered_game_states` in `websocket_manager.py` to send messages to clients concurrently using `asyncio.gather`. This replaces the sequential loop which caused increasing latency for clients further down the list.

Benchmark results (500 clients, 10ms simulated delay):
- Baseline (Sequential): ~6.0s
- Optimized (Concurrent): ~0.12s
- Speedup: ~50x

This ensures all players receive state updates at roughly the same time, improving fairness and responsiveness in large games.

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.

@WeixuanZ WeixuanZ merged commit 1d59d4f into master Feb 1, 2026
2 checks passed
@WeixuanZ WeixuanZ deleted the perf-websocket-broadcast-11568541175769807919 branch February 1, 2026 15:30
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