Skip to content

Optimize MCTS playout performance (~2.4x speedup)#371

Open
cullback wants to merge 2 commits intobcollazo:masterfrom
cullback:mcts-perf-optimizations
Open

Optimize MCTS playout performance (~2.4x speedup)#371
cullback wants to merge 2 commits intobcollazo:masterfrom
cullback:mcts-perf-optimizations

Conversation

@cullback
Copy link
Copy Markdown

Summary

  • Cache player_key strings: Precompute "P0", "P1" prefixes in State.__init__ instead of constructing via f-string on every call (was 138M calls / 26s)
  • Switch Enums to IntEnum: Color, ActionType, ActionPrompt, NodeRef, EdgeRef now use IntEnum for faster hashing (was 171M calls / 17.5s)
  • Optimize longest road DFS: Iterative DFS with visited-set mutation, leaf extension fast path, start-node filtering (was 132k calls / 26s)
  • Add Game.play_random(): Lightweight playout loop that skips action validation overhead
  • Fix division-by-zero: Guard against empty results when all games hit the turn limit

Profiled with catanatron-play --players M:5,M:5 --num 2 --config-vps-to-win 15 --config-discard-limit 9:

  • Before: ~357s
  • After: ~150s

Test plan

  • python -m pytest tests/ passes
  • Profile confirms reduced time in longest_acyclic_path, player_key, and enum hashing

- Cache player_key strings to avoid 138M f-string allocations per run
- Switch Color, ActionType, ActionPrompt, NodeRef, EdgeRef to IntEnum
  for faster hashing (171M calls)
- Optimize longest_acyclic_path: visited-set mutation instead of list
  copying, DFS only from leaves/branch points, leaf extension fast path
- Add Game.play_random() to skip action validation in random playouts
Both accumulators skip tracking when there's no winner (turn limit
reached). The summary code then divides by zero. Guard all division
sites in play.py and accumulators.py.
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