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

@ -15,6 +15,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -29,10 +30,10 @@ namespace Server;
public interface ISlotRangeSource
{
/// <summary>
/// Serializes every occupied slot in [offset, offset + count) into the writer,
/// stamping each entity's thread/position/length. Returns the number serialized.
/// Serializes every occupied slot in [offset, offset + count) into the writer, appending
/// each record's byte length to <paramref name="lengths"/>. Returns the number serialized.
/// </summary>
int SerializeRange(BufferWriter writer, byte threadIndex, int offset, int count);
int SerializeRange(BufferWriter writer, List<int> lengths, int offset, int count);
}
/// <summary>
@ -54,22 +55,24 @@ public sealed class SerializationChunkSource
internal readonly struct Chunk
{
public readonly IGenericSerializable Single;
public readonly GenericPersistence Single;
public readonly IGenericSerializable[] Buffer;
public readonly ISlotRangeSource Source;
public readonly Persistence Owner; // buffer chunks only; ranges use Source, singles record their own placement
public readonly int Offset;
public readonly int Count;
public Chunk(IGenericSerializable single)
public Chunk(GenericPersistence single)
{
Single = single;
Count = 1;
}
public Chunk(IGenericSerializable[] buffer, int count)
public Chunk(IGenericSerializable[] buffer, int count, Persistence owner)
{
Buffer = buffer;
Count = count;
Owner = owner;
}
public Chunk(ISlotRangeSource source, int offset, int count)
@ -86,6 +89,21 @@ public sealed class SerializationChunkSource
// Producer state - written only by the game loop thread.
private IGenericSerializable[] _current;
private int _count;
private Persistence _currentOwner;
/// <summary>
/// Declares the owner of subsequently pushed entities. Publishes the partial chunk when
/// the owner changes, keeping buffer chunks persistence-homogeneous so workers can
/// attribute their serialized records to a persistence without any per-entity state.
/// </summary>
public void SetOwner(Persistence owner)
{
if (!ReferenceEquals(_currentOwner, owner))
{
Flush();
_currentOwner = owner;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Push(IGenericSerializable entity)
@ -99,18 +117,18 @@ public sealed class SerializationChunkSource
if (++_count == ChunkCapacity)
{
_chunks.Enqueue(new Chunk(current, ChunkCapacity));
_chunks.Enqueue(new Chunk(current, ChunkCapacity, _currentOwner));
_current = null;
_count = 0;
}
}
/// <summary>
/// Publishes the entity as a dedicated chunk regardless of its estimated size.
/// Used for persistence self-payloads, which can be large on the first save
/// before a <see cref="IGenericSerializable.SerializedLength"/> estimate exists.
/// Publishes a persistence self-payload as a dedicated chunk regardless of its
/// estimated size — it can be large on the first save before an estimate exists.
/// The worker records the payload's placement on the persistence itself.
/// </summary>
public void PushSingle(IGenericSerializable entity) => _chunks.Enqueue(new Chunk(entity));
public void PushSingle(GenericPersistence persistence) => _chunks.Enqueue(new Chunk(persistence));
/// <summary>
/// Publishes slot ranges covering [0, slotCount) of a directly-iterable persistence.
@ -133,7 +151,7 @@ public sealed class SerializationChunkSource
{
if (_count > 0)
{
_chunks.Enqueue(new Chunk(_current, _count));
_chunks.Enqueue(new Chunk(_current, _count, _currentOwner));
_current = null;
_count = 0;
}