fix(shared): coalesce free spans in the region index #25
No reviewers
Labels
No labels
Compat/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
scope/assets
scope/client
scope/networking
scope/renderer
scope/scripting
scope/server
scope/shared
scope/workspace
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Synvael/synvael!25
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/region-free-list"
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?
What does this PR do?
I made the free list in
RegionIndex(crates/shared/src/save/region.rs) sorted by offset and coalesced.push_freenow finds its place with a binary search and merges the new span with every span it touches (offset + length == next.offset) or overlaps. A gap of one byte keeps spans apart. A zero-length span is ignored. It returnsbool:falseleaves the list untouched whenoffset + lengthoverflowsu64,trueotherwise.decodereads the stored free spans through a newread_free_list. It drops empty spans, sorts once, refuses spans whose end overflows or that overlap, and coalesces adjacent ones in a single linear pass. A loaded index therefore satisfies the same invariant as one built withpush_free.SYNRlength field is au32, so a free extent longer thanu32::MAXbytes cannot be one span.push_extentstores such an extent as fullu32::MAXspans followed by a remainder. Any record of up tou32::MAXbytes that fits the extent fits its first span.u32by specification, not an oversight.FreeSpanandpush_freedocument the sorted, coalesced invariant and the long-extent exception.RegionFile::allocateincrates/serveris unchanged. It already goes throughpush_freeand still picks the same best-fit span for the same free list, so no existing record moves.Why is this change necessary?
push_freeappended spans without merging them, andallocatesplits a best-fit span and pushes the leftover back. A region whose records are rewritten repeatedly ground its free space into slivers too small for any record, so the file kept growing while its live content did not. Every rewrite of a record frees a span, so this gets worse as more regions take write traffic.Scope of Changes
server, shared
Testing
Automated, run on this branch:
cargo check -p shared -p server: passes.cargo test -p shared: 374 passed, 0 failed.cargo test -p server: 98 passed, 0 failed.cargo clippy --all-targets --all-features -- -D warnings: passes.cargo fmt --all -- --check: passes.New tests in
crates/shared/src/tests/region.rscover:u32::MAX, on push and on decode;u64offset range, andpush_freerejecting an end that overflows;rewriting_records_of_varying_sizes_keeps_the_file_boundedincrates/server/src/tests/region_file.rsrewrites 8 records of 100 to 999 bytes over 200 saves. The region file length was 138007 bytes before the fix and is 78461 bytes after.Manual: none. I did not run the game against a real world.
Additional Context
push_freeabsorbs an overlapping span into the existing one and rejects an end that overflowsu64by returningfalse.decoderefuses a stored index whose free spans overlap or overflow, since that file is corrupt, and coalesces adjacent spans.SYNRbyte layout andREGION_FORMAT_VERSIONare unchanged. Old files load, and their free lists come out coalesced.Related Issues
None.
Checklist
dev(or a feature branch offdev) and my PR targetsdev.///doc comments for Rust) where necessary.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.