Changed the `CollectionBenchmark` class to use unsafe code for improved memory operations and added benchmarks for stack-allocated arrays. Changed the `ParallelNoiseBenchmark` class to remove the internal `NoiseJob` struct, promoting better organization. Changed the `AllocationManager` class to remove the lock mechanism for thread safety and simplified the `Reset` method. Changed the `Arena` and `DynamicArena` structs to include `Initialize` methods for better initialization control. Changed the `UnsafeArray<T>`, `UnsafeHashSet<T>`, and `UnsafeList<T>` structs to improve element access and management. Updated the `UnsafeCollectionExtensions` class to enhance usability with new methods for copying and converting collections. Updated the `MemoryLeakException` class to provide more detailed stack trace information for better debugging. Removed the usage of `UnsafeHashMap` in `Program.cs` and directly ran the `CollectionBenchmark`. Added a new `NoiseJob` struct in `NoiseJob.cs` for generating gradient noise using `UnsafeArray<float>`. Fixed minor typos and improved method signatures throughout the codebase for clarity.
66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
#if DEBUG
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
#endif
|
|
|
|
namespace Misaki.HighPerformance.Unsafe;
|
|
|
|
public readonly struct MemoryLeakExceptionInfo
|
|
{
|
|
public nuint Size
|
|
{
|
|
get; init;
|
|
}
|
|
#if DEBUG
|
|
public StackTrace StackTrace
|
|
{
|
|
get; init;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public class MemoryLeakException(params MemoryLeakExceptionInfo[] Infos) : Exception
|
|
{
|
|
#if DEBUG
|
|
private static string GetMessage(StackTrace? stackTrace)
|
|
{
|
|
if (stackTrace == null)
|
|
{
|
|
return "No stack trace available.";
|
|
}
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendLine("Memory leak detected at: ");
|
|
|
|
for (var i = 0; i < stackTrace.FrameCount; i++)
|
|
{
|
|
var frame = stackTrace.GetFrame(i);
|
|
if (frame != null)
|
|
{
|
|
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
|
|
}
|
|
}
|
|
|
|
return stringBuilder.ToString();
|
|
}
|
|
#endif
|
|
|
|
public override string Message
|
|
{
|
|
get
|
|
{
|
|
#if DEBUG
|
|
var stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendLine($"Found {Infos.Length} memory lakes!");
|
|
foreach (var info in Infos)
|
|
{
|
|
stringBuilder.AppendLine(GetMessage(info.StackTrace));
|
|
}
|
|
|
|
return stringBuilder.ToString();
|
|
#else
|
|
return $"There are still {Infos.Length} buffers that hold {Infos.Sum(i => (uint)i.Size)} bytes in total are not freed yet. Please free them before disposing. Switch to debug mode for more information.";
|
|
#endif
|
|
}
|
|
}
|
|
} |