Skip to content

Commit

Permalink
Adds configuration for multithreaded saves
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Sep 14, 2024
1 parent 44c9bba commit db36201
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 2 additions & 6 deletions Projects/Server/Serialization/SerializationThreadWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Server;

public class SerializationThreadWorker
{
private const int MinHeapSize = 1024 * 1024; // 1MB
private readonly int _index;
private readonly Thread _thread;

Check warning on line 27 in Projects/Server/Serialization/SerializationThreadWorker.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Private field can be converted into local variable

The field is always assigned before being used and can be converted into a local variable
private readonly AutoResetEvent _startEvent; // Main thread tells the thread to start working
Expand Down Expand Up @@ -60,12 +61,7 @@ public void Exit()
Sleep();
}

public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray<byte>(1024 * 1024); // 1MB

public void DeallocateHeap()
{
_heap = null;
}
public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray<byte>(MinHeapSize); // 1MB

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Push(IGenericSerializable entity) => _entities.Enqueue(entity);
Expand Down
8 changes: 7 additions & 1 deletion Projects/Server/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class World
private static readonly GenericEntityPersistence<BaseGuild> _guildPersistence = new("Guilds", 3, 1, 0x7FFFFFFF);

private static int _threadId;
internal static readonly SerializationThreadWorker[] _threadWorkers = new SerializationThreadWorker[Math.Max(Environment.ProcessorCount - 1, 1)];
internal static SerializationThreadWorker[] _threadWorkers;
private static readonly ManualResetEvent _diskWriteHandle = new(true);
private static readonly ConcurrentQueue<Item> _decayQueue = new();

Expand Down Expand Up @@ -88,6 +88,7 @@ public static Serial NewVirtual
public static Dictionary<Serial, Mobile> Mobiles => _mobilePersistence.EntitiesBySerial;
public static Dictionary<Serial, BaseGuild> Guilds => _guildPersistence.EntitiesBySerial;

public static bool UseMultiThreadedSaves { get; private set; }
public static string SavePath { get; private set; }
public static WorldState WorldState { get; private set; }
public static bool Saving => WorldState == WorldState.Saving;
Expand All @@ -101,6 +102,8 @@ public static void Configure()

var savePath = ServerConfiguration.GetOrUpdateSetting("world.savePath", "Saves");
SavePath = PathUtility.GetFullPath(savePath);

UseMultiThreadedSaves = ServerConfiguration.GetOrUpdateSetting("world.useMultithreadedSaves", true);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -204,6 +207,9 @@ public static void Load()
);

// Create the serialization threads.
var threadCount = UseMultiThreadedSaves ? Math.Max(Environment.ProcessorCount - 1, 1) : 1;
_threadWorkers = new SerializationThreadWorker[threadCount];

for (var i = 0; i < _threadWorkers.Length; i++)
{
_threadWorkers[i] = new SerializationThreadWorker(i);
Expand Down

0 comments on commit db36201

Please sign in to comment.