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:
@@ -7,7 +7,7 @@
|
|||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>Misaki</Authors>
|
<Authors>Misaki</Authors>
|
||||||
<AssemblyVersion>1.3.8</AssemblyVersion>
|
<AssemblyVersion>1.3.9</AssemblyVersion>
|
||||||
<Version>$(AssemblyVersion)</Version>
|
<Version>$(AssemblyVersion)</Version>
|
||||||
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
|
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>
|
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||||
using Misaki.HighPerformance.LowLevel.Collections;
|
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Misaki.HighPerformance.LowLevel.Utilities;
|
namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||||
|
|
||||||
public unsafe struct HashMapHelper<TKey> : IDisposable
|
public unsafe struct HashMapHelper<TKey> : IDisposable
|
||||||
where TKey : unmanaged, IEquatable<TKey>
|
where TKey : unmanaged, IEquatable<TKey>
|
||||||
@@ -79,6 +79,8 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
public unsafe struct ParallelWriter
|
public unsafe struct ParallelWriter
|
||||||
{
|
{
|
||||||
|
private volatile int _resizeLock;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The UnsafeList to write to.
|
/// The UnsafeList to write to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -259,22 +261,20 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
|||||||
/// Adds a range of elements to the collection.
|
/// Adds a range of elements to the collection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="values">A span containing the elements to add. The span must not exceed the specified <paramref name="count"/>.</param>
|
/// <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
|
public void AddRange(Span<T> values)
|
||||||
/// equal to the length of <paramref name="values"/>.</param>
|
|
||||||
public void AddRange(Span<T> values, int count)
|
|
||||||
{
|
{
|
||||||
var newSize = _count + count;
|
var newSize = _count + values.Length;
|
||||||
if (newSize > Capacity)
|
if (newSize > Capacity)
|
||||||
{
|
{
|
||||||
Resize(Capacity + count);
|
Resize(Capacity + values.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
fixed (T* ptr = values)
|
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>
|
/// <summary>
|
||||||
|
|||||||
@@ -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.
|
/// Attempts to remove the item at the specified slot index and generation from the collection.
|
||||||
/// </summary>
|
/// </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="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
|
/// <param name="generation">The generation value associated with the slot. Removal succeeds only if this matches the current generation of the slot.</param>
|
||||||
/// 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>
|
/// <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)
|
if (slotIndex < 0 || slotIndex >= _capacity)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -192,6 +193,8 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
item = _data[slotIndex];
|
||||||
|
|
||||||
gen++;
|
gen++;
|
||||||
_validBits.ClearBit(slotIndex);
|
_validBits.ClearBit(slotIndex);
|
||||||
_freeSlots.Enqueue(slotIndex);
|
_freeSlots.Enqueue(slotIndex);
|
||||||
@@ -201,6 +204,18 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
|||||||
return true;
|
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>
|
/// <summary>
|
||||||
/// Determines whether the specified slot index contains a valid entry with the given generation.
|
/// Determines whether the specified slot index contains a valid entry with the given generation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -190,6 +190,49 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
|
|||||||
return sparseIndex;
|
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>
|
/// <summary>
|
||||||
/// Removes the value at the specified sparse index.
|
/// Removes the value at the specified sparse index.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Misaki.HighPerformance.LowLevel;
|
namespace Misaki.HighPerformance.LowLevel;
|
||||||
@@ -45,9 +46,11 @@ public class MemoryLeakException : Exception
|
|||||||
for (var i = 0; i < stackTrace.FrameCount; i++)
|
for (var i = 0; i < stackTrace.FrameCount; i++)
|
||||||
{
|
{
|
||||||
var frame = stackTrace.GetFrame(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()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,27 +34,6 @@ internal struct SPMDJobWrapper<T, TNumber> : IJobParallelFor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//public void Execute(int startIndex, int endIndex, int threadIndex)
|
|
||||||
//{
|
|
||||||
// for (int i = startIndex; i < endIndex; i++)
|
|
||||||
// {
|
|
||||||
// var baseIndex = i * WideLane<TNumber>.LaneWidth;
|
|
||||||
// var remaining = totalCount - baseIndex;
|
|
||||||
|
|
||||||
// if (remaining >= WideLane<TNumber>.LaneWidth)
|
|
||||||
// {
|
|
||||||
// innerJob.Execute<WideLane<TNumber>>(baseIndex, threadIndex);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// for (var j = 0; j < remaining; j++)
|
|
||||||
// {
|
|
||||||
// innerJob.Execute<ScalarLane<TNumber>>(baseIndex + j, threadIndex);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal struct SPMDScalerJobWrapper<T, TNumber> : IJobParallelFor
|
internal struct SPMDScalerJobWrapper<T, TNumber> : IJobParallelFor
|
||||||
@@ -68,15 +47,6 @@ internal struct SPMDScalerJobWrapper<T, TNumber> : IJobParallelFor
|
|||||||
{
|
{
|
||||||
innerJob.Execute<ScalarLane<TNumber>>(loopIndex, threadIndex);
|
innerJob.Execute<ScalarLane<TNumber>>(loopIndex, threadIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
//public void Execute(int startIndex, int endIndex, int threadIndex)
|
|
||||||
//{
|
|
||||||
// for (int i = startIndex; i < endIndex; i++)
|
|
||||||
// {
|
|
||||||
// innerJob.Execute<ScalarLane<TNumber>>(i, threadIndex);
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class IJobParallelForSPMDExtensions
|
public static class IJobParallelForSPMDExtensions
|
||||||
|
|||||||
Reference in New Issue
Block a user