Skip to content

Commit

Permalink
Fixes race condition and limits growth
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Sep 12, 2024
1 parent f9528c1 commit e777829
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
8 changes: 1 addition & 7 deletions Projects/Server/Serialization/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ public void Resize(int size)
_buffer = newBuffer;
}

public virtual void Flush()
{
// Need to avoid buffer.Length = 2, buffer * 2 is 4, but we need 8 or 16bytes, causing an exception.
// The least we need is 16bytes + Index, but we use BufferSize since it should always be big enough for a single
// non-dynamic field.
Resize(Math.Clamp(_buffer.Length * 2, BufferSize, 1024 * 1024 * 64));
}
public virtual void Flush() => Resize(Math.Clamp(_buffer.Length * 2, BufferSize, _buffer.Length + 1024 * 1024 * 64));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FlushIfNeeded(int amount)
Expand Down
10 changes: 1 addition & 9 deletions Projects/Server/Serialization/SerializationThreadWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ public void Exit()
Sleep();
}

// 6GB heap, divide by number of threads from World.GetThreadWorkerCount()
private const ulong totalMemory = 1024 * 1024;
// private static readonly ulong _memoryPerThread = totalMemory / (ulong)World.GetThreadWorkerCount();

// private static ulong NextBlockSize(ulong amount) => (amount + 4096UL - 1) & ~(4096UL - 1);

// public void AllocateHeap() => _heap ??= GC.AllocateUninitializedArray<byte>((int)NextBlockSize(_memoryPerThread));

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

public void DeallocateHeap()
{
Expand Down
9 changes: 2 additions & 7 deletions Projects/Server/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ public static void Configure()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WaitForWriteCompletion()
{
_diskWriteHandle.WaitOne();
}
public static void WaitForWriteCompletion() => _diskWriteHandle.WaitOne();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnqueueForDecay(Item item)
Expand Down Expand Up @@ -303,7 +300,6 @@ internal static void Snapshot(string snapshotPath)
}

WaitForWriteCompletion(); // Blocks Save until current disk flush is done.

_diskWriteHandle.Reset();

NetState.FlushAll();
Expand Down Expand Up @@ -396,14 +392,13 @@ private static void WriteFiles(object state)
// Clear types
SerializedTypes.Clear();

_diskWriteHandle.Set();
Core.LoopContext.Post(FinishWorldSave);
}

private static void FinishWorldSave()
{
WorldState = WorldState.Running;
_diskWriteHandle.Set();

Persistence.PostWorldSaveAll(); // Process decay and safety queues
}

Expand Down

0 comments on commit e777829

Please sign in to comment.