perf(snapshot): stop persisting derived sparse-set tables in snapshots - #935
Open
AnthonyWadham wants to merge 1 commit into
Open
perf(snapshot): stop persisting derived sparse-set tables in snapshots#935AnthonyWadham wants to merge 1 commit into
AnthonyWadham wants to merge 1 commit into
Conversation
AnthonyWadham
force-pushed
the
Anthony/drop-derived-snapshot-tables
branch
2 times, most recently
from
August 6, 2026 18:52
1cbaf8d to
f2d665f
Compare
AnthonyWadham
force-pushed
the
Anthony/drop-derived-snapshot-tables
branch
from
August 6, 2026 20:59
f2d665f to
e191abf
Compare
AnthonyWadham
marked this pull request as ready for review
August 6, 2026 21:00
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.
Snapshots persisted two sparse sets that are pure derived data:
WorldState.entity_arch— which archetype an entity belongs to. Already implied by which archetype'sentitieslist the entity appears in.Archetype.rows— entity → row. The exact inverse ofArchetype.entities.Both are now rebuilt on load instead of written to storage. Each is a
repeated int64sized by the highest entity ID ever allocated, not by the live entity count, so they were the part of a snapshot that grew with churn rather than with world size.Breaking: snapshot format 1 → 2
CurrentVersiongoes to2, andValidateVersionkeeps its exact-equality policy — a version-1 snapshot is refused at boot, not migrated.The direction that matters here is the reverse one. A v1 snapshot is in fact readable by this build: protobuf ignores the fields it no longer knows, and the rebuild only needs
entitiesandarchetypes, both of which v1 carries. What is not safe is an older build reading a v2 snapshot — it would callfromInt64Slice(nil), get empty tables, and come up silently wrong.Bumping the version is what stops that: the older build already refuses anything above its own
CurrentVersion. Staying at 1 would leave a build on an older checkout accepting these snapshots and constructing a broken world with no error — which in practice means branch-switching quietly corrupts local state.Impact: any existing v1 snapshot will not restore after this lands. Wipe local state, or land a migration path first. Nothing is deployed, so this is a one-time developer cost in exchange for a loud failure instead of a silent one.
Benchmarks
BenchmarkSnapshotStore,pkg/plugin/physics2d/test. linux/amd64, go1.26.5, GOMAXPROCS 16,-benchtime=200x -count=5. Baseline isscott/cardinal-persiststate-perf(this PR's base). Times are medians of 5 with full ranges; byte counts are exact and identical across all runs.Snapshot size — the point of the change
Serialization on the production path (
SingleMarshal)The 5000-body ranges do not overlap. The 1000-body ranges overlap slightly, so read that one as "smaller, roughly in line with the byte reduction."
What this does not claim
BenchmarkSnapshotTickwas too noisy on my machine to attribute. TheRate_1000000control case — which never takes a snapshot and is untouched by this change — swung −10.9% between runs. Nothing from that benchmark is reported here. Worth a rerun on a quiet box if anyone wants the end-to-end tick figure.fromInt64Slicecopy it replaces, and it drops the varint decode of two large arrays. That reasoning says restore should be neutral-to-faster, but it is reasoning, not a measurement.Testing
go test ./pkg/cardinal/...— all passing.Round-trip coverage in
world_state_internal_test.goandarchetype_internal_test.gonow compares the rebuilt sparse sets throughget()over the key domain rather than by backing array, because a rebuilt table is sized to the highest live entity while the original grew to its high-water mark.New:
TestRestoredWorldStaysUsabledrives a restored world instead of only comparing it — reads every survivor through bothIter()(archetype entity lists) andGetByID()(the rebuilt tables), then creates, destroys, and snapshots a second time.One thing reviewers should know:
TestSnapshotRoundTripThroughStorageno longer covers the sparse sets. Itsproto.EqualcomparesToProtooutput, andToProtodoesn't emit these tables anymore — with therowsrebuild deleted outright, that test still reportsok. I verified this by mutation: disabling either rebuild loop failsTestRestoredWorldStaysUsableand nothing else. That new test is the only thing standing on this now.