From d6b4074281a3c84356f78c2391257d82d79127a2 Mon Sep 17 00:00:00 2001 From: Misaki Date: Sat, 2 May 2026 13:52:45 +0900 Subject: [PATCH] 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. --- .../Collections/UnsafeHashSet.cs | 6 +++--- Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs | 6 +++--- .../Collections/UnsafeParallelQueue.cs | 4 ++-- Misaki.HighPerformance.LowLevel/Collections/UnsafeQueue.cs | 2 +- .../Collections/UnsafeSlotMap.cs | 2 +- .../Collections/UnsafeSparseSet.cs | 2 +- Misaki.HighPerformance.LowLevel/Collections/UnsafeStack.cs | 2 +- .../Misaki.HighPerformance.LowLevel.csproj | 2 +- Misaki.HighPerformance/Misaki.HighPerformance.csproj | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs index a64723a..a6f17b9 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs @@ -74,7 +74,7 @@ public unsafe struct UnsafeHashSet : IUnsafeHashCollection /// The value to add. /// True if the value was not already present. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Add(T item) + public bool Add(scoped in T item) { return -1 != _helper.TryAdd(item); } @@ -85,7 +85,7 @@ public unsafe struct UnsafeHashSet : IUnsafeHashCollection /// The value to remove. /// True if the value was present. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Remove(T item) + public bool Remove(scoped in T item) { return -1 != _helper.TryRemove(item); } @@ -96,7 +96,7 @@ public unsafe struct UnsafeHashSet : IUnsafeHashCollection /// The value to check for. /// True if the value was present. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Contains(T item) + public bool Contains(scoped in T item) { return -1 != _helper.Find(item); } diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs index b94a136..bad623b 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeList.cs @@ -126,7 +126,7 @@ public unsafe struct UnsafeList : IUnsafeCollection /// Adds a value to a collection without resizing it, ensuring capacity is checked before insertion. /// /// The value to be added to the collection. - public void AddNoResize(T value) + public void AddNoResize(scoped in T value) { var idx = Interlocked.Increment(ref listData->_count) - 1; listData->CheckNoResizeCapacity(idx, 1); @@ -305,7 +305,7 @@ public unsafe struct UnsafeList : IUnsafeCollection /// Adds a new element to the end of the list, resizing the internal array if necessary. /// /// The element to be added to the list. - public void Add(T value) + public void Add(scoped in T value) { if (_count >= Capacity) { @@ -320,7 +320,7 @@ public unsafe struct UnsafeList : IUnsafeCollection /// Adds the specified value to the collection without resizing the underlying storage. /// /// The value to add to the collection. - public void AddNoResize(T value) + public void AddNoResize(scoped in T value) { CheckNoResizeCapacity(1); diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeParallelQueue.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeParallelQueue.cs index e0887ff..c69944f 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeParallelQueue.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeParallelQueue.cs @@ -47,7 +47,7 @@ public unsafe struct UnsafeParallelQueue : IDisposable } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Enqueue(T item) + public void Enqueue(scoped in T item) { _queue->Enqueue(item); } @@ -120,7 +120,7 @@ public unsafe struct UnsafeParallelQueue : IDisposable /// /// Try to enqueue an item. Expands automatically if the current chunk is full. /// - public void Enqueue(T item) + public void Enqueue(scoped in T item) { SpinWait spin = new SpinWait(); diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeQueue.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeQueue.cs index eda1b70..1984986 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeQueue.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeQueue.cs @@ -122,7 +122,7 @@ public unsafe struct UnsafeQueue : IUnsafeCollection /// stored in a circular buffer. /// /// The item to be added to the collection. - public void Enqueue(T value) + public void Enqueue(scoped in T value) { if (_count >= Capacity) { diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeSlotMap.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeSlotMap.cs index b29b9a7..04f15a9 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeSlotMap.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeSlotMap.cs @@ -206,7 +206,7 @@ public unsafe struct UnsafeSlotMap : IUnsafeCollection /// The item to add to the collection. /// When this method returns, contains the Generation number associated with the slot where the item was stored. /// The index of the slot in which the item was stored. - public int Add(T item, out int generation) + public int Add(scoped in T item, out int generation) { if (_freeSlots.Count > 0) { diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeSparseSet.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeSparseSet.cs index 8739443..199ab16 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeSparseSet.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeSparseSet.cs @@ -158,7 +158,7 @@ public unsafe struct UnsafeSparseSet : IUnsafeCollection /// The value to add to the sparse set. /// Outputs the Generation number associated with the added value. /// A unique sparse index that can be used to reference this value. - public int Add(T value, out int generation) + public int Add(scoped in T value, out int generation) { if (!_freeSparse.TryPop(out var sparseIndex)) { diff --git a/Misaki.HighPerformance.LowLevel/Collections/UnsafeStack.cs b/Misaki.HighPerformance.LowLevel/Collections/UnsafeStack.cs index 06010f4..810d2ab 100644 --- a/Misaki.HighPerformance.LowLevel/Collections/UnsafeStack.cs +++ b/Misaki.HighPerformance.LowLevel/Collections/UnsafeStack.cs @@ -119,7 +119,7 @@ public unsafe struct UnsafeStack : IUnsafeCollection /// Adds an element to the top of the stack. /// /// The element to add to the stack. - public void Push(T value) + public void Push(scoped in T value) { if (_count >= Capacity) { diff --git a/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj b/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj index 8732eda..bb56f5e 100644 --- a/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj +++ b/Misaki.HighPerformance.LowLevel/Misaki.HighPerformance.LowLevel.csproj @@ -7,7 +7,7 @@ true true Misaki - 1.6.16 + 1.6.17 $(AssemblyVersion) https://git.personalnas.com/Misaki/Misaki.HighPerformance.git https://git.personalnas.com/Misaki/Misaki.HighPerformance.git diff --git a/Misaki.HighPerformance/Misaki.HighPerformance.csproj b/Misaki.HighPerformance/Misaki.HighPerformance.csproj index 2080984..bf9472c 100644 --- a/Misaki.HighPerformance/Misaki.HighPerformance.csproj +++ b/Misaki.HighPerformance/Misaki.HighPerformance.csproj @@ -7,7 +7,7 @@ True True Misaki - 1.0.8 + 1.0.9 $(AssemblyVersion) https://git.personalnas.com/Misaki/Misaki.HighPerformance.git https://git.personalnas.com/Misaki/Misaki.HighPerformance.git