perf(saves): workers iterate entity dictionaries directly; main thread joins the drain

Removes the per-entity handoff from the freeze entirely. GenericEntityPersistence
publishes 4096-slot ranges over its dictionary's backing entries array, and workers
serialize occupied slots (value != null) directly via a ShadowEntry<TValue> struct
that mirrors the runtime's private Dictionary Entry layout. Safe because the
dictionary is frozen during Saving (mutations divert to the pending safety queues).

The layout is proven at startup before any code reads through it: validation
measures the true Entry stride via precise allocation accounting (so all shadow
reads are guaranteed in-bounds), then compares every key and value of a churned,
resized, freelist-exercised dictionary reading value slots as raw pointer bits
only - never materializing a managed reference until the layout is proven. If a
future runtime changes Dictionary internals, validation fails and saves fall back
to the enumerate-and-push path with a logged warning.

The main thread now joins the drain through an inline (threadless) worker after
publishing work, instead of idling while the thread workers finish - worth a full
worker share on the freeze and proportionally more on low-core hosts.

Measured through the real pipeline classes (24 cores, 10M entities, 1.7GB, dense
2-byte write profile): publish cost drops from ~55ms to ~0.1ms, the freeze is now
bound by pure serialize throughput at ~99ms steady state (vs ~740ms before this
branch, ~7.5x), and the first save after boot drops from ~468ms to ~139ms because
fine-grained ranges self-balance without needing size estimates.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kamron Batman 2026-07-13 22:51:14 -07:00
parent 230c39851f
commit 9acd701aaa
No known key found for this signature in database
GPG key ID: 7D81DF26D9A5D94A
6 changed files with 509 additions and 34 deletions

View file

@ -36,16 +36,33 @@ public class SerializationThreadWorker
private long _bytesSerialized;
public SerializationThreadWorker(int index, SerializationChunkSource chunkSource, int heapSizeHint = 0)
: this(index, chunkSource, heapSizeHint, inline: false)
{
}
private SerializationThreadWorker(int index, SerializationChunkSource chunkSource, int heapSizeHint, bool inline)
{
_index = index;
_chunkSource = chunkSource;
_heapSizeHint = heapSizeHint;
_startEvent = new AutoResetEvent(false);
_stopEvent = new AutoResetEvent(false);
_thread = new Thread(Execute);
_thread.Start(this);
if (!inline)
{
_startEvent = new AutoResetEvent(false);
_stopEvent = new AutoResetEvent(false);
_thread = new Thread(Execute);
_thread.Start(this);
}
}
/// <summary>
/// Creates a worker with no thread of its own. The owner drains chunks inline via
/// <see cref="DrainInline"/> — used by the main thread to join the drain instead of
/// idling while the thread workers finish.
/// </summary>
public static SerializationThreadWorker CreateInline(int index, SerializationChunkSource chunkSource, int heapSizeHint = 0) =>
new(index, chunkSource, heapSizeHint, inline: true);
// Stats from the most recent save, for diagnosing load balance.
public long EntitiesSerialized => _entitiesSerialized;
public long BytesSerialized => _bytesSerialized;
@ -82,7 +99,7 @@ public class SerializationThreadWorker
public ReadOnlySpan<byte> GetHeap(int start, int length) => _heap.AsSpan(start, length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Serialize(IGenericSerializable e, BufferWriter writer, byte threadIndex)
internal static void Serialize(IGenericSerializable e, BufferWriter writer, byte threadIndex)
{
e.SerializedThread = threadIndex;
var start = e.SerializedPosition = (int)writer.Position;
@ -90,6 +107,56 @@ public class SerializationThreadWorker
e.SerializedLength = (int)(writer.Position - start);
}
private static long ProcessChunk(
in SerializationChunkSource.Chunk chunk, SerializationChunkSource chunkSource,
BufferWriter writer, byte threadIndex
)
{
if (chunk.Single != null)
{
Serialize(chunk.Single, writer, threadIndex);
return 1;
}
if (chunk.Source != null)
{
return chunk.Source.SerializeRange(writer, threadIndex, chunk.Offset, chunk.Count);
}
var buffer = chunk.Buffer;
var count = chunk.Count;
for (var i = 0; i < count; i++)
{
Serialize(buffer[i], writer, threadIndex);
}
chunkSource.Return(buffer, count);
return count;
}
/// <summary>
/// Drains chunks on the calling thread until the queue is empty, then returns.
/// Only valid on inline workers; the main thread calls this after publishing all work
/// so it contributes drain throughput instead of idling.
/// </summary>
public void DrainInline()
{
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);
}
_heap = writer.Buffer;
_entitiesSerialized = entities;
_bytesSerialized = writer.Position;
writer.Close();
}
private static void Execute(object obj)
{
var worker = (SerializationThreadWorker)obj;
@ -110,24 +177,7 @@ public class SerializationThreadWorker
if (chunkSource.TryTake(out var chunk))
{
spinner.Reset();
if (chunk.Single != null)
{
Serialize(chunk.Single, writer, threadIndex);
entities++;
}
else
{
var buffer = chunk.Buffer;
var count = chunk.Count;
for (var i = 0; i < count; i++)
{
Serialize(buffer[i], writer, threadIndex);
}
entities += count;
chunkSource.Return(buffer, count);
}
entities += ProcessChunk(in chunk, chunkSource, writer, threadIndex);
}
else if (pauseRequested) // Break when finished
{