perf(saves): drop the 9-byte per-entity placement state; write snapshots from worker segment logs

Every ISerializable carried SerializedThread/SerializedPosition/SerializedLength
so WriteSnapshot could gather each entity's bytes from the worker heaps in
dictionary order. The idx records absolute positions, so bin order is free -
the snapshot can be written in worker-heap order instead, and the join inverts:

- Chunks are persistence-homogeneous: SerializeAll declares the owner at each
  boundary, publishing the partial chunk on change.
- Workers log segments (owner, slot range, heap start) plus one length per
  record as they serialize. Positions are implicit because a worker's writes
  are contiguous; identity comes from re-walking the same snapshot slots in
  the same order (guaranteed stable - mutations divert to the pending queues
  until PostWorldSave), or from an entities log on the fallback path.
- WriteSnapshot routes segments by owner, emits idx entries during the
  re-walk, and writes each segment's heap bytes as a single span instead of
  one copy per entity, which also speeds up the background write phase.
- Persistence self-payloads keep placement as three private fields on the
  ~dozens of persistence instances; PushSingle is now typed accordingly.

Net effect: 9 bytes (plus padding) of resident state removed from every item,
mobile, guild, and account on every shard; three interface-property stores
per entity leave the drain hot path (stamping dirtied one cache line per
entity mid-freeze - the lengths log is a single sequential stream); and the
vestigial loader-side length stamp is gone. The transient cost is ~4 bytes
per entity in pooled per-worker logs that are released after each write.

The save format is unchanged (idx v3, same loader); only the write-side
mechanics moved. Adds an end-to-end round-trip test that drives real workers
through the chunk source, snapshots from the segment logs, and reloads.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kamron Batman 2026-07-14 13:49:12 -07:00
parent 9780fc6634
commit 3b75b96008
No known key found for this signature in database
GPG key ID: 7D81DF26D9A5D94A
19 changed files with 625 additions and 189 deletions

View file

@ -25,14 +25,24 @@ public abstract class GenericPersistence : Persistence, IGenericSerializable
public string Name { get; }
public string SaveFilePath { get; protected set; } // "<Folder>/<System>.bin"
public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }
// Placement of the self-payload in the worker heaps for the most recent save. Only
// persistences carry placement state — entities are located through the per-worker
// segment logs instead.
private protected byte _selfThread;
private protected int _selfPosition;
private protected int _selfLength;
internal void SetSelfPlacement(byte thread, int position, int length)
{
_selfThread = thread;
_selfPosition = position;
_selfLength = length;
}
private long _loadedFileLength;
// Scheduling estimate only: previous save's payload size, or the loaded file size before the first save.
internal long EstimatedSize => SerializedLength > 0 ? SerializedLength : _loadedFileLength;
internal long EstimatedSize => _selfLength > 0 ? _selfLength : _loadedFileLength;
public GenericPersistence(string name, int priority) : base(priority)
{
@ -49,7 +59,7 @@ public abstract class GenericPersistence : Persistence, IGenericSerializable
public override void WriteSnapshot(string savePath, HashSet<Type> typeSet)
{
if (SerializedLength == 0)
if (_selfLength == 0)
{
return;
}
@ -62,11 +72,7 @@ public abstract class GenericPersistence : Persistence, IGenericSerializable
using var binFs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None);
var thread = SerializedThread;
var heapStart = SerializedPosition;
var heapLength = SerializedLength;
binFs.Write(threads[thread].GetHeap(heapStart, heapLength));
binFs.Write(threads[_selfThread].GetHeap(_selfPosition, _selfLength));
}
public override unsafe void Deserialize(string savePath, Dictionary<ulong, string> typesDb)