Replaces the per-entity round-robin ConcurrentQueue handoff between the game loop and the serialization workers with pooled 4096-entity chunks published to a single shared queue. The producer's per-entity cost drops from a synchronized enqueue to a plain array store, and workers pulling whole chunks load-balance dynamically: a worker busy with a thick entity simply takes fewer chunks. Scheduling changes so indivisible multi-megabyte payloads no longer extend the freeze tail: - Persistence.SerializeAll pushes systems largest-first (LPT), using the previous save's SerializedLength (or the loaded file size on first save). - Persistence self-payloads and entities whose previous size exceeds 1MB are published as dedicated single-entity chunks so they spread across workers instead of riding inside one shared chunk. - Entity SerializedLength is stamped from the index at load so estimates exist on the first save after boot. Worker heaps are pre-sized from the loaded save's .bin sizes (25% headroom) so the first save doesn't pay copy-on-grow inside the freeze, and the drain loop uses SpinWait backoff (never Sleep(1)) instead of hammering the queue head while the producer works. Snapshot writing uses a 1MB FileStream buffer, and per-worker entity/byte counts are logged at Debug for balance diagnostics. Measured on a 24-core machine with a synthetic 10M-entity, 1.7GB world (64/64/32MB system payloads, 24x 2MB thick entities) through the real pipeline classes: freeze window 740ms -> 122-160ms steady state (~5-6x), first save 490ms -> 330ms, steady-state allocations converge to zero, and worker byte loads converge (previous max/min spread ~2x -> ~1.15x for the small-entity stream). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2026 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: GenericPersistence.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.MemoryMappedFiles;
|
|
|
|
namespace Server;
|
|
|
|
public abstract class GenericPersistence : Persistence, IGenericSerializable
|
|
{
|
|
public string Name { get; }
|
|
public string SaveFilePath { get; protected set; } // "<Folder>/<System>.bin"
|
|
|
|
public byte SerializedThread { get; set; }
|
|
public int SerializedPosition { get; set; }
|
|
public int SerializedLength { get; set; }
|
|
|
|
private long _loadedFileLength;
|
|
|
|
// Scheduling estimate only: previous save's payload size, or the loaded file size before the first save.
|
|
internal long EstimatedSize => SerializedLength > 0 ? SerializedLength : _loadedFileLength;
|
|
|
|
public GenericPersistence(string name, int priority) : base(priority)
|
|
{
|
|
Name = name;
|
|
SaveFilePath = Path.Combine(Name, $"{Name}.bin");
|
|
}
|
|
|
|
public override void Serialize()
|
|
{
|
|
// Always a dedicated chunk: self-payloads can be arbitrarily large and must not
|
|
// ride inside a shared chunk where one worker would serialize them plus the chunk.
|
|
World.PushSingleToCache(this);
|
|
}
|
|
|
|
public override void WriteSnapshot(string savePath, HashSet<Type> typeSet)
|
|
{
|
|
if (SerializedLength == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var file = Path.Combine(savePath, SaveFilePath);
|
|
var dir = Path.GetDirectoryName(file);
|
|
PathUtility.EnsureDirectory(dir);
|
|
|
|
var threads = World._threadWorkers;
|
|
|
|
using var binFs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
|
|
var thread = SerializedThread;
|
|
var heapStart = SerializedPosition;
|
|
var heapLength = SerializedLength;
|
|
|
|
binFs.Write(threads[thread].GetHeap(heapStart, heapLength));
|
|
}
|
|
|
|
public override unsafe void Deserialize(string savePath, Dictionary<ulong, string> typesDb)
|
|
{
|
|
// Assume savePath has the Core.BaseDirectory already prepended
|
|
var dataPath = Path.GetFullPath(SaveFilePath, savePath);
|
|
var file = new FileInfo(dataPath);
|
|
|
|
if (!file.Exists || file.Length <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var fileLength = file.Length;
|
|
_loadedFileLength = fileLength;
|
|
|
|
string error;
|
|
|
|
try
|
|
{
|
|
using var mmf = MemoryMappedFile.CreateFromFile(dataPath, FileMode.Open);
|
|
using var accessor = mmf.CreateViewStream();
|
|
|
|
byte* ptr = null;
|
|
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
|
|
var dataReader = new UnmanagedDataReader(ptr, accessor.Length, typesDb);
|
|
Deserialize(dataReader);
|
|
|
|
error = dataReader.Position != fileLength
|
|
? $"Serialized {fileLength} bytes, but {dataReader.Position} bytes deserialized"
|
|
: null;
|
|
|
|
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
error = e.ToString();
|
|
}
|
|
|
|
if (error != null)
|
|
{
|
|
Console.WriteLine($"***** Bad deserialize of {file.FullName} *****");
|
|
Console.WriteLine(error);
|
|
|
|
Console.Write("Skip this file and continue? (y/n): ");
|
|
var y = ConsoleInputHandler.ReadLine();
|
|
|
|
if (!y.InsensitiveEquals("y"))
|
|
{
|
|
throw new Exception("Deserialization failed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract void Serialize(IGenericWriter writer);
|
|
public abstract void Deserialize(IGenericReader reader);
|
|
}
|