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:
parent
230c39851f
commit
9acd701aaa
6 changed files with 509 additions and 34 deletions
|
|
@ -20,6 +20,21 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace Server;
|
||||
|
||||
/// <summary>
|
||||
/// A range of backing-store slots that a serialization worker can serialize directly,
|
||||
/// letting workers iterate a persistence's storage in parallel instead of the main thread
|
||||
/// enumerating and handing off every entity. Implemented by
|
||||
/// <see cref="GenericEntityPersistence{T}"/> over its dictionary's entries array.
|
||||
/// </summary>
|
||||
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.
|
||||
/// </summary>
|
||||
int SerializeRange(BufferWriter writer, byte threadIndex, int offset, int count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Single-producer/multi-consumer handoff between the game loop and the serialization
|
||||
/// thread workers during a world save. The producer batches entities into pooled chunks
|
||||
|
|
@ -29,6 +44,8 @@ namespace Server;
|
|||
/// Entities whose previous serialized size exceeds <see cref="HeavyEntityThreshold"/> are
|
||||
/// published as dedicated single-entity chunks so multi-megabyte payloads spread across
|
||||
/// workers instead of riding inside one chunk.
|
||||
/// Persistences that support direct parallel iteration publish slot ranges instead of
|
||||
/// filled chunks, removing the per-entity handoff from the freeze entirely.
|
||||
/// </summary>
|
||||
public sealed class SerializationChunkSource
|
||||
{
|
||||
|
|
@ -47,21 +64,28 @@ public sealed class SerializationChunkSource
|
|||
{
|
||||
public readonly IGenericSerializable Single;
|
||||
public readonly IGenericSerializable[] Buffer;
|
||||
public readonly ISlotRangeSource Source;
|
||||
public readonly int Offset;
|
||||
public readonly int Count;
|
||||
|
||||
public Chunk(IGenericSerializable single)
|
||||
{
|
||||
Single = single;
|
||||
Buffer = null;
|
||||
Count = 1;
|
||||
}
|
||||
|
||||
public Chunk(IGenericSerializable[] buffer, int count)
|
||||
{
|
||||
Single = null;
|
||||
Buffer = buffer;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
public Chunk(ISlotRangeSource source, int offset, int count)
|
||||
{
|
||||
Source = source;
|
||||
Offset = offset;
|
||||
Count = count;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly ConcurrentQueue<Chunk> _chunks = new();
|
||||
|
|
@ -96,6 +120,19 @@ public sealed class SerializationChunkSource
|
|||
/// </summary>
|
||||
public void PushSingle(IGenericSerializable entity) => _chunks.Enqueue(new Chunk(entity));
|
||||
|
||||
/// <summary>
|
||||
/// Publishes slot ranges covering [0, slotCount) of a directly-iterable persistence.
|
||||
/// Workers claim ranges like any other chunk, so the per-entity handoff cost disappears
|
||||
/// and load balancing is unchanged.
|
||||
/// </summary>
|
||||
public void PushSlotRanges(ISlotRangeSource source, int slotCount)
|
||||
{
|
||||
for (var offset = 0; offset < slotCount; offset += ChunkCapacity)
|
||||
{
|
||||
_chunks.Enqueue(new Chunk(source, offset, Math.Min(ChunkCapacity, slotCount - offset)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes the partial chunk, if any. Must be called on the producer thread before
|
||||
/// the workers are told to finish draining, or the tail of the stream is not serialized.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue