Refactor collections to use 'scoped in T' parameters

Updated Add/Remove/Enqueue/Push/etc. methods in core unsafe collections to accept parameters as 'scoped in T' for improved performance and safety. Bumped assembly versions in both csproj files.
This commit is contained in:
2026-05-02 13:52:45 +09:00
parent eb01e557d5
commit d6b4074281
9 changed files with 14 additions and 14 deletions

View File

@@ -74,7 +74,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>
/// <param name="item">The value to add.</param> /// <param name="item">The value to add.</param>
/// <returns>True if the value was not already present.</returns> /// <returns>True if the value was not already present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Add(T item) public bool Add(scoped in T item)
{ {
return -1 != _helper.TryAdd(item); return -1 != _helper.TryAdd(item);
} }
@@ -85,7 +85,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>
/// <param name="item">The value to remove.</param> /// <param name="item">The value to remove.</param>
/// <returns>True if the value was present.</returns> /// <returns>True if the value was present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(T item) public bool Remove(scoped in T item)
{ {
return -1 != _helper.TryRemove(item); return -1 != _helper.TryRemove(item);
} }
@@ -96,7 +96,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>
/// <param name="item">The value to check for.</param> /// <param name="item">The value to check for.</param>
/// <returns>True if the value was present.</returns> /// <returns>True if the value was present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item) public bool Contains(scoped in T item)
{ {
return -1 != _helper.Find(item); return -1 != _helper.Find(item);
} }

View File

@@ -126,7 +126,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// Adds a value to a collection without resizing it, ensuring capacity is checked before insertion. /// Adds a value to a collection without resizing it, ensuring capacity is checked before insertion.
/// </summary> /// </summary>
/// <param name="value">The value to be added to the collection.</param> /// <param name="value">The value to be added to the collection.</param>
public void AddNoResize(T value) public void AddNoResize(scoped in T value)
{ {
var idx = Interlocked.Increment(ref listData->_count) - 1; var idx = Interlocked.Increment(ref listData->_count) - 1;
listData->CheckNoResizeCapacity(idx, 1); listData->CheckNoResizeCapacity(idx, 1);
@@ -305,7 +305,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// Adds a new element to the end of the list, resizing the internal array if necessary. /// Adds a new element to the end of the list, resizing the internal array if necessary.
/// </summary> /// </summary>
/// <param name="value">The element to be added to the list.</param> /// <param name="value">The element to be added to the list.</param>
public void Add(T value) public void Add(scoped in T value)
{ {
if (_count >= Capacity) if (_count >= Capacity)
{ {
@@ -320,7 +320,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// Adds the specified value to the collection without resizing the underlying storage. /// Adds the specified value to the collection without resizing the underlying storage.
/// </summary> /// </summary>
/// <param name="value">The value to add to the collection.</param> /// <param name="value">The value to add to the collection.</param>
public void AddNoResize(T value) public void AddNoResize(scoped in T value)
{ {
CheckNoResizeCapacity(1); CheckNoResizeCapacity(1);

View File

@@ -47,7 +47,7 @@ public unsafe struct UnsafeParallelQueue<T> : IDisposable
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(T item) public void Enqueue(scoped in T item)
{ {
_queue->Enqueue(item); _queue->Enqueue(item);
} }
@@ -120,7 +120,7 @@ public unsafe struct UnsafeParallelQueue<T> : IDisposable
/// <summary> /// <summary>
/// Try to enqueue an item. Expands automatically if the current chunk is full. /// Try to enqueue an item. Expands automatically if the current chunk is full.
/// </summary> /// </summary>
public void Enqueue(T item) public void Enqueue(scoped in T item)
{ {
SpinWait spin = new SpinWait(); SpinWait spin = new SpinWait();

View File

@@ -122,7 +122,7 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
/// stored in a circular buffer. /// stored in a circular buffer.
/// </summary> /// </summary>
/// <param name="value">The item to be added to the collection.</param> /// <param name="value">The item to be added to the collection.</param>
public void Enqueue(T value) public void Enqueue(scoped in T value)
{ {
if (_count >= Capacity) if (_count >= Capacity)
{ {

View File

@@ -206,7 +206,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="item">The item to add to the collection.</param> /// <param name="item">The item to add to the collection.</param>
/// <param name="generation">When this method returns, contains the Generation number associated with the slot where the item was stored.</param> /// <param name="generation">When this method returns, contains the Generation number associated with the slot where the item was stored.</param>
/// <returns>The index of the slot in which the item was stored.</returns> /// <returns>The index of the slot in which the item was stored.</returns>
public int Add(T item, out int generation) public int Add(scoped in T item, out int generation)
{ {
if (_freeSlots.Count > 0) if (_freeSlots.Count > 0)
{ {

View File

@@ -158,7 +158,7 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
/// <param name="value">The value to add to the sparse set.</param> /// <param name="value">The value to add to the sparse set.</param>
/// <param name="generation">Outputs the Generation number associated with the added value.</param> /// <param name="generation">Outputs the Generation number associated with the added value.</param>
/// <returns>A unique sparse index that can be used to reference this value.</returns> /// <returns>A unique sparse index that can be used to reference this value.</returns>
public int Add(T value, out int generation) public int Add(scoped in T value, out int generation)
{ {
if (!_freeSparse.TryPop(out var sparseIndex)) if (!_freeSparse.TryPop(out var sparseIndex))
{ {

View File

@@ -119,7 +119,7 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
/// Adds an element to the top of the stack. /// Adds an element to the top of the stack.
/// </summary> /// </summary>
/// <param name="value">The element to add to the stack.</param> /// <param name="value">The element to add to the stack.</param>
public void Push(T value) public void Push(scoped in T value)
{ {
if (_count >= Capacity) if (_count >= Capacity)
{ {

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.6.16</AssemblyVersion> <AssemblyVersion>1.6.17</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>

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.0.8</AssemblyVersion> <AssemblyVersion>1.0.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>