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:
@@ -13,6 +13,76 @@ public static unsafe class MemoryUtilities
|
||||
public T data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a block of memory of the specified size in bytes.
|
||||
/// </summary>
|
||||
/// <param name="size">Specifies the number of bytes to allocate in memory.</param>
|
||||
/// <returns>Returns a pointer to the allocated memory block.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* Malloc(nuint size)
|
||||
{
|
||||
return NativeMemory.Alloc(size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a block of memory with a specified size and alignment.
|
||||
/// </summary>
|
||||
/// <param name="size">Specifies the total number of bytes to allocate for the memory block.</param>
|
||||
/// <param name="alignment">Defines the required alignment for the allocated memory address.</param>
|
||||
/// <returns>Returns a pointer to the allocated memory block.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* AlignedAlloc(nuint size, nuint alignment)
|
||||
{
|
||||
return NativeMemory.AlignedAlloc(size, alignment);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes a previously allocated memory block to a new size. It returns a pointer to the reallocated memory.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The pointer to the memory block that needs to be resized.</param>
|
||||
/// <param name="size">The new size for the memory block after resizing.</param>
|
||||
/// <returns>A pointer to the reallocated memory block, or null if the operation fails.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* Realloc(void* ptr, nuint size)
|
||||
{
|
||||
return NativeMemory.Realloc(ptr, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reallocates memory to a specified size with a given alignment. It returns a pointer to the newly allocated
|
||||
/// memory.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The pointer to the existing memory block that needs to be reallocated.</param>
|
||||
/// <param name="size">The new size for the memory allocation.</param>
|
||||
/// <param name="alignment">The required alignment for the new memory allocation.</param>
|
||||
/// <returns>A pointer to the reallocated memory block, or null if the allocation fails.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* AlignedRealloc(void* ptr, nuint size, nuint alignment)
|
||||
{
|
||||
return NativeMemory.AlignedRealloc(ptr, size, alignment);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases the allocated memory pointed to by the given pointer. This helps in managing memory usage effectively.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The pointer to the memory block that needs to be freed.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Free(void* ptr)
|
||||
{
|
||||
NativeMemory.Free(ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases memory that was allocated with alignment requirements. It ensures proper deallocation of aligned memory
|
||||
/// blocks.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The pointer to the memory block that needs to be freed.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void AlignedFree(void* ptr)
|
||||
{
|
||||
NativeMemory.AlignedFree(ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears a block of memory by setting it to zero. It initializes a specified number of bytes at a given memory
|
||||
/// address.
|
||||
@@ -32,7 +102,7 @@ public static unsafe class MemoryUtilities
|
||||
/// <param name="size">The number of bytes to set to the specified value.</param>
|
||||
/// <param name="value">The byte value to which the memory block will be initialized.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void MemSet(void* ptr, nuint size, byte value)
|
||||
public static void MemSet(void* ptr, byte value, nuint size)
|
||||
{
|
||||
NativeMemory.Fill(ptr, size, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user