perf: parallelize players/random fetches in roll_for_cards #97
Labels
No Label
ai-changes-requested
ai-failed
ai-pr-opened
ai-reviewed
ai-reviewing
ai-working
ai-working
bug
enhancement
feature
in-queue
performance
security
tech-debt
todo
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/paper-dynasty-discord#97
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
roll_for_cards()inhelpers/main.py(~line 831-884) makes 4-6 sequentialdb_get("players/random")calls per pack, one per rarity tier. For 5 packs this is 20-30 sequential HTTP round-trips.These calls are completely independent of each other — both within a single pack and across packs.
Proposed Fix
Use
asyncio.gather()to parallelize theplayers/randomcalls:Impact
This is the single biggest bottleneck in the pack opening flow. Cutting 30 sequential round-trips to ~6 parallel batches would dramatically reduce wait time.
Context
Part of pack opening performance investigation. Current flow does ~90 sequential DB round-trips for 5 packs, with a hard cap of 5 packs to keep delay tolerable.
Implementation complete on branch
performance/97-parallelize-roll-for-cards— ready for Discord testing.What changed:
roll_for_cardsrestructured into three phases: dice rolling (CPU-only), batched player fetches (asyncio.gather— one call per rarity tier across all packs), and gathered writes (cards + pack patches concurrent)SelectOpenPackxvariable bug in the dupe branch (cardset 23 path)all_playersaccumulationBenchmarks (10 packs, dev API):
Scaling stays flat — 20 packs in ~1s, 50 packs in ~3s.
Tests: 13 unit tests + 5 integration tests (real API reads, mocked writes) all passing.