fix(shared): coalesce free spans in the region index #25

Open
Serkyo wants to merge 5 commits from fix/region-free-list into dev
Owner

What does this PR do?

I made the free list in RegionIndex (crates/shared/src/save/region.rs) sorted by offset and coalesced.

  • push_free now 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 returns bool: false leaves the list untouched when offset + length overflows u64, true otherwise.
  • decode reads the stored free spans through a new read_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 with push_free.
  • The SYNR length field is a u32, so a free extent longer than u32::MAX bytes cannot be one span. push_extent stores such an extent as full u32::MAX spans followed by a remainder. Any record of up to u32::MAX bytes that fits the extent fits its first span.
  • I corrected the comment on the stamp field: the stamp is a u32 by specification, not an oversight.
  • FreeSpan and push_free document the sorted, coalesced invariant and the long-extent exception.

RegionFile::allocate in crates/server is unchanged. It already goes through push_free and still picks the same best-fit span for the same free list, so no existing record moves.

Why is this change necessary?

push_free appended spans without merging them, and allocate splits 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.rs cover:

  • shuffled pushes with the sorted and coalesced invariant checked after each push;
  • three consecutive spans merging into one in every order;
  • the one-byte gap;
  • an overlapping push being absorbed;
  • the rebalance past u32::MAX, on push and on decode;
  • a span at the top of the u64 offset range, and push_free rejecting an end that overflows;
  • decoding adjacent spans into a coalesced list;
  • decoding overlapping or overflowing spans being refused;
  • decoding many descending spans;
  • decoding an index that holds an empty span next to a real one.

rewriting_records_of_varying_sizes_keeps_the_file_bounded in crates/server/src/tests/region_file.rs rewrites 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

  • Overlap handling is split by entry point. push_free absorbs an overlapping span into the existing one and rejects an end that overflows u64 by returning false. decode refuses a stored index whose free spans overlap or overflow, since that file is corrupt, and coalesces adjacent spans.
  • The SYNR byte layout and REGION_FORMAT_VERSION are unchanged. Old files load, and their free lists come out coalesced.
  • Out of scope: the compaction trigger, and truncating a free span that reaches the end of the file.

None.

Checklist

  • I have branched from dev (or a feature branch off dev) and my PR targets dev.
  • I have kept my changes focused to a single concept.
  • I have added or updated documentation (/// doc comments for Rust) where necessary.
  • I have tested my changes and described any relevant automated or manual testing above.
### What does this PR do? I made the free list in `RegionIndex` (`crates/shared/src/save/region.rs`) sorted by offset and coalesced. - `push_free` now 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 returns `bool`: `false` leaves the list untouched when `offset + length` overflows `u64`, `true` otherwise. - `decode` reads the stored free spans through a new `read_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 with `push_free`. - The `SYNR` length field is a `u32`, so a free extent longer than `u32::MAX` bytes cannot be one span. `push_extent` stores such an extent as full `u32::MAX` spans followed by a remainder. Any record of up to `u32::MAX` bytes that fits the extent fits its first span. - I corrected the comment on the stamp field: the stamp is a `u32` by specification, not an oversight. - `FreeSpan` and `push_free` document the sorted, coalesced invariant and the long-extent exception. `RegionFile::allocate` in `crates/server` is unchanged. It already goes through `push_free` and still picks the same best-fit span for the same free list, so no existing record moves. ### Why is this change necessary? `push_free` appended spans without merging them, and `allocate` splits 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.rs` cover: - shuffled pushes with the sorted and coalesced invariant checked after each push; - three consecutive spans merging into one in every order; - the one-byte gap; - an overlapping push being absorbed; - the rebalance past `u32::MAX`, on push and on decode; - a span at the top of the `u64` offset range, and `push_free` rejecting an end that overflows; - decoding adjacent spans into a coalesced list; - decoding overlapping or overflowing spans being refused; - decoding many descending spans; - decoding an index that holds an empty span next to a real one. `rewriting_records_of_varying_sizes_keeps_the_file_bounded` in `crates/server/src/tests/region_file.rs` rewrites 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 - Overlap handling is split by entry point. `push_free` absorbs an overlapping span into the existing one and rejects an end that overflows `u64` by returning `false`. `decode` refuses a stored index whose free spans overlap or overflow, since that file is corrupt, and coalesces adjacent spans. - The `SYNR` byte layout and `REGION_FORMAT_VERSION` are unchanged. Old files load, and their free lists come out coalesced. - Out of scope: the compaction trigger, and truncating a free span that reaches the end of the file. ### Related Issues None. ### Checklist - [x] I have branched from `dev` (or a feature branch off `dev`) and my PR targets `dev`. - [x] I have kept my changes focused to a single concept. - [x] I have added or updated documentation (`///` doc comments for Rust) where necessary. - [x] I have tested my changes and described any relevant automated or manual testing above.
fix(shared): drop empty stored free spans before the overlap check
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 2s
CLA Signed All authors have signed the CLA.
CLA Check / cla-check (pull_request_target) Successful in 2s
CI / Rust Check, Lint & Test (pull_request) Successful in 37m28s
CI / Dependency Licenses & Advisories (pull_request) Successful in 29s
CI / Lua Lint & Format (pull_request) Successful in 7s
CI / Commit Message Lint (pull_request) Successful in 2s
CI / LFS Pointer Guard (pull_request) Successful in 5s
44072bbfcb
All checks were successful
Auto Labeler / label-scope (pull_request_target) Successful in 2s
CLA Signed All authors have signed the CLA.
Required
CLA Check / cla-check (pull_request_target) Successful in 2s
CI / Rust Check, Lint & Test (pull_request) Successful in 37m28s
Required
Details
CI / Dependency Licenses & Advisories (pull_request) Successful in 29s
CI / Lua Lint & Format (pull_request) Successful in 7s
Required
Details
CI / Commit Message Lint (pull_request) Successful in 2s
CI / LFS Pointer Guard (pull_request) Successful in 5s
Required
Details
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/region-free-list:fix/region-free-list
git switch fix/region-free-list
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Synvael/synvael!25
No description provided.