ModernUO/Projects/Server.Tests/Tests/Serialization/ShadowDictionaryEntriesTests.cs
Kamron Batman 3b75b96008
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>
2026-07-16 22:38:47 -07:00

140 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using Xunit;
namespace Server.Tests;
public class ShadowDictionaryEntriesTests
{
private class TestEntity : ISerializable
{
public TestEntity(Serial serial) => Serial = serial;
public Serial Serial { get; }
public DateTime Created { get; set; }
public bool Deleted => false;
public void Delete()
{
}
public void Serialize(IGenericWriter writer)
{
writer.Write(Serial);
writer.Write(0xC0FFEE);
}
public void Deserialize(IGenericReader reader)
{
}
}
private static TestEntity GetSlotValue(Array entries, int slot) =>
System.Runtime.CompilerServices.Unsafe.As<ShadowEntry<TestEntity>[]>(entries)[slot].Value;
[Fact]
public void RuntimeLayoutIsSupported()
{
// If this fails on a runtime upgrade, saves still work via the fallback path,
// but the parallel iteration fast path is silently lost — this test makes it loud.
Assert.True(ShadowDictionaryEntries.Supported);
}
[Fact]
public void SerializeRangeCoversExactlyTheLiveEntities()
{
var persistence = new GenericEntityPersistence<TestEntity>("ShadowTest", 1000, 1, 0x7FFFFFFF);
try
{
var rng = new System.Random(0xBEEF);
var dict = persistence.EntitiesBySerial;
// Heavy churn: adds, removes, and re-adds to exercise freelist reuse and resizes,
// leaving free slots scattered through the entries array.
var serials = new List<Serial>();
for (var i = 0; i < 50_000; i++)
{
var serial = (Serial)(uint)rng.Next(1, int.MaxValue);
if (dict.TryAdd(serial, new TestEntity(serial)))
{
serials.Add(serial);
}
if (i % 4 == 3)
{
var index = rng.Next(serials.Count);
dict.Remove(serials[index]);
serials.RemoveAt(index);
}
}
Assert.True(persistence.TrySnapshotEntries(out var slotCount));
Assert.True(slotCount >= dict.Count);
var source = (ISlotRangeSource)persistence;
var writer = new BufferWriter(new byte[dict.Count * 16], true);
var lengths = new List<int>();
// Serialize in worker-sized slices, like the drain does.
var serialized = 0;
for (var offset = 0; offset < slotCount; offset += 4096)
{
serialized += source.SerializeRange(writer, lengths, offset, Math.Min(4096, slotCount - offset));
}
Assert.Equal(dict.Count, serialized);
Assert.Equal(dict.Count, lengths.Count);
// Re-walk the same slots in the same order, pairing each occupied slot with the
// next logged length — exactly how the snapshot writer locates each record.
var entriesField = ShadowDictionaryEntries.GetEntriesField<TestEntity>();
var entries = (Array)entriesField.GetValue(dict);
var position = 0;
var lengthIndex = 0;
var matched = 0;
for (var slot = 0; slot < entries.Length; slot++)
{
// Occupancy is exactly the non-null values, same as production.
var entity = GetSlotValue(entries, slot);
if (entity == null)
{
continue;
}
var length = lengths[lengthIndex++];
Assert.Equal(8, length); // serial + int
var span = writer.Buffer.AsSpan(position, length);
Assert.Equal(entity.Serial, (Serial)System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(span));
position += length;
matched++;
}
Assert.Equal(dict.Count, matched);
}
finally
{
persistence.Unregister();
}
}
[Fact]
public void SnapshotFailsGracefullyOnEmptyDictionary()
{
var persistence = new GenericEntityPersistence<TestEntity>("ShadowTestEmpty", 1001, 1, 0x7FFFFFFF);
try
{
Assert.False(persistence.TrySnapshotEntries(out var slotCount));
Assert.Equal(0, slotCount);
}
finally
{
persistence.Unregister();
}
}
}