Parallelize the snapshot rebuild across cores (opt-in, validated)#586
Open
ptrlrd wants to merge 1 commit into
Open
Parallelize the snapshot rebuild across cores (opt-in, validated)#586ptrlrd wants to merge 1 commit into
ptrlrd wants to merge 1 commit into
Conversation
The snapshot walk reads every run blob (~750k files) and is CPU-bound in the pure-Python accumulation, so it takes ~80 min single-threaded. Split _build_cache_data into _accumulate (per-run-chunk) + _finalize, so the walk can run as a map-reduce: each core accumulates a chunk in its own process, the raw accumulators are merged, and the result is finalized once. Every accumulator is a plain sum (or min/max for the three records), so the merge is lossless. OFF by default (ENTITY_STATS_REBUILD_WORKERS=1). Set it to N>1 to use N cores, with an automatic fall back to serial on any error. Uses the "spawn" start method, not fork: this runs in the leader's refresher thread, and forking a multithreaded process can deadlock the child and hang the refresher. Safety: the refactored serial path is byte-identical to the original (verified), and backend/scripts/validate_parallel_rebuild.py runs the full walk both ways and proves they match — run it against the real run data before enabling parallel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Speeds up the ~80-minute snapshot rebuild by parallelizing the blob walk across cores. The walk reads every run blob (~750k files) and is CPU-bound in pure-Python accumulation, so it's a natural map-reduce.
How
Split
_build_cache_datainto_accumulate(rows) → raw accumulatorsand_finalize(raw) → snapshot. The walk can now run either serially (as before) or as a map-reduce: each core accumulates a chunk of runs in its own process, the raw accumulators are merged, and the result is finalized once. Every accumulator is a plain sum (or min/max for the fastest-win / longest-run / biggest-deck records), so the merge is lossless — addedmerge()to community/charts/encounter stats and the entity-cache merge inrun_entity_stats.Safety — this is the whole point
Given how central this code is, I built it to be provably correct and default-off:
ENTITY_STATS_REBUILD_WORKERS=1(serial, unchanged behavior). Set it to N>1 to use N cores. Deploying this PR alone changes nothing — the serial path is byte-identical to today, so no snapshot version bump and no forced rebuild.backend/scripts/validate_parallel_rebuild.pyruns the full walk both ways in one process and deep-compares (ints exact, floats within 1e-9; serialized-dict lists compared order-insensitively since the merge orders them differently). Run it against the real run data before enabling.Validated locally
enc_hist/death_roomare int counters not lists; relicact_picks/act_winsarrays) — now fixed.Rollout
python -m scripts.validate_parallel_rebuild 4— runs the full walk twice on your real 750k runs and prints IDENTICAL (or the diffs). This takes a while (two full walks) but proves correctness on your data.ENTITY_STATS_REBUILD_WORKERS=4and the next rebuild uses 4 cores.Expected ~3-4x at scale (per-run work dominates over the spawn/pickle overhead once you're past a few thousand runs). Doesn't touch the walk being file-by-file — moving blobs into Mongo is a separate, bigger win if you want it later.