Refactor collections, add Remove overloads, bump version

- Bump project version to 1.3.9.
- Move HashMapHelper to Collections namespace.
- Simplify UnsafeList<T>.AddRange to use only Span<T>.
- Add Remove overloads with out parameter to UnsafeSlotMap<T> and UnsafeSparseSet<T>.
- Improve MemoryLeakException stack trace formatting.
- Remove obsolete commented code in IJobSPMD.cs.
This commit is contained in:
2026-03-02 15:16:48 +09:00
parent 9413c1ee0b
commit b9ca71834f
7 changed files with 76 additions and 45 deletions

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors>
<AssemblyVersion>1.3.8</AssemblyVersion>
<AssemblyVersion>1.3.9</AssemblyVersion>
<Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>

View File

@@ -1,10 +1,10 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Utilities;
namespace Misaki.HighPerformance.LowLevel.Collections;
public unsafe struct HashMapHelper<TKey> : IDisposable
where TKey : unmanaged, IEquatable<TKey>

View File

@@ -79,6 +79,8 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// </remarks>
public unsafe struct ParallelWriter
{
private volatile int _resizeLock;
/// <summary>
/// The UnsafeList to write to.
/// </summary>
@@ -259,22 +261,20 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// Adds a range of elements to the collection.
/// </summary>
/// <param name="values">A span containing the elements to add. The span must not exceed the specified <paramref name="count"/>.</param>
/// <param name="count">The number of elements to add from the <paramref name="values"/> span. Must be non-negative and less than or
/// equal to the length of <paramref name="values"/>.</param>
public void AddRange(Span<T> values, int count)
public void AddRange(Span<T> values)
{
var newSize = _count + count;
var newSize = _count + values.Length;
if (newSize > Capacity)
{
Resize(Capacity + count);
Resize(Capacity + values.Length);
}
fixed (T* ptr = values)
{
MemCpy(UnsafeUtility.ReadArrayElementUnsafe<T>(_array.GetUnsafePtr(), _count), ptr, (uint)(count * sizeof(T)));
MemCpy(UnsafeUtility.ReadArrayElementUnsafe<T>(_array.GetUnsafePtr(), _count), ptr, (uint)(values.Length * sizeof(T)));
}
_count += count;
_count += values.Length;
}
/// <summary>

View File

@@ -176,11 +176,12 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// Attempts to remove the item at the specified slot index and generation from the collection.
/// </summary>
/// <param name="slotIndex">The zero-based index of the slot to remove. Must be within the valid range of slot indices.</param>
/// <param name="generation">The generation value associated with the slot. Removal succeeds only if this matches the current generation of
/// the slot.</param>
/// <param name="generation">The generation value associated with the slot. Removal succeeds only if this matches the current generation of the slot.</param>
/// <param name="item">When this method returns, contains the item that was removed if the removal was successful; otherwise, the default value for type <typeparamref name="T"/>.</param>
/// <returns>true if the item was successfully removed; otherwise, false.</returns>
public bool Remove(int slotIndex, int generation)
public bool Remove(int slotIndex, int generation, out T item)
{
item = default;
if (slotIndex < 0 || slotIndex >= _capacity)
{
return false;
@@ -192,6 +193,8 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
return false;
}
item = _data[slotIndex];
gen++;
_validBits.ClearBit(slotIndex);
_freeSlots.Enqueue(slotIndex);
@@ -201,6 +204,18 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
return true;
}
/// <summary>
/// Attempts to remove the item at the specified slot index and generation from the collection.
/// </summary>
/// <param name="slotIndex">The zero-based index of the slot to remove. Must be within the valid range of slot indices.</param>
/// <param name="generation">The generation value associated with the slot. Removal succeeds only if this matches the current generation of
/// the slot.</param>
/// <returns>true if the item was successfully removed; otherwise, false.</returns>
public bool Remove(int slotIndex, int generation)
{
return Remove(slotIndex, generation, out _);
}
/// <summary>
/// Determines whether the specified slot index contains a valid entry with the given generation.
/// </summary>

View File

@@ -190,6 +190,49 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
return sparseIndex;
}
/// <summary>
/// Removes the value at the specified sparse index.
/// </summary>
/// <param name="sparseIndex">The sparse index of the value to remove.</param>
/// <param name="generation">The generation number associated with the sparse index to validate.</param>
/// <param name="item">When this method returns, contains the item that was removed if the removal was successful; otherwise, the default value for type <typeparamref name="T"/>.</param>
/// <returns>True if the value was removed, false if the sparse index was not found.</returns>
public bool Remove(int sparseIndex, int generation, out T item)
{
item = default;
if (!Contains(sparseIndex, generation))
{
return false;
}
var denseIndex = _sparse[sparseIndex];
var lastIndex = _count - 1;
if (denseIndex != lastIndex)
{
// Move the last element to the position of the removed element
var lastValue = _dense[lastIndex];
var lastSparseIndex = _reverse[lastIndex]; // Get sparse index of last element
_dense[denseIndex] = lastValue;
_reverse[denseIndex] = lastSparseIndex;
// Update the sparse mapping for the moved element
_sparse[lastSparseIndex] = denseIndex;
}
// Mark the sparse index as unused and add to free list
_sparse[sparseIndex] = -1;
_generations[sparseIndex]++; // Increment generation to invalidate old references
item = _dense[denseIndex];
_freeSparse.Push(sparseIndex);
_count--;
return true;
}
/// <summary>
/// Removes the value at the specified sparse index.
/// </summary>

View File

@@ -1,5 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
namespace Misaki.HighPerformance.LowLevel;
@@ -45,9 +46,11 @@ public class MemoryLeakException : Exception
for (var i = 0; i < stackTrace.FrameCount; i++)
{
var frame = stackTrace.GetFrame(i);
if (frame != null)
var fileName = frame?.GetFileName();
if (frame != null && fileName != null)
{
stringBuilder.AppendLine($"File: {frame.GetFileName()}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
stringBuilder.AppendLine($"File: {fileName}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
}
}