Enhance memory management and performance benchmarks
Added a new configuration setting in `.editorconfig` to sort system directives last and increased the maximum line length to 400 characters. Added a new static class `MathUtilities` in `MathUtilities.cs` with a method `CeilPow2` for computing powers of two. Added a new benchmark class `CollectionBenchmark` in `CollectionBenchmark.cs` to measure performance of standard versus unsafe arrays. Added a new benchmark class `HashCodeBenchmark` in `HashCodeBenchmark.cs` to evaluate hash code generation performance. Added new utility methods in `UnsafeUtilities.cs` for memory allocation and deallocation, including `Malloc`, `AlignedAlloc`, `Realloc`, and `Free`. Added a new `AllocationType` enum in `AllocationType.cs` to specify memory allocation types. Changed the project file `Misaki.HighPerformance.Mathematics.csproj` to target .NET 9.0 and enable implicit usings and nullable reference types. Changed the `ParallelNoiseBenchmark` class in `ParallelNoiseBenchmark.cs` to improve memory allocation strategies and performance. Changed memory management in `Arena.cs` and `DynamicArena.cs` to use custom `Malloc` and `Free` functions. Changed the `IUnsafeCollection` interface in `IUnsafeCollection.cs` to include new methods for resizing collections and obtaining unsafe pointers. Changed the `UnsafeArray.cs` to improve management of unsafe arrays, including constructor and method updates. Changed the `UnsafeHashMap` and `UnsafeHashSet` classes to enhance performance and memory management. Changed the `UnsafeCollectionExtensions` class to provide additional methods for copying elements and converting collections. Changed the `ObjectPool` class in `ObjectPool.cs` to simplify cleanup and remove auto-cleanup functionality. Changed job scheduling and worker classes in `JobExtensions.cs` and `JobWorker.cs` to improve job scheduling in a thread pool. Removed commented-out code in `Program.cs` related to previous testing methods. Removed auto-cleanup functionality from the `ObjectPool` class.
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
/// </summary>
|
||||
public unsafe struct Arena : IDisposable
|
||||
{
|
||||
private void* _buffer;
|
||||
private byte* _buffer;
|
||||
private uint _size;
|
||||
private uint _offset;
|
||||
|
||||
@@ -16,7 +16,7 @@ public unsafe struct Arena : IDisposable
|
||||
|
||||
public Arena(uint size)
|
||||
{
|
||||
_buffer = NativeMemory.Alloc(size);
|
||||
_buffer = (byte*)Malloc(size);
|
||||
_size = size;
|
||||
_offset = 0;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public unsafe struct Arena : IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
NativeMemory.Free(_buffer);
|
||||
Free(_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
@@ -27,18 +26,17 @@ public unsafe struct DynamicArena : IDisposable
|
||||
public DynamicArena(uint initialSize)
|
||||
{
|
||||
_initialSize = initialSize;
|
||||
_root = (ArenaNode*)NativeMemory.Alloc(SizeOf<ArenaNode>());
|
||||
_root = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
_root->arena = new Arena(initialSize);
|
||||
_root->next = null;
|
||||
_current = _root;
|
||||
_disposed = false;
|
||||
}
|
||||
|
||||
private bool CreateNewNode(uint size)
|
||||
{
|
||||
try
|
||||
{
|
||||
var newNode = (ArenaNode*)NativeMemory.Alloc(SizeOf<ArenaNode>());
|
||||
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
newNode->arena = new Arena(size);
|
||||
newNode->next = null;
|
||||
|
||||
@@ -70,10 +68,14 @@ public unsafe struct DynamicArena : IDisposable
|
||||
{
|
||||
result = current->arena.Allocate(size, alignSize, allocationType);
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (current->next == null && !CreateNewNode(Math.Max(size, _initialSize)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
@@ -107,14 +109,16 @@ public unsafe struct DynamicArena : IDisposable
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var current = _root;
|
||||
while (current != null)
|
||||
{
|
||||
var next = current->next;
|
||||
current->arena.Dispose();
|
||||
NativeMemory.Free(current);
|
||||
Free(current);
|
||||
current = next;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user