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:
2025-04-03 09:13:07 +09:00
parent 060b4c9477
commit 48f2dce778
27 changed files with 1557 additions and 228 deletions

View File

@@ -0,0 +1,491 @@
using Misaki.HighPerformance.Mathematics;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Helpers;
public unsafe struct HashMapHelper<TKey> : IDisposable
where TKey : unmanaged, IEquatable<TKey>
{
internal unsafe struct Enumerator
{
public HashMapHelper<TKey>* buffer;
public int index;
public int bucketIndex;
public int nextIndex;
public unsafe Enumerator(HashMapHelper<TKey>* data)
{
buffer = data;
index = -1;
bucketIndex = 0;
nextIndex = -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
return buffer->MoveNext(ref bucketIndex, ref nextIndex, out index);
}
public void Reset()
{
index = -1;
bucketIndex = 0;
nextIndex = -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public KeyValuePair<TKey, TValue> GetCurrent<TValue>()
where TValue : unmanaged
{
return new KeyValuePair<TKey, TValue>(buffer->_keys[index], UnsafeUtilities.ReadArrayElement<TValue>(buffer->_buffer, index));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TKey GetCurrentKey()
{
if (index != -1)
{
return buffer->_keys[index];
}
return default;
}
public void Dispose()
{
}
}
// This buffer has 4 parts: TValue, TKey, Next, Buckets.
private byte* _buffer;
internal TKey* _keys;
internal int* _next;
internal int* _buckets;
private int _count;
private int _capacity;
private int _bucketCapacity;
private int _allocatedIndex;
private int _firstFreeIndex;
private readonly int _sizeOfTValue;
private readonly int _log2MinGrowth;
public const int MINIMAL_CAPACITY = 64;
public readonly byte* Buffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _buffer;
}
public readonly int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _count;
}
public readonly int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _capacity;
}
public readonly bool IsCreated
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _buffer != null;
}
public readonly bool IsEmpty
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => !IsCreated || _count == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int CalcCapacityCeilPow2(int capacity)
{
capacity = Math.Max(Math.Max(1, _count), capacity);
var newCapacity = Math.Max(capacity, 1 << _log2MinGrowth);
var result = MathUtilities.CeilPow2(newCapacity);
return result;
}
private static int CalculateDataSize(int capacity, int bucketCapacity, int sizeOfTValue, out int outKeyOffset, out int outNextOffset, out int outBucketOffset)
{
var sizeOfTKey = sizeof(TKey);
var sizeOfInt = sizeof(int);
var valuesSize = sizeOfTValue * capacity;
var keysSize = sizeOfTKey * capacity;
var nextSize = sizeOfInt * capacity;
var bucketSize = sizeOfInt * bucketCapacity;
var totalSize = valuesSize + keysSize + nextSize + bucketSize;
outKeyOffset = 0 + valuesSize;
outNextOffset = outKeyOffset + keysSize;
outBucketOffset = outNextOffset + nextSize;
return totalSize;
}
public HashMapHelper(int capacity, int sizeOfTValue, uint minGrowth)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
}
if (sizeOfTValue <= 0)
{
throw new ArgumentOutOfRangeException(nameof(sizeOfTValue), "Size of TValue must be greater than zero.");
}
_capacity = CalcCapacityCeilPow2(capacity);
_bucketCapacity = _capacity * 2;
var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue,
out var keyOffset, out var nextOffset, out var bucketOffset);
_buffer = (byte*)Malloc((nuint)totalSize);
_keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset);
Clear();
_sizeOfTValue = sizeOfTValue;
_log2MinGrowth = BitOperations.Log2(minGrowth);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(in TKey key)
{
return (int)((uint)key.GetHashCode() & _bucketCapacity - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly void CheckIndexOutOfBounds(int idx)
{
if ((uint)idx >= (uint)_capacity)
{
throw new InvalidOperationException($"Internal HashMap error. idx {idx}");
}
}
internal void ResizeExact(int newCapacity, int newBucketCapacity)
{
var totalSize = CalculateDataSize(newCapacity, newBucketCapacity, _sizeOfTValue,
out var keyOffset, out var nextOffset, out var bucketOffset);
var oldBuffer = _buffer;
var oldKeys = _keys;
var oldNext = _next;
var oldBuckets = _buckets;
var oldBucketCapacity = _bucketCapacity;
_buffer = (byte*)Malloc((nuint)totalSize);
_keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset);
_capacity = newCapacity;
_bucketCapacity = newBucketCapacity;
Clear();
for (int i = 0, num = oldBucketCapacity; i < num; ++i)
{
for (var idx = oldBuckets[i]; idx != -1; idx = oldNext[idx])
{
var newIdx = TryAdd(oldKeys[idx]);
MemCpy(_buffer + _sizeOfTValue * newIdx, oldBuffer + _sizeOfTValue * idx, (nuint)_sizeOfTValue);
}
}
Free(oldBuffer);
}
internal void Resize(int newCapacity)
{
newCapacity = Math.Max(newCapacity, _count);
var newBucketCapacity = MathUtilities.CeilPow2(newCapacity * 2);
if (_capacity == newCapacity && _bucketCapacity == newBucketCapacity)
{
return;
}
ResizeExact(newCapacity, newBucketCapacity);
}
public void TrimExcess()
{
var capacity = CalcCapacityCeilPow2(_count);
ResizeExact(capacity, capacity * 2);
}
public int Find(TKey key)
{
if (_allocatedIndex <= 0)
{
return -1;
}
// First find the slot based on the hash
var bucket = GetBucket(key);
var entryIdx = _buckets[bucket];
if ((uint)entryIdx < (uint)_capacity)
{
var nextPtrs = _next;
var test = UnsafeUtilities.ReadArrayElement<TKey>(_keys, entryIdx);
var test2 = UnsafeUtilities.ReadArrayElement<TKey>(_next, 0);
while (!UnsafeUtilities.ReadArrayElement<TKey>(_keys, entryIdx).Equals(key))
{
entryIdx = nextPtrs[entryIdx];
if ((uint)entryIdx >= (uint)_capacity)
{
return -1;
}
}
return entryIdx;
}
return -1;
}
public int TryAdd(in TKey key)
{
if (Find(key) != -1)
{
return -1;
}
// Allocate an entry from the free list
int idx;
int* next;
if (_allocatedIndex >= _capacity && _firstFreeIndex < 0)
{
var newCap = Math.Min(MINIMAL_CAPACITY, CalcCapacityCeilPow2(_capacity + (1 << _log2MinGrowth)));
Resize(newCap);
}
idx = _firstFreeIndex;
if (idx >= 0)
{
_firstFreeIndex = _next[idx];
}
else
{
idx = _allocatedIndex++;
}
CheckIndexOutOfBounds(idx);
UnsafeUtilities.WriteArrayElement(_keys, idx, key);
var bucket = GetBucket(key);
// Add the index to the hash-map
next = _next;
next[idx] = _buckets[bucket];
_buckets[bucket] = idx;
_count++;
return idx;
}
public int TryRemove(TKey key)
{
if (_capacity == 0)
{
return -1;
}
var removed = 0;
// First find the slot based on the hash
var bucket = GetBucket(key);
var prevEntry = -1;
var entryIdx = _buckets[bucket];
while (entryIdx >= 0 && entryIdx < _capacity)
{
if (UnsafeUtilities.ReadArrayElement<TKey>(_keys, entryIdx).Equals(key))
{
++removed;
// Found matching element, remove it
if (prevEntry < 0)
{
_buckets[bucket] = _next[entryIdx];
}
else
{
_next[prevEntry] = _next[entryIdx];
}
// And free the index
var nextIdx = _next[entryIdx];
_next[entryIdx] = _firstFreeIndex;
_firstFreeIndex = entryIdx;
entryIdx = nextIdx;
break;
}
else
{
prevEntry = entryIdx;
entryIdx = _next[entryIdx];
}
}
_count -= removed;
return 0 != removed ? removed : -1;
}
public bool TryGetValue<TValue>(TKey key, out TValue item)
where TValue : unmanaged
{
var idx = Find(key);
if (idx != -1)
{
item = UnsafeUtilities.ReadArrayElement<TValue>(_buffer, idx);
return true;
}
item = default;
return false;
}
public bool MoveNextSearch(ref int bucketIndex, ref int nextIndex, out int index)
{
for (int i = bucketIndex, num = _bucketCapacity; i < num; ++i)
{
var idx = _buckets[i];
if (idx != -1)
{
index = idx;
bucketIndex = i + 1;
nextIndex = _next[idx];
return true;
}
}
index = -1;
bucketIndex = _bucketCapacity;
nextIndex = -1;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext(ref int bucketIndex, ref int nextIndex, out int index)
{
if (nextIndex != -1)
{
index = nextIndex;
nextIndex = _next[nextIndex];
return true;
}
return MoveNextSearch(ref bucketIndex, ref nextIndex, out index);
}
internal UnsafeArray<TKey> GetKeyArray(Allocator allocator)
{
var result = new UnsafeArray<TKey>(_count, allocator, AllocationType.UnInitialized);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
{
var bucket = _buckets[i];
while (bucket != -1)
{
result[count++] = UnsafeUtilities.ReadArrayElement<TKey>(_keys, bucket);
bucket = _next[bucket];
}
}
return result;
}
internal UnsafeArray<TValue> GetValueArray<TValue>(Allocator allocator)
where TValue : unmanaged
{
var result = new UnsafeArray<TValue>(_count, allocator, AllocationType.UnInitialized);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; ++i)
{
var bucket = _buckets[i];
while (bucket != -1)
{
result[count++] = UnsafeUtilities.ReadArrayElement<TValue>(_buffer, bucket);
bucket = _next[bucket];
}
}
return result;
}
public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays<TValue>(Allocator allocator)
where TValue : unmanaged
{
var result = new UnsafeArray<KeyValuePair<TKey, TValue>>(_count, allocator, AllocationType.UnInitialized);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
{
var bucket = _buckets[i];
while (bucket != -1)
{
result[count] = new(UnsafeUtilities.ReadArrayElement<TKey>(_keys, bucket),
UnsafeUtilities.ReadArrayElement<TValue>(_buffer, bucket));
count++;
bucket = _next[bucket];
}
}
return result;
}
public void Clear()
{
MemSet(_buckets, 0xff, (nuint)_bucketCapacity * sizeof(int));
MemSet(_next, 0xff, (nuint)_capacity * sizeof(int));
_count = 0;
_firstFreeIndex = -1;
_allocatedIndex = 0;
}
public void Dispose()
{
if (IsCreated)
{
Free(_buffer);
_buffer = null;
_keys = null;
_next = null;
_buckets = null;
_count = 0;
_capacity = 0;
_bucketCapacity = 0;
}
}
}

View File

@@ -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);
}

View File

@@ -2,6 +2,10 @@
namespace Misaki.HighPerformance.Unsafe.Helpers;
/// <summary>
/// Provides extension methods for copying elements between unsafe collections and spans, converting collections to
/// arrays or lists, and searching for values.
/// </summary>
public unsafe static class UnsafeCollectionExtensions
{
/// <summary>
@@ -13,14 +17,37 @@ public unsafe static class UnsafeCollectionExtensions
/// <exception cref="ArgumentException">Thrown when the sizes of the source collection and destination span do not match.</exception>
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination) where T : unmanaged
{
if (source.Size > destination.Length)
if (source.Count > destination.Length)
{
throw new ArgumentException("Source collection is larger than the destination span.");
}
fixed (T* ptr = destination)
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
SystemUnsfae.CopyBlock(ptr, source.GetUnsafePtr(), (uint)(source.Count * sizeof(T)));
}
}
/// <summary>
/// Copies a range of elements from a source collection to a destination span, ensuring both are adequately sized.
/// </summary>
/// <typeparam name="T">Specifies the type of elements being copied, which must be a value type.</typeparam>
/// <param name="source">The collection from which elements are copied.</param>
/// <param name="destination">The span where the elements will be copied to.</param>
/// <param name="sourceIndex">The starting index in the source collection for the copy operation.</param>
/// <param name="destinationIndex">The starting index in the destination span where the elements will be placed.</param>
/// <param name="length">The number of elements to copy from the source to the destination.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source collection or destination span.</exception>
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination, int sourceIndex, int destinationIndex, int length) where T : unmanaged
{
if (sourceIndex + length > source.Count || destinationIndex + length > destination.Length)
{
throw new ArgumentException("Source collection or destination span is too small for the specified range.");
}
fixed (T* ptr = destination)
{
SystemUnsfae.CopyBlock(ptr + destinationIndex, (byte*)source.GetUnsafePtr() + (sourceIndex * sizeof(T)), (uint)(length * sizeof(T)));
}
}
@@ -33,14 +60,37 @@ public unsafe static class UnsafeCollectionExtensions
/// <exception cref="ArgumentException">Thrown when the source span and destination collection have different sizes.</exception>
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source) where T : unmanaged
{
if (destination.Size > source.Length)
if (destination.Count > source.Length)
{
throw new ArgumentException("Destination collection is larger than the source span.");
}
fixed (T* ptr = source)
{
SystemUnsfae.CopyBlock(destination.Buffer, ptr, (uint)(source.Length * sizeof(T)));
SystemUnsfae.CopyBlock(destination.GetUnsafePtr(), ptr, (uint)(source.Length * sizeof(T)));
}
}
/// <summary>
/// Copies a specified range of elements from a source span to a destination collection.
/// </summary>
/// <typeparam name="T">Represents the type of elements being copied, which must be unmanaged.</typeparam>
/// <param name="destination">The collection where elements will be copied to.</param>
/// <param name="source">The span containing the elements to be copied.</param>
/// <param name="sourceIndex">The starting index in the source span from which to begin copying.</param>
/// <param name="destinationIndex">The starting index in the destination collection where the elements will be placed.</param>
/// <param name="length">The number of elements to copy from the source span to the destination collection.</param>
/// <exception cref="ArgumentException">Thrown when the specified range exceeds the bounds of the source span or destination collection.</exception>
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source, int sourceIndex, int destinationIndex, int length) where T : unmanaged
{
if (sourceIndex + length > source.Length || destinationIndex + length > destination.Count)
{
throw new ArgumentException("Source span or destination collection is too small for the specified range.");
}
fixed (T* ptr = source)
{
SystemUnsfae.CopyBlock((byte*)destination.GetUnsafePtr() + (destinationIndex * sizeof(T)), ptr + sourceIndex, (uint)(length * sizeof(T)));
}
}
@@ -52,10 +102,10 @@ public unsafe static class UnsafeCollectionExtensions
/// <returns>A new collection containing the elements from the UnsafeCollection.</returns>
public static T[] ToArray<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
var array = new T[source.Size];
var array = new T[source.Count];
fixed (T* ptr = array)
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
SystemUnsfae.CopyBlock(ptr, source.GetUnsafePtr(), (uint)(source.Count * sizeof(T)));
}
return array;
@@ -69,10 +119,10 @@ public unsafe static class UnsafeCollectionExtensions
/// <returns>A list containing the elements from the specified unmanaged collection.</returns>
public static List<T> ToList<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
var list = new List<T>(source.Size);
var list = new List<T>(source.Count);
fixed (T* ptr = list.ToArray())
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
SystemUnsfae.CopyBlock(ptr, source.GetUnsafePtr(), (uint)(source.Count * sizeof(T)));
}
return list;
}
@@ -85,6 +135,39 @@ public unsafe static class UnsafeCollectionExtensions
/// <returns>A Span that provides a view over the elements of the UnsafeCollection.</returns>
public static Span<T> AsSpan<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
return new(source.Buffer, source.Size);
return new(source.GetUnsafePtr(), source.Count);
}
/// <summary>
/// Finds the index of a specified value in a collection. Returns -1 if the value is not found.
/// </summary>
/// <typeparam name="T">The type of elements in the collection, which must support equality comparison.</typeparam>
/// <param name="source">The collection to search for the specified value.</param>
/// <param name="value">The value to locate within the collection.</param>
/// <param name="index">Outputs the index of the found value or -1 if not found.</param>
public static void IndexOf<T>(this IUnsafeCollection<T> source, T value, out int index) where T : unmanaged, IEquatable<T>
{
for (var i = 0; i < source.Count; i++)
{
if (UnsafeUtilities.ReadArrayElement<T>(source.GetUnsafePtr(), i).Equals(value))
{
index = i;
return;
}
}
index = -1;
}
/// <summary>
/// Checks if a specified value exists within an unsafe collection of unmanaged types.
/// </summary>
/// <typeparam name="T">Represents a type that is unmanaged and supports equality comparison.</typeparam>
/// <param name="source">The collection being searched for the specified value.</param>
/// <param name="value">The value being searched for within the collection.</param>
/// <returns>Returns true if the value is found; otherwise, returns false.</returns>
public static bool Conations<T>(this IUnsafeCollection<T> source, T value) where T : unmanaged, IEquatable<T>
{
source.IndexOf(value, out var index);
return index != -1;
}
}

View File

@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Helpers;
@@ -79,4 +80,18 @@ public static unsafe class UnsafeUtilities
{
*ReadArrayElementUnsafe<T>(ptr, index) = value;
}
/// <summary>
/// Converts an UnsafeArray of one unmanaged type to another unmanaged type without copying the elements.
/// </summary>
/// <typeparam name="TIn">Represents the type of elements in the input array.</typeparam>
/// <typeparam name="TOut">Represents the type of elements in the output array.</typeparam>
/// <param name="array">The input array containing elements of the specified input type.</param>
/// <returns>An UnsafeArray containing elements of the specified output type.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UnsafeArray<TOut> CastArray<TIn, TOut>(UnsafeArray<TIn> array)
where TIn : unmanaged where TOut : unmanaged
{
return new UnsafeArray<TOut>(array.GetUnsafePtr(), array.Count * sizeof(TIn) / sizeof(TOut));
}
}