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.
97 lines
2.0 KiB
C#
97 lines
2.0 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Misaki.HighPerformance.Buffer
|
|
{
|
|
public class ObjectPool<T> : IDisposable where T : class
|
|
{
|
|
private readonly Func<T> _factory;
|
|
private readonly ConcurrentQueue<T> _objects = new();
|
|
|
|
private bool _disposed;
|
|
|
|
public uint InitialSize
|
|
{
|
|
get;
|
|
}
|
|
|
|
public uint MaxSize
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public ObjectPool(Func<T> factory, uint initialSize = uint.MinValue, uint maxSize = uint.MaxValue)
|
|
{
|
|
_factory = factory;
|
|
|
|
InitialSize = initialSize;
|
|
MaxSize = maxSize;
|
|
|
|
if (initialSize != uint.MinValue)
|
|
{
|
|
for (var i = 0; i < initialSize; i++)
|
|
{
|
|
_objects.Enqueue(_factory());
|
|
}
|
|
}
|
|
}
|
|
|
|
~ObjectPool()
|
|
{
|
|
Dispose();
|
|
}
|
|
|
|
public bool TryRent([MaybeNullWhen(false)] out T obj)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
|
|
if (!_objects.IsEmpty)
|
|
{
|
|
return _objects.TryDequeue(out obj);
|
|
}
|
|
|
|
obj = null;
|
|
return false;
|
|
}
|
|
|
|
public void Return(T obj)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
|
|
if (_objects.Count < MaxSize)
|
|
{
|
|
_objects.Enqueue(obj);
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
foreach (var obj in _objects)
|
|
{
|
|
if (obj is IDisposable disposable)
|
|
{
|
|
disposable.Dispose();
|
|
}
|
|
}
|
|
|
|
_objects.Clear();
|
|
GC.Collect();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Reset();
|
|
|
|
_disposed = true;
|
|
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
}
|