feat: Upgrades serialization v4 (Threaded Heap Serialization) (#1947)

### Summary

- `GenericEntityPersistence` is now a type of `GenericPersistence`. This allows developers to serialize both entities and non-entities in the same system. 🎉
- Each `SerializationThreadWorker` now allocates 1MB of heap for serialization _permanently_. If more memory is needed, that thread will double it's memory, not to exceed increments of 64MB.
- Several bugs with serialization introduced with the pure MMF implementation have been fixed.
- `BinaryFileReader` has been added back. 🎉
- Adds `world.useMultithreadedSaves` to allow disabling threaded saves.

> [!IMPORTANT]
> **Developer Note**
> The split file serialization has been deprecated and is no longer used. We have effectively gone back to the same file writing we had before the pure MMF implementation.
This commit is contained in:
Kamron Batman 2024-09-14 09:57:43 -07:00 committed by GitHub
parent aaac0c596c
commit 465d3c8187
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 564 additions and 373 deletions

View file

@ -14,7 +14,6 @@
*************************************************************************/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
@ -53,7 +52,7 @@ public abstract class Persistence
}
}
private unsafe static Dictionary<ulong, string> LoadTypes(string path)
private static unsafe Dictionary<ulong, string> LoadTypes(string path)
{
var db = new Dictionary<ulong, string>();
@ -85,32 +84,31 @@ public abstract class Persistence
}
// Note: This is strictly on a background thread
internal static void PreSerializeAll(string path, ConcurrentQueue<Type> types)
internal static void WriteSnapshotAll(string path, HashSet<Type> typeSet)
{
foreach (var p in _registry)
{
p.Preserialize(path, types);
p.WriteSnapshot(path, typeSet);
}
WriteSerializedTypesSnapshot(path, typeSet);
}
private static readonly HashSet<Type> _typesSet = [];
// Note: This is strictly on a background thread
internal static void WriteSnapshotAll(string path, ConcurrentQueue<Type> types)
public static void WriteSerializedTypesSnapshot(string path, HashSet<Type> types)
{
foreach (var p in _registry)
{
p.WriteSnapshot();
}
string typesPath = Path.Combine(path, "SerializedTypes.db");
using var fs = new FileStream(typesPath, FileMode.Create);
using var writer = new MemoryMapFileWriter(fs, 1024 * 1024 * 4);
writer.Write(0); // version
writer.Write(types.Count);
// Dedupe the queue.
foreach (var type in types)
{
_typesSet.Add(type);
var fullName = type.FullName;
writer.Write(HashUtility.ComputeHash64(fullName));
writer.WriteStringRaw(fullName);
}
WriteSerializedTypesSnapshot(path, _typesSet);
_typesSet.Clear();
}
internal static void SerializeAll()
@ -137,32 +135,8 @@ public abstract class Persistence
}
}
public static void WriteSerializedTypesSnapshot(string path, HashSet<Type> types)
{
string typesPath = Path.Combine(path, "SerializedTypes.db");
using var fs = new FileStream(typesPath, FileMode.Create);
using var writer = new MemoryMapFileWriter(fs, 1024 * 1024 * 4);
writer.Write(0); // version
writer.Write(types.Count);
foreach (var type in types)
{
var fullName = type.FullName;
writer.Write(HashUtility.ComputeHash64(fullName));
writer.WriteStringRaw(fullName);
}
}
// Open file streams, MMFs, prepare data structures
// Note: This should only be run on a background thread
public abstract void Preserialize(string savePath, ConcurrentQueue<Type> types);
// Note: This should only be run on a background thread
public abstract void Serialize(IGenericSerializable e, int threadIndex);
// Note: This should only be run on a background thread
public abstract void WriteSnapshot();
public abstract void WriteSnapshot(string savePath, HashSet<Type> typeSet);
public abstract void Serialize();