Improve memory safety and alignment handling
Some checks failed
Publish NuGet Packages / publish (pull_request) Failing after 1m5s

- Updated `.gitignore` to include `.vscode/` and clarified comments.
- Introduced `SafeHandle` for managing memory alignment and safe access.
- Refactored `UnsafeArray<T>` to add bounds checking and alignment logic.
- Added `IUnsafeHashCollection<T>` for specialized hash-based collections.
- Refactored `UnsafeHashMap<TKey, TValue>` and `UnsafeHashSet<T>` to use `HashMapHelper<TKey>` with alignment support.
- Made `UnsafeSlotMap<T>` methods `readonly` for immutability.
- Enhanced `HashMapHelper<TKey>` with alignment-aware buffer management and validation.
- Updated benchmarks to use `UnsafeArray<Vector256<int>>` and added capacity checks.
- Incremented assembly version to `1.1.3` in `Misaki.HighPerformance.LowLevel.csproj`.
- Updated `Program.cs` to run `CollectionBenchmark` and demonstrate safe disposal handling.
This commit is contained in:
2025-11-18 01:25:40 +09:00
parent 57725369f9
commit c0a0861897
13 changed files with 309 additions and 140 deletions

1
.gitignore vendored
View File

@@ -35,6 +35,7 @@ bld/
# Visual Studio 2015/2017 cache/options directory # Visual Studio 2015/2017 cache/options directory
.vs/ .vs/
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot # Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/ #wwwroot/

View File

View File

@@ -0,0 +1,31 @@
namespace Misaki.HighPerformance.LowLevel.Buffer;
public unsafe struct SafeHandle
{
private const nuint _ALIGNMENT = 16u;
public int valid;
public static nuint GetAlignWithHeader(nuint baseAlign)
{
return Math.Max(_ALIGNMENT, baseAlign);
}
public static nuint GetPaddedHeaderSize(nuint baseAlign)
{
var headerBaseSize = (nuint)sizeof(SafeHandle);
var dataAlignment = Math.Max(_ALIGNMENT, baseAlign);
return (headerBaseSize + (dataAlignment - 1u)) & ~(dataAlignment - 1u);
}
public static SafeHandle* GetSafeHandle(void* ptr, nuint baseAlign)
{
if (ptr == null)
{
return null;
}
var alignedHeaderSize = GetPaddedHeaderSize(baseAlign);
return (SafeHandle*)((byte*)ptr - alignedHeaderSize);
}
}

View File

@@ -24,7 +24,7 @@ public unsafe interface IUnsafeCollection : IDisposable
void* GetUnsafePtr(); void* GetUnsafePtr();
} }
public unsafe interface IUnsafeCollection<T> : IUnsafeCollection, IEnumerable<T> public interface IUnsafeCollection<T> : IUnsafeCollection, IEnumerable<T>
where T : unmanaged where T : unmanaged
{ {
/// <summary> /// <summary>
@@ -44,7 +44,40 @@ public unsafe interface IUnsafeCollection<T> : IUnsafeCollection, IEnumerable<T>
void Resize(int newSize, AllocationOption option); void Resize(int newSize, AllocationOption option);
} }
public unsafe interface IUnTypedCollection : IUnsafeCollection public interface IUnsafeHashCollection<T> : IEnumerable<T>, IDisposable
where T : unmanaged
{
/// <summary>
/// Indicates whether the object has been created. Returns true if the object is created, otherwise false.
/// </summary>
bool IsCreated
{
get;
}
/// <summary>
/// Gets the number of elements in a collection. The value is read-only.
/// </summary>
int Count
{
get;
}
/// <summary>
/// Removes all elements from the collection. The collection will be empty after this operation.
/// </summary>
void Clear();
/// <summary>
/// Changes the size of a collection to the specified value.
/// </summary>
/// <remarks>This is to adjust the element count of the collection, not the size of the underlying buffer in memory.</remarks>
/// <param name="newSize">Specifies the new size to which the collection should be adjusted.</param>
/// <param name="option">Specifies allocation options that may affect how memory is managed during the resize operation.</param>
void Resize(int newSize, AllocationOption option);
}
public interface IUnTypedCollection : IUnsafeCollection
{ {
/// <summary> /// <summary>
/// The total size of the buffer in bytes. /// The total size of the buffer in bytes.

View File

@@ -3,6 +3,7 @@ using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts; using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities; using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections; using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Collections; namespace Misaki.HighPerformance.LowLevel.Collections;
@@ -57,13 +58,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get get
{ {
#if ENABLE_COLLECTION_CHECKS CheckIndexBounds(index);
if (index < 0 || index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
}
#endif
return ref UnsafeUtility.ReadArrayElementRef<T>(_buffer, index); return ref UnsafeUtility.ReadArrayElementRef<T>(_buffer, index);
} }
} }
@@ -73,21 +68,18 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get get
{ {
#if ENABLE_COLLECTION_CHECKS CheckIndexBounds((int)index);
if (index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
}
#endif
return ref UnsafeUtility.ReadArrayElementRef<T>(_buffer, index); return ref UnsafeUtility.ReadArrayElementRef<T>(_buffer, index);
} }
} }
public readonly bool IsCreated public readonly bool IsCreated
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] get
get => _buffer != null; {
var handle = SafeHandle.GetSafeHandle(_buffer, AlignOf<T>());
return handle != null && Volatile.Read(ref handle->valid) == 1;
}
} }
public Enumerator GetEnumerator() => new((UnsafeArray<T>*)UnsafeUtility.AddressOf(ref this)); public Enumerator GetEnumerator() => new((UnsafeArray<T>*)UnsafeUtility.AddressOf(ref this));
@@ -111,13 +103,20 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnsafeArray(int count, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None) public UnsafeArray(int count, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{ {
if (count <= 0) if (count < 0)
{ {
throw new ArgumentOutOfRangeException(nameof(count), "Count must be greater than zero."); throw new ArgumentOutOfRangeException(nameof(count), "Count can not be less than zero.");
} }
var tAlign = AlignOf<T>();
var headerSize = SafeHandle.GetPaddedHeaderSize(tAlign);
var sizeWithHeader = (nuint)(count * sizeof(T)) + headerSize;
var alignment = SafeHandle.GetAlignWithHeader(tAlign);
var buff = handle.Alloc(handle.Allocator, sizeWithHeader, alignment, allocationOption);
_buffer = (T*)((byte*)buff + headerSize);
_handle = (AllocationHandle*)Unsafe.AsPointer(ref handle); _handle = (AllocationHandle*)Unsafe.AsPointer(ref handle);
_buffer = (T*)handle.Alloc(_handle->Allocator, (uint)count * (uint)sizeof(T), (uint)AlignOf<T>(), allocationOption);
_count = count; _count = count;
} }
@@ -149,7 +148,32 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_count = count; _count = count;
} }
public ReadOnlyUnsafeCollection<T> AsReadOnly() [MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
{
throw new InvalidOperationException("The UnsafeArray is not created.");
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_COLLECTION_CHECKS")]
private readonly void CheckIndexBounds(int index)
{
ThrowIfNotCreated();
if (index >= _count)
{
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
}
}
/// <summary>
/// Returns a read-only view of the current collection.
/// </summary>
/// <returns>A <see cref="ReadOnlyUnsafeCollection{T}"/> that provides a read-only view of the elements in the current collection.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlyUnsafeCollection<T> AsReadOnly()
{ {
return new ReadOnlyUnsafeCollection<T>(_buffer, _count); return new ReadOnlyUnsafeCollection<T>(_buffer, _count);
} }
@@ -157,6 +181,8 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <inheritdoc/> /// <inheritdoc/>
public void Resize(int newSize, AllocationOption option = AllocationOption.None) public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{ {
ThrowIfNotCreated();
if (newSize == Count) if (newSize == Count)
{ {
return; return;
@@ -171,6 +197,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear() public readonly void Clear()
{ {
ThrowIfNotCreated();
MemClear(_buffer, (nuint)(Count * sizeof(T))); MemClear(_buffer, (nuint)(Count * sizeof(T)));
} }
@@ -178,6 +205,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr() public readonly void* GetUnsafePtr()
{ {
ThrowIfNotCreated();
return _buffer; return _buffer;
} }
@@ -190,6 +218,8 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
public readonly UnsafeArray<U> Reinterpret<U>() public readonly UnsafeArray<U> Reinterpret<U>()
where U : unmanaged where U : unmanaged
{ {
ThrowIfNotCreated();
var totalSize = (nuint)(Count * sizeof(T)); var totalSize = (nuint)(Count * sizeof(T));
if (totalSize % (nuint)sizeof(U) != 0) if (totalSize % (nuint)sizeof(U) != 0)
{ {

View File

@@ -7,7 +7,7 @@ using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Collections; namespace Misaki.HighPerformance.LowLevel.Collections;
public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePair<TKey, TValue>> public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValuePair<TKey, TValue>>
where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged
{ {
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
@@ -32,11 +32,11 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
} }
} }
private HashMapHelper<TKey> _hashMap; private HashMapHelper<TKey> _helper;
public readonly int Count => _hashMap.Count; public readonly int Count => _helper.Count;
public readonly int Capacity => _hashMap.Capacity; public readonly int Capacity => _helper.Capacity;
public readonly bool IsCreated => _hashMap.IsCreated; public readonly bool IsCreated => _helper.IsCreated;
/// <summary> /// <summary>
/// Gets and sets values by key. /// Gets and sets values by key.
@@ -49,7 +49,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
{ {
get get
{ {
if (!_hashMap.TryGetValue<TValue>(key, out var result)) if (!_helper.TryGetValue<TValue>(key, out var result))
{ {
throw new ArgumentException($"Key: {key} is not present."); throw new ArgumentException($"Key: {key} is not present.");
} }
@@ -59,10 +59,10 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
set set
{ {
var idx = _hashMap.Find(key); var idx = _helper.Find(key);
if (-1 != idx) if (-1 != idx)
{ {
UnsafeUtility.WriteArrayElement(_hashMap.Buffer, idx, value); UnsafeUtility.WriteArrayElement(_helper.Buffer, idx, value);
return; return;
} }
@@ -84,7 +84,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
public UnsafeHashMap(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None) public UnsafeHashMap(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{ {
_hashMap = new HashMapHelper<TKey>(capacity, sizeof(TValue), HashMapHelper<TKey>.MINIMAL_CAPACITY, ref handle, allocationOption); _helper = new HashMapHelper<TKey>(capacity, sizeof(TValue), (int)AlignOf<TValue>(), HashMapHelper<TKey>.MINIMAL_CAPACITY, ref handle, allocationOption);
} }
public UnsafeHashMap(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None) public UnsafeHashMap(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
@@ -101,10 +101,10 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
/// <returns>True if the key-value pair was added.</returns> /// <returns>True if the key-value pair was added.</returns>
public bool TryAdd(TKey key, TValue item) public bool TryAdd(TKey key, TValue item)
{ {
var idx = _hashMap.TryAdd(key); var idx = _helper.TryAdd(key);
if (idx != -1) if (idx != -1)
{ {
UnsafeUtility.WriteArrayElement(_hashMap.Buffer, idx, item); UnsafeUtility.WriteArrayElement(_helper.Buffer, idx, item);
return true; return true;
} }
@@ -134,7 +134,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
/// <returns>True if the value was present.</returns> /// <returns>True if the value was present.</returns>
public bool Remove(TKey key) public bool Remove(TKey key)
{ {
return -1 != _hashMap.TryRemove(key); return -1 != _helper.TryRemove(key);
} }
/// <summary> /// <summary>
@@ -145,7 +145,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
/// <returns>True if the key was present.</returns> /// <returns>True if the key was present.</returns>
public bool TryGetValue(TKey key, out TValue item) public bool TryGetValue(TKey key, out TValue item)
{ {
return _hashMap.TryGetValue(key, out item); return _helper.TryGetValue(key, out item);
} }
/// <summary> /// <summary>
@@ -155,51 +155,51 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
/// <returns>True if the key was present.</returns> /// <returns>True if the key was present.</returns>
public bool ContainsKey(TKey key) public bool ContainsKey(TKey key)
{ {
return -1 != _hashMap.Find(key); return -1 != _helper.Find(key);
} }
/// <summary> /// <summary>
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries. /// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
/// </summary> /// </summary>
public void TrimExcess() => _hashMap.TrimExcess(); public void TrimExcess() => _helper.TrimExcess();
public void Resize(int newSize, AllocationOption option = AllocationOption.None) public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{ {
_hashMap.Resize(newSize); _helper.Resize(newSize);
} }
public void Clear() public void Clear()
{ {
_hashMap.Clear(); _helper.Clear();
} }
/// <summary> /// <summary>
/// Retrieves an array of keys from the hash map. /// Retrieves an array of keys from the hash map.
/// </summary> /// </summary>
/// <returns>An array containing the keys stored in the hash map.</returns> /// <returns>An array containing the keys stored in the hash map.</returns>
public UnsafeArray<TKey> GetKeyArray(Allocator allocator) => _hashMap.GetKeyArray(allocator); public UnsafeArray<TKey> GetKeyArray(Allocator allocator) => _helper.GetKeyArray(allocator);
/// <summary> /// <summary>
/// Retrieves an array of values from the underlying hash map. /// Retrieves an array of values from the underlying hash map.
/// </summary> /// </summary>
/// <returns>An UnsafeArray containing the values stored in the hash map.</returns> /// <returns>An UnsafeArray containing the values stored in the hash map.</returns>
public UnsafeArray<TValue> GetValueArray(Allocator allocator) => _hashMap.GetValueArray<TValue>(allocator); public UnsafeArray<TValue> GetValueArray(Allocator allocator) => _helper.GetValueArray<TValue>(allocator);
/// <summary> /// <summary>
/// Retrieves an array of key-value pairs from the hash map. The keys are of type TKey and the values are of type /// Retrieves an array of key-value pairs from the hash map. The keys are of type TKey and the values are of type
/// TValue. /// TValue.
/// </summary> /// </summary>
/// <returns>Returns an UnsafeArray containing KeyValuePair objects.</returns> /// <returns>Returns an UnsafeArray containing KeyValuePair objects.</returns>
public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays(Allocator allocator) => _hashMap.GetKeyValueArrays<TValue>(allocator); public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays(Allocator allocator) => _helper.GetKeyValueArrays<TValue>(allocator);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr() public readonly void* GetUnsafePtr()
{ {
return _hashMap.Buffer; return _helper.Buffer;
} }
public void Dispose() public void Dispose()
{ {
_hashMap.Dispose(); _helper.Dispose();
} }
} }

View File

@@ -12,7 +12,7 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
/// removing, and checking for values. /// removing, and checking for values.
/// </summary> /// </summary>
/// <typeparam name="T">Represents an unmanaged type that can be compared for equality.</typeparam> /// <typeparam name="T">Represents an unmanaged type that can be compared for equality.</typeparam>
public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T> public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
where T : unmanaged, IEquatable<T> where T : unmanaged, IEquatable<T>
{ {
public struct Enumerator : IEnumerator<T> public struct Enumerator : IEnumerator<T>
@@ -37,11 +37,11 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
} }
} }
private HashMapHelper<T> _hashMap; private HashMapHelper<T> _helper;
public readonly int Count => _hashMap.Count; public readonly int Count => _helper.Count;
public readonly int Capacity => _hashMap.Capacity; public readonly int Capacity => _helper.Capacity;
public readonly bool IsCreated => _hashMap.IsCreated; public readonly bool IsCreated => _helper.IsCreated;
public Enumerator GetEnumerator() => new((HashMapHelper<T>*)UnsafeUtility.AddressOf(ref this)); public Enumerator GetEnumerator() => new((HashMapHelper<T>*)UnsafeUtility.AddressOf(ref this));
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
@@ -57,7 +57,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
public UnsafeHashSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None) public UnsafeHashSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{ {
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, ref handle, allocationOption); _helper = new HashMapHelper<T>(capacity, 0, 0, HashMapHelper<T>.MINIMAL_CAPACITY, ref handle, allocationOption);
} }
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None) public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
@@ -72,7 +72,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
/// <returns>True if the value was not already present.</returns> /// <returns>True if the value was not already present.</returns>
public bool Add(T item) public bool Add(T item)
{ {
return -1 != _hashMap.TryAdd(item); return -1 != _helper.TryAdd(item);
} }
/// <summary> /// <summary>
@@ -82,7 +82,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
/// <returns>True if the value was present.</returns> /// <returns>True if the value was present.</returns>
public bool Remove(T item) public bool Remove(T item)
{ {
return -1 != _hashMap.TryRemove(item); return -1 != _helper.TryRemove(item);
} }
/// <summary> /// <summary>
@@ -92,13 +92,13 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
/// <returns>True if the value was present.</returns> /// <returns>True if the value was present.</returns>
public bool Contains(T item) public bool Contains(T item)
{ {
return -1 != _hashMap.Find(item); return -1 != _helper.Find(item);
} }
/// <summary> /// <summary>
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries. /// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
/// </summary> /// </summary>
public void TrimExcess() => _hashMap.TrimExcess(); public void TrimExcess() => _helper.TrimExcess();
/// <summary> /// <summary>
/// Returns an array with a copy of this set's values (in no particular order). /// Returns an array with a copy of this set's values (in no particular order).
@@ -107,27 +107,27 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
/// <returns>An array with a copy of the set's values.</returns> /// <returns>An array with a copy of the set's values.</returns>
public UnsafeArray<T> ToNativeArray(Allocator allocator) public UnsafeArray<T> ToNativeArray(Allocator allocator)
{ {
return _hashMap.GetKeyArray(allocator); return _helper.GetKeyArray(allocator);
} }
public void Resize(int newSize, AllocationOption option = AllocationOption.None) public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{ {
_hashMap.Resize(newSize); _helper.Resize(newSize);
} }
public void Clear() public void Clear()
{ {
_hashMap.Clear(); _helper.Clear();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr() public readonly void* GetUnsafePtr()
{ {
return _hashMap.Buffer; return _helper.Buffer;
} }
public void Dispose() public void Dispose()
{ {
_hashMap.Dispose(); _helper.Dispose();
} }
} }

View File

@@ -206,7 +206,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="value">When this method returns, contains the element at the specified slot and generation if found; otherwise, the /// <param name="value">When this method returns, contains the element at the specified slot and generation if found; otherwise, the
/// default value for type <typeparamref name="T"/>.</param> /// default value for type <typeparamref name="T"/>.</param>
/// <returns>true if the element at the specified slot index and generation is found; otherwise, false.</returns> /// <returns>true if the element at the specified slot index and generation is found; otherwise, false.</returns>
public bool TryGetElementAt(int slotIndex, int generation, out T value) public readonly bool TryGetElementAt(int slotIndex, int generation, out T value)
{ {
if (slotIndex < 0 || slotIndex >= _capacity) if (slotIndex < 0 || slotIndex >= _capacity)
{ {
@@ -232,7 +232,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>The element of type <see cref="T"/> stored at the specified slot and generation.</returns> /// <returns>The element of type <see cref="T"/> stored at the specified slot and generation.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="slotIndex"/> is less than zero or greater than or equal to the capacity.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="slotIndex"/> is less than zero or greater than or equal to the capacity.</exception>
/// <exception cref="InvalidOperationException">Thrown when the specified slot is not occupied or the generation does not match.</exception> /// <exception cref="InvalidOperationException">Thrown when the specified slot is not occupied or the generation does not match.</exception>
public T GetElementAt(int slotIndex, int generation) public readonly T GetElementAt(int slotIndex, int generation)
{ {
if (slotIndex < 0 || slotIndex >= _capacity) if (slotIndex < 0 || slotIndex >= _capacity)
{ {

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.1.2</AssemblyVersion> <AssemblyVersion>1.1.3</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>

View File

@@ -16,7 +16,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public int bucketIndex; public int bucketIndex;
public int nextIndex; public int nextIndex;
public unsafe Enumerator(HashMapHelper<TKey>* data) public Enumerator(HashMapHelper<TKey>* data)
{ {
buffer = data; buffer = data;
index = -1; index = -1;
@@ -73,6 +73,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
private int _allocatedIndex; private int _allocatedIndex;
private int _firstFreeIndex; private int _firstFreeIndex;
private readonly int _alignment;
private readonly int _sizeOfTValue; private readonly int _sizeOfTValue;
private readonly int _log2MinGrowth; private readonly int _log2MinGrowth;
@@ -86,55 +87,90 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public readonly int Capacity => _capacity; public readonly int Capacity => _capacity;
public readonly bool IsCreated => _buffer != null;
public readonly bool IsEmpty => !IsCreated || _count == 0; public readonly bool IsEmpty => !IsCreated || _count == 0;
private static int CalculateDataSize(int capacity, int bucketCapacity, int sizeOfTValue, out int outKeyOffset, out int outNextOffset, out int outBucketOffset) public readonly bool IsCreated
{ {
var sizeOfTKey = sizeof(TKey); get
var sizeOfInt = sizeof(int); {
var handle = SafeHandle.GetSafeHandle(_buffer, (nuint)_alignment);
return handle != null && Volatile.Read(ref handle->valid) == 1;
}
}
var valuesSize = sizeOfTValue * capacity; private static int CalculateDataSize(int capacity, int bucketCapacity, int sizeOfTValue, int alignment, out int outValueOffset, out int outKeyOffset, out int outNextOffset, out int outBucketOffset)
var keysSize = sizeOfTKey * capacity; {
var nextSize = sizeOfInt * capacity; static int AlignUp(int size, int align)
var bucketSize = sizeOfInt * bucketCapacity; {
var totalSize = valuesSize + keysSize + nextSize + bucketSize; return (size + (align - 1)) & ~(align - 1);
}
outKeyOffset = 0 + valuesSize; var headerSize = SafeHandle.GetPaddedHeaderSize((nuint)alignment);
outNextOffset = outKeyOffset + keysSize;
outBucketOffset = outNextOffset + nextSize; var valuesSize = (sizeOfTValue * capacity);
var valuesSizePadded = AlignUp(valuesSize, alignment);
var keysSize = (sizeof(TKey) * capacity);
var keysSizePadded = AlignUp(keysSize, alignment);
var nextSize = (sizeof(int) * capacity);
var nextSizePadded = AlignUp(nextSize, alignment);
// Buckets are the last item, doesn't need padding after it
var bucketSize = (sizeof(int) * bucketCapacity);
outValueOffset = (int)headerSize;
outKeyOffset = outValueOffset + valuesSizePadded;
outNextOffset = outKeyOffset + keysSizePadded;
outBucketOffset = outNextOffset + nextSizePadded;
// Total size is header + all buffers
var totalSize = (int)headerSize + outKeyOffset + keysSizePadded + nextSizePadded + bucketSize;
return totalSize; return totalSize;
} }
public HashMapHelper(int capacity, int sizeOfTValue, uint minGrowth, ref AllocationHandle handle, AllocationOption allocationOption) public HashMapHelper(int capacity, int sizeOfTValue, int alignOfTValue, uint minGrowth, ref AllocationHandle handle, AllocationOption allocationOption)
{ {
if (capacity <= 0) if (capacity <= 0)
{ {
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero."); throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
} }
if (sizeOfTValue <= 0) if (sizeOfTValue < 0 || alignOfTValue < 0)
{ {
throw new ArgumentOutOfRangeException(nameof(sizeOfTValue), "Size of TValue must be greater than zero."); throw new ArgumentOutOfRangeException(nameof(sizeOfTValue), "Size or alignment of TValue can not be less than zero.");
} }
_capacity = CalcCapacityCeilPow2(capacity); _capacity = CalcCapacityCeilPow2(capacity);
_bucketCapacity = _capacity * 2; _bucketCapacity = _capacity * 2;
var alignOfKey = (int)AlignOf<TKey>();
var alignOfInt = (int)AlignOf<int>();
var maxDataAlign = Math.Max(Math.Max(alignOfTValue, alignOfKey), alignOfInt);
_alignment = (int)SafeHandle.GetAlignWithHeader((nuint)maxDataAlign);
_sizeOfTValue = sizeOfTValue; _sizeOfTValue = sizeOfTValue;
_log2MinGrowth = BitOperations.Log2(minGrowth); _log2MinGrowth = BitOperations.Log2(minGrowth);
_handle = (AllocationHandle*)Unsafe.AsPointer(ref handle); _handle = (AllocationHandle*)Unsafe.AsPointer(ref handle);
var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue, var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue, _alignment,
out var keyOffset, out var nextOffset, out var bucketOffset); out var valueOffset, out var keyOffset, out var nextOffset, out var bucketOffset);
AllocateBuffer(totalSize, keyOffset, nextOffset, bucketOffset, allocationOption); AllocateBuffer(totalSize, valueOffset, keyOffset, nextOffset, bucketOffset, _alignment, allocationOption);
Clear(); Clear();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
{
throw new InvalidOperationException("The HashMapHelper is not created.");
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CeilPow2(int x) private static int CeilPow2(int x)
{ {
@@ -174,20 +210,20 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AllocateBuffer(int totalSize, int keyOffset, int nextOffset, int bucketOffset, AllocationOption allocationOption) private void AllocateBuffer(int totalSize, int valueOffset, int keyOffset, int nextOffset, int bucketOffset, int alignment, AllocationOption allocationOption)
{ {
var alignSize = sizeof(TKey) > sizeof(int) ? AlignOf<TKey>() : AlignOf<int>(); var buf = (byte*)_handle->Alloc(_handle->Allocator, (uint)totalSize, (nuint)alignment, allocationOption);
_buffer = (byte*)_handle->Alloc(_handle->Allocator, (uint)totalSize, (uint)alignSize, allocationOption); _buffer = buf + valueOffset;
_keys = (TKey*)(_buffer + keyOffset); _keys = (TKey*)(_buffer + keyOffset);
_next = (int*)(_buffer + nextOffset); _next = (int*)(_buffer + nextOffset);
_buckets = (int*)(_buffer + bucketOffset); _buckets = (int*)(_buffer + bucketOffset);
} }
internal void ResizeExact(int newCapacity, int newBucketCapacity) private void ResizeExact(int newCapacity, int newBucketCapacity)
{ {
var totalSize = CalculateDataSize(newCapacity, newBucketCapacity, _sizeOfTValue, var totalSize = CalculateDataSize(newCapacity, newBucketCapacity, _sizeOfTValue, _alignment,
out var keyOffset, out var nextOffset, out var bucketOffset); out var valueOffset, out var keyOffset, out var nextOffset, out var bucketOffset);
var oldBuffer = _buffer; var oldBuffer = _buffer;
var oldKeys = _keys; var oldKeys = _keys;
@@ -195,7 +231,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
var oldBuckets = _buckets; var oldBuckets = _buckets;
var oldBucketCapacity = _bucketCapacity; var oldBucketCapacity = _bucketCapacity;
AllocateBuffer(totalSize, keyOffset, nextOffset, bucketOffset, AllocationOption.None); AllocateBuffer(totalSize, valueOffset, keyOffset, nextOffset, bucketOffset, _alignment, AllocationOption.None);
_capacity = newCapacity; _capacity = newCapacity;
_bucketCapacity = newBucketCapacity; _bucketCapacity = newBucketCapacity;
@@ -213,8 +249,10 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_handle->Free(_handle->Allocator, oldBuffer); _handle->Free(_handle->Allocator, oldBuffer);
} }
internal void Resize(int newCapacity) public void Resize(int newCapacity)
{ {
ThrowIfNotCreated();
newCapacity = Math.Max(newCapacity, _count); newCapacity = Math.Max(newCapacity, _count);
var newBucketCapacity = CeilPow2(newCapacity * 2); var newBucketCapacity = CeilPow2(newCapacity * 2);
@@ -228,12 +266,16 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public void TrimExcess() public void TrimExcess()
{ {
ThrowIfNotCreated();
var capacity = CalcCapacityCeilPow2(_count); var capacity = CalcCapacityCeilPow2(_count);
ResizeExact(capacity, capacity * 2); ResizeExact(capacity, capacity * 2);
} }
public int Find(in TKey key) public int Find(in TKey key)
{ {
ThrowIfNotCreated();
if (_allocatedIndex <= 0) if (_allocatedIndex <= 0)
{ {
return -1; return -1;
@@ -263,6 +305,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public int TryAdd(in TKey key) public int TryAdd(in TKey key)
{ {
ThrowIfNotCreated();
var k = key; var k = key;
if (Find(in key) != -1) if (Find(in key) != -1)
{ {
@@ -306,6 +350,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public int TryRemove(in TKey key) public int TryRemove(in TKey key)
{ {
ThrowIfNotCreated();
if (_capacity == 0) if (_capacity == 0)
{ {
return -1; return -1;
@@ -357,6 +403,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public bool TryGetValue<TValue>(in TKey key, out TValue item) public bool TryGetValue<TValue>(in TKey key, out TValue item)
where TValue : unmanaged where TValue : unmanaged
{ {
ThrowIfNotCreated();
var idx = Find(key); var idx = Find(key);
if (idx != -1) if (idx != -1)
@@ -371,6 +419,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public bool MoveNextSearch(ref int bucketIndex, ref int nextIndex, out int index) public bool MoveNextSearch(ref int bucketIndex, ref int nextIndex, out int index)
{ {
ThrowIfNotCreated();
for (int i = bucketIndex, num = _bucketCapacity; i < num; ++i) for (int i = bucketIndex, num = _bucketCapacity; i < num; ++i)
{ {
var idx = _buckets[i]; var idx = _buckets[i];
@@ -394,6 +444,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext(ref int bucketIndex, ref int nextIndex, out int index) public bool MoveNext(ref int bucketIndex, ref int nextIndex, out int index)
{ {
ThrowIfNotCreated();
if (nextIndex != -1) if (nextIndex != -1)
{ {
index = nextIndex; index = nextIndex;
@@ -406,6 +458,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
internal UnsafeArray<TKey> GetKeyArray(Allocator allocator) internal UnsafeArray<TKey> GetKeyArray(Allocator allocator)
{ {
ThrowIfNotCreated();
var result = new UnsafeArray<TKey>(_count, allocator); var result = new UnsafeArray<TKey>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++) for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
@@ -425,6 +479,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
internal UnsafeArray<TValue> GetValueArray<TValue>(Allocator allocator) internal UnsafeArray<TValue> GetValueArray<TValue>(Allocator allocator)
where TValue : unmanaged where TValue : unmanaged
{ {
ThrowIfNotCreated();
var result = new UnsafeArray<TValue>(_count, allocator); var result = new UnsafeArray<TValue>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; ++i) for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; ++i)
@@ -444,6 +500,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays<TValue>(Allocator allocator) public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays<TValue>(Allocator allocator)
where TValue : unmanaged where TValue : unmanaged
{ {
ThrowIfNotCreated();
var result = new UnsafeArray<KeyValuePair<TKey, TValue>>(_count, allocator); var result = new UnsafeArray<KeyValuePair<TKey, TValue>>(_count, allocator);
for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++) for (int i = 0, count = 0, max = result.Count, capacity = _bucketCapacity; i < capacity && count < max; i++)
@@ -465,6 +523,8 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public void Clear() public void Clear()
{ {
ThrowIfNotCreated();
MemSet(_buckets, 0xff, (nuint)_bucketCapacity * sizeof(int)); MemSet(_buckets, 0xff, (nuint)_bucketCapacity * sizeof(int));
MemSet(_next, 0xff, (nuint)_capacity * sizeof(int)); MemSet(_next, 0xff, (nuint)_capacity * sizeof(int));
@@ -475,9 +535,15 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public void Dispose() public void Dispose()
{ {
if (IsCreated) if (!IsCreated)
{
return;
}
if (_handle != null)
{ {
_handle->Free(_handle->Allocator, _buffer); _handle->Free(_handle->Allocator, _buffer);
}
_buffer = null; _buffer = null;
_keys = null; _keys = null;
@@ -489,4 +555,3 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_bucketCapacity = 0; _bucketCapacity = 0;
} }
} }
}

View File

@@ -1,55 +1,49 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.Intrinsics;
namespace Misaki.HighPerformance.Test.Benchmark; namespace Misaki.HighPerformance.Test.Benchmark;
[MemoryDiagnoser] public class CollectionBenchmark
public unsafe class CollectionBenchmark
{ {
private UnsafeArray<Vector256<int>> _array;
[Params(10, 100, 1000)] [Params(10, 100, 1000)]
public int count; public int count;
[GlobalSetup] [GlobalSetup]
public void Setup() public void Setup()
{ {
} _array = new UnsafeArray<Vector256<int>>(count, Allocator.Persistent);
[Benchmark]
public void Array()
{
var array = new int[count];
for (var i = 0; i < count; i++)
{
array[i] = i;
}
}
[Benchmark(Baseline = true)]
public void UnsafeArray()
{
var array = new UnsafeArray<int>(count, Allocator.Temp);
for (var i = 0; i < count; i++)
{
array[i] = i;
}
AllocationManager.ResetTempAllocator();
}
[Benchmark]
public void StackArray()
{
var array = stackalloc int[count];
for (var i = 0; i < count; i++)
{
array[i] = i;
}
} }
[GlobalCleanup] [GlobalCleanup]
public void Cleanup() public void Cleanup()
{ {
AllocationManager.Dispose(); _array.Dispose();
}
[Benchmark]
public void WithCapacityChecks()
{
for (var i = 0; i < _array.Count; i++)
{
if (i < 0 || i >= _array.Count)
{
throw new IndexOutOfRangeException();
}
_array[i] = default;
}
}
[Benchmark]
public void WithoutCapacityChecks()
{
for (var i = 0; i < _array.Count; i++)
{
_array[i] = default;
}
} }
} }

View File

@@ -9,6 +9,10 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'" />
<ItemGroup> <ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" /> <PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="MSTest" Version="3.10.1" /> <PackageReference Include="MSTest" Version="3.10.1" />

View File

@@ -18,9 +18,7 @@
//Console.WriteLine($"Count should be {threadCount * 990}, actual: {map.Count}"); //Console.WriteLine($"Count should be {threadCount * 990}, actual: {map.Count}");
//using Misaki.HighPerformance.Test.Benchmark; BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.CollectionBenchmark>();
BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmark.MathematicsBenchmark>();
//using Misaki.HighPerformance.LowLevel.Buffer; //using Misaki.HighPerformance.LowLevel.Buffer;
//using Misaki.HighPerformance.LowLevel.Collections; //using Misaki.HighPerformance.LowLevel.Collections;
@@ -40,3 +38,16 @@ BenchmarkDotNet.Running.BenchmarkRunner.Run<Misaki.HighPerformance.Test.Benchmar
// } // }
//} //}
//var arr1 = new Misaki.HighPerformance.LowLevel.Collections.UnsafeArray<int>(10, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent);
//var arr2 = arr1;
//arr1.Dispose();
//try
//{
// arr2[0] = 42; // This should throw an exception because arr1 has been disposed.
//}
//catch (Exception ex)
//{
// Console.WriteLine($"Caught expected exception: {ex.Message}");
//}
//arr2.Dispose(); // This should not cause a double free error because of safe handle.