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

@ -14,11 +14,44 @@
*************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Server;
/// <summary>
/// One contiguous run of records a worker serialized into its heap from a single chunk.
/// Together with the worker's lengths log this replaces per-entity placement state:
/// positions are implicit (a worker's writes are contiguous), and identity comes from
/// re-walking the same slots for range segments, or from the entities log for buffer
/// segments. The snapshot writer routes segments to files by <see cref="Owner"/>.
/// </summary>
internal readonly struct SerializedSegment
{
public readonly object Owner; // ISlotRangeSource for range segments, Persistence for buffer segments
public readonly int SlotOffset; // -1 when the segment came from a buffer chunk
public readonly int SlotCount;
public readonly long HeapStart;
public readonly int LengthsStart;
public readonly int RecordCount;
public readonly int EntitiesStart; // buffer segments only
public SerializedSegment(
object owner, int slotOffset, int slotCount, long heapStart, int lengthsStart, int recordCount,
int entitiesStart
)
{
Owner = owner;
SlotOffset = slotOffset;
SlotCount = slotCount;
HeapStart = heapStart;
LengthsStart = lengthsStart;
RecordCount = recordCount;
EntitiesStart = entitiesStart;
}
}
public class SerializationThreadWorker
{
private const int MinHeapSize = 1024 * 1024; // 1MB
@ -35,6 +68,28 @@ public class SerializationThreadWorker
private long _entitiesSerialized;
private long _bytesSerialized;
// What this worker serialized where, logged during the drain and consumed by
// WriteSnapshot on the background writer thread. Cleared once the snapshot is on disk.
private readonly List<SerializedSegment> _segments = [];
private readonly List<int> _lengths = [];
private readonly List<IGenericSerializable> _bufferEntities = [];
internal List<SerializedSegment> Segments => _segments;
internal List<int> Lengths => _lengths;
internal List<IGenericSerializable> BufferEntities => _bufferEntities;
/// <summary>
/// Releases the write logs after the snapshot is written so serialized entity
/// references don't linger between saves. Capacity is retained: the logs regrow to
/// the same size every save.
/// </summary>
internal void ReleaseWriteLogs()
{
_segments.Clear();
_lengths.Clear();
_bufferEntities.Clear();
}
public SerializationThreadWorker(int index, SerializationChunkSource chunkSource, int heapSizeHint = 0)
: this(index, chunkSource, heapSizeHint, inline: false)
{
@ -98,39 +153,55 @@ public class SerializationThreadWorker
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<byte> GetHeap(int start, int length) => _heap.AsSpan(start, length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Serialize(IGenericSerializable e, BufferWriter writer, byte threadIndex)
{
e.SerializedThread = threadIndex;
var start = e.SerializedPosition = (int)writer.Position;
e.Serialize(writer);
e.SerializedLength = (int)(writer.Position - start);
}
private static long ProcessChunk(
in SerializationChunkSource.Chunk chunk, SerializationChunkSource chunkSource,
BufferWriter writer, byte threadIndex
)
private long ProcessChunk(in SerializationChunkSource.Chunk chunk, BufferWriter writer)
{
if (chunk.Single != null)
{
Serialize(chunk.Single, writer, threadIndex);
// Self-payloads are written to their own file, so they record placement on the
// persistence itself instead of the segment logs.
var start = writer.Position;
chunk.Single.Serialize(writer);
chunk.Single.SetSelfPlacement((byte)_index, (int)start, (int)(writer.Position - start));
return 1;
}
if (chunk.Source != null)
{
return chunk.Source.SerializeRange(writer, threadIndex, chunk.Offset, chunk.Count);
var heapStart = writer.Position;
var lengthsStart = _lengths.Count;
var serialized = chunk.Source.SerializeRange(writer, _lengths, chunk.Offset, chunk.Count);
if (serialized > 0)
{
_segments.Add(
new SerializedSegment(chunk.Source, chunk.Offset, chunk.Count, heapStart, lengthsStart, serialized, -1)
);
}
return serialized;
}
var buffer = chunk.Buffer;
var count = chunk.Count;
var bufferHeapStart = writer.Position;
var bufferLengthsStart = _lengths.Count;
var entitiesStart = _bufferEntities.Count;
for (var i = 0; i < count; i++)
{
Serialize(buffer[i], writer, threadIndex);
var e = buffer[i];
var start = writer.Position;
e.Serialize(writer);
_lengths.Add((int)(writer.Position - start));
_bufferEntities.Add(e);
}
chunkSource.Return(buffer, count);
_segments.Add(
new SerializedSegment(chunk.Owner, -1, 0, bufferHeapStart, bufferLengthsStart, count, entitiesStart)
);
_chunkSource.Return(buffer, count);
return count;
}
@ -141,13 +212,14 @@ public class SerializationThreadWorker
/// </summary>
public void DrainInline()
{
ReleaseWriteLogs();
var writer = new BufferWriter(_heap, true, World.SerializedTypes);
var threadIndex = (byte)_index;
var entities = 0L;
while (_chunkSource.TryTake(out var chunk))
{
entities += ProcessChunk(in chunk, _chunkSource, writer, threadIndex);
entities += ProcessChunk(in chunk, writer);
}
_heap = writer.Buffer;
@ -160,13 +232,14 @@ public class SerializationThreadWorker
private static void Execute(object obj)
{
var worker = (SerializationThreadWorker)obj;
var threadIndex = (byte)worker._index;
var chunkSource = worker._chunkSource;
var serializedTypes = World.SerializedTypes;
while (worker._startEvent.WaitOne())
{
worker.ReleaseWriteLogs();
var writer = new BufferWriter(worker._heap, true, serializedTypes);
var entities = 0L;
var spinner = new SpinWait();
@ -177,7 +250,7 @@ public class SerializationThreadWorker
if (chunkSource.TryTake(out var chunk))
{
spinner.Reset();
entities += ProcessChunk(in chunk, chunkSource, writer, threadIndex);
entities += worker.ProcessChunk(in chunk, writer);
}
else if (pauseRequested) // Break when finished
{