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:
parent
9780fc6634
commit
3b75b96008
19 changed files with 625 additions and 189 deletions
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -7,10 +8,6 @@ public class SerializationChunkSourceTests
|
|||
{
|
||||
private class TestEntity : IGenericSerializable
|
||||
{
|
||||
public byte SerializedThread { get; set; }
|
||||
public int SerializedPosition { get; set; }
|
||||
public int SerializedLength { get; set; }
|
||||
|
||||
public int PayloadSize { get; init; } = 16;
|
||||
|
||||
public void Serialize(IGenericWriter writer)
|
||||
|
|
@ -22,24 +19,40 @@ public class SerializationChunkSourceTests
|
|||
}
|
||||
}
|
||||
|
||||
private class TestPersistence : GenericPersistence
|
||||
{
|
||||
public int PayloadSize { get; init; } = 16;
|
||||
|
||||
public TestPersistence() : base("ChunkSourceTest", 100)
|
||||
{
|
||||
}
|
||||
|
||||
public (byte Thread, int Position, int Length) Placement => (_selfThread, _selfPosition, _selfLength);
|
||||
|
||||
public override void Serialize(IGenericWriter writer)
|
||||
{
|
||||
for (var i = 0; i < PayloadSize; i++)
|
||||
{
|
||||
writer.Write((byte)(i & 0xFF));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Deserialize(IGenericReader reader)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private static List<TestEntity> Drain(SerializationChunkSource source)
|
||||
{
|
||||
var drained = new List<TestEntity>();
|
||||
while (source.TryTake(out var chunk))
|
||||
{
|
||||
if (chunk.Single != null)
|
||||
for (var i = 0; i < chunk.Count; i++)
|
||||
{
|
||||
drained.Add((TestEntity)chunk.Single);
|
||||
drained.Add((TestEntity)chunk.Buffer[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < chunk.Count; i++)
|
||||
{
|
||||
drained.Add((TestEntity)chunk.Buffer[i]);
|
||||
}
|
||||
|
||||
source.Return(chunk.Buffer, chunk.Count);
|
||||
}
|
||||
source.Return(chunk.Buffer, chunk.Count);
|
||||
}
|
||||
|
||||
return drained;
|
||||
|
|
@ -94,24 +107,67 @@ public class SerializationChunkSourceTests
|
|||
public void PushSingleDoesNotDisturbPartialChunk()
|
||||
{
|
||||
var source = new SerializationChunkSource();
|
||||
var heavy = new TestPersistence();
|
||||
|
||||
var small1 = new TestEntity();
|
||||
var heavy = new TestEntity { SerializedLength = 2 * 1024 * 1024 };
|
||||
var small2 = new TestEntity();
|
||||
try
|
||||
{
|
||||
var small1 = new TestEntity();
|
||||
var small2 = new TestEntity();
|
||||
|
||||
source.Push(small1);
|
||||
source.PushSingle(heavy); // published immediately as a single, ahead of the partial chunk
|
||||
source.Push(small2);
|
||||
source.Push(small1);
|
||||
source.PushSingle(heavy); // published immediately as a single, ahead of the partial chunk
|
||||
source.Push(small2);
|
||||
|
||||
Assert.True(source.TryTake(out var chunk));
|
||||
Assert.Same(heavy, chunk.Single);
|
||||
Assert.Equal(1, chunk.Count);
|
||||
Assert.True(source.TryTake(out var chunk));
|
||||
Assert.Same(heavy, chunk.Single);
|
||||
Assert.Equal(1, chunk.Count);
|
||||
|
||||
Assert.False(source.TryTake(out _));
|
||||
source.Flush();
|
||||
Assert.False(source.TryTake(out _));
|
||||
source.Flush();
|
||||
|
||||
var drained = Drain(source);
|
||||
Assert.Equal([small1, small2], drained);
|
||||
var drained = Drain(source);
|
||||
Assert.Equal([small1, small2], drained);
|
||||
}
|
||||
finally
|
||||
{
|
||||
heavy.Unregister();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetOwnerPublishesPartialChunkOnChange()
|
||||
{
|
||||
var source = new SerializationChunkSource();
|
||||
var ownerA = new TestPersistence();
|
||||
var ownerB = new TestPersistence();
|
||||
|
||||
try
|
||||
{
|
||||
source.SetOwner(ownerA);
|
||||
source.Push(new TestEntity());
|
||||
source.Push(new TestEntity());
|
||||
|
||||
// Same owner: partial chunk stays private to the producer.
|
||||
source.SetOwner(ownerA);
|
||||
Assert.False(source.TryTake(out _));
|
||||
|
||||
// Owner change publishes the partial chunk, keeping chunks persistence-homogeneous.
|
||||
source.SetOwner(ownerB);
|
||||
Assert.True(source.TryTake(out var chunk));
|
||||
Assert.Same(ownerA, chunk.Owner);
|
||||
Assert.Equal(2, chunk.Count);
|
||||
source.Return(chunk.Buffer, chunk.Count);
|
||||
|
||||
source.Push(new TestEntity());
|
||||
source.Flush();
|
||||
Assert.True(source.TryTake(out chunk));
|
||||
Assert.Same(ownerB, chunk.Owner);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ownerA.Unregister();
|
||||
ownerB.Unregister();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -138,9 +194,10 @@ public class SerializationChunkSourceTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void WorkersDrainAllEntitiesAndStampPositions()
|
||||
public void WorkersDrainAllEntitiesAndLogSegments()
|
||||
{
|
||||
var source = new SerializationChunkSource();
|
||||
var owner = new TestPersistence { PayloadSize = 512 * 1024 };
|
||||
var workers = new SerializationThreadWorker[2];
|
||||
for (var i = 0; i < workers.Length; i++)
|
||||
{
|
||||
|
|
@ -161,22 +218,17 @@ public class SerializationChunkSourceTests
|
|||
worker.Wake();
|
||||
}
|
||||
|
||||
// A large payload published as a dedicated single chunk (like persistence
|
||||
// self-payloads), interleaved with the bare entity stream.
|
||||
var heavy = new TestEntity { PayloadSize = 512 * 1024 };
|
||||
entities.Insert(5000, heavy);
|
||||
|
||||
// A large payload published as a dedicated single chunk (a persistence
|
||||
// self-payload), interleaved with the bare entity stream.
|
||||
source.SetOwner(owner);
|
||||
for (var i = 0; i < entities.Count; i++)
|
||||
{
|
||||
var e = entities[i];
|
||||
if (e == heavy)
|
||||
if (i == 5000)
|
||||
{
|
||||
source.PushSingle(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
source.Push(e);
|
||||
source.PushSingle(owner);
|
||||
}
|
||||
|
||||
source.Push(entities[i]);
|
||||
}
|
||||
|
||||
// Mirrors World.PauseSerializationThreads
|
||||
|
|
@ -194,26 +246,60 @@ public class SerializationChunkSourceTests
|
|||
totalBytes += worker.BytesSerialized;
|
||||
}
|
||||
|
||||
Assert.Equal(entities.Count, totalEntities);
|
||||
Assert.Equal(entities.Count + 1, totalEntities);
|
||||
|
||||
long expectedBytes = 0;
|
||||
// The self-payload recorded its placement on the persistence itself.
|
||||
var (selfThread, selfPosition, selfLength) = owner.Placement;
|
||||
Assert.Equal(owner.PayloadSize, selfLength);
|
||||
Assert.InRange(selfThread, (byte)0, (byte)(workers.Length - 1));
|
||||
var selfHeap = workers[selfThread].GetHeap(selfPosition, selfLength);
|
||||
Assert.Equal(0, selfHeap[0]);
|
||||
Assert.Equal((selfLength - 1) & 0xFF, selfHeap[^1]);
|
||||
|
||||
// Every entity appears exactly once in the worker segment logs, with a
|
||||
// consistent span on that worker's heap: positions are implicit (contiguous
|
||||
// writes), identity comes from the buffer-entities log.
|
||||
var seen = new HashSet<TestEntity>();
|
||||
long expectedBytes = owner.PayloadSize;
|
||||
foreach (var e in entities)
|
||||
{
|
||||
expectedBytes += e.PayloadSize;
|
||||
|
||||
// Every entity serialized exactly once with a consistent span on its worker's heap
|
||||
Assert.Equal(e.PayloadSize, e.SerializedLength);
|
||||
Assert.InRange(e.SerializedThread, (byte)0, (byte)(workers.Length - 1));
|
||||
|
||||
var heap = workers[e.SerializedThread].GetHeap(e.SerializedPosition, e.SerializedLength);
|
||||
Assert.Equal(0, heap[0]);
|
||||
Assert.Equal((e.SerializedLength - 1) & 0xFF, heap[^1]);
|
||||
}
|
||||
|
||||
foreach (var worker in workers)
|
||||
{
|
||||
var lengths = worker.Lengths;
|
||||
var bufferEntities = worker.BufferEntities;
|
||||
|
||||
foreach (var segment in worker.Segments)
|
||||
{
|
||||
Assert.Same(owner, segment.Owner);
|
||||
Assert.Equal(-1, segment.SlotOffset);
|
||||
|
||||
var heapPos = (int)segment.HeapStart;
|
||||
for (var i = 0; i < segment.RecordCount; i++)
|
||||
{
|
||||
var entity = (TestEntity)bufferEntities[segment.EntitiesStart + i];
|
||||
var length = lengths[segment.LengthsStart + i];
|
||||
|
||||
Assert.True(seen.Add(entity));
|
||||
Assert.Equal(entity.PayloadSize, length);
|
||||
|
||||
var heap = workers[Array.IndexOf(workers, worker)].GetHeap(heapPos, length);
|
||||
Assert.Equal(0, heap[0]);
|
||||
Assert.Equal((length - 1) & 0xFF, heap[^1]);
|
||||
|
||||
heapPos += length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(entities.Count, seen.Count);
|
||||
Assert.Equal(expectedBytes, totalBytes);
|
||||
}
|
||||
finally
|
||||
{
|
||||
owner.Unregister();
|
||||
foreach (var worker in workers)
|
||||
{
|
||||
worker.Exit();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue