Cleanup. Hints and warnings either disabled or fixed. Typos fixed.

This commit is contained in:
mlemanczyk committed 2023-07-01 17:07:16 +02:00
1 parent 2317f53d51
commit c20b252a6c
17 files changed
+79 -39

No files matched your search

+1 -1
View File
@@ -143,7 +143,7 @@
1. 🅿️ Implement `ILongList<T>` interface 1. 🅿️ Implement `ILongList<T>` interface
1. 🅿️ `RecyclableList<T>` 1. 🅿️ `RecyclableList<T>`
1. 🅿️ `RecyclableLongList<T>` 1. 🅿️ `RecyclableLongList<T>`
1. 🅿️ Implement `RecyclableQeueue<T>` 1. 🅿️ Implement `RecyclableQueue<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableQueue<T>` 1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableQueue<T>`
1. 🅿️ Release 0.0.4 1. 🅿️ Release 0.0.4
1. 🅿️ Implement `RecyclableStack<T>` 1. 🅿️ Implement `RecyclableStack<T>`
@@ -1,5 +1,7 @@
using Collections.Pooled; using Collections.Pooled;
#pragma warning disable CA1822, CA1825
namespace Recyclable.Collections.Benchmarks namespace Recyclable.Collections.Benchmarks
{ {
public partial class RecyclableCollectionsBenchmarks public partial class RecyclableCollectionsBenchmarks
@@ -53,7 +53,7 @@ namespace Recyclable.Collections.Benchmarks.POC
private struct MyComparerStruct<T> : IComparer<T> private struct MyComparerStruct<T> : IComparer<T>
{ {
public int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y); public readonly int Compare(T? x, T? y) => Comparer<T>.Default.Compare(x, y);
} }
private class MyComparer : IComparer<long> private class MyComparer : IComparer<long>
@@ -132,7 +132,7 @@ namespace Recyclable.Collections.Benchmarks.POC
} }
} }
private void TestWithDelegate<T>(Compare<long> compareFunc) private void TestWithDelegate(Compare<long> compareFunc)
{ {
var testData = _testObjects!.ToArray(); var testData = _testObjects!.ToArray();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++) for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
@@ -177,7 +177,7 @@ namespace Recyclable.Collections.Benchmarks.POC
public void Delegate() public void Delegate()
{ {
TestWithDelegate<long>((x, y) => (x - y) switch TestWithDelegate((x, y) => (x - y) switch
{ {
> 0 => 1, > 0 => 1,
< 0 => -1, < 0 => -1,
@@ -1,12 +1,14 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#pragma warning disable CA1822
namespace Recyclable.Collections.Benchmarks.POC namespace Recyclable.Collections.Benchmarks.POC
{ {
public class LessOperatorVsAndOperatorBenchmarks public class LessOperatorVsAndOperatorBenchmarks
{ {
private const int bitShift = 1 << 31; private const int bitShift = 1 << 31;
private static Random _rnd = new(1); private static readonly Random _rnd = new(1);
private static int num = -Math.Abs(_rnd.Next()); private static int num = -Math.Abs(_rnd.Next());
public void Setup() public void Setup()
@@ -17,10 +19,13 @@ namespace Recyclable.Collections.Benchmarks.POC
public void Cleanup() { } public void Cleanup() { }
#pragma warning disable RCS1163, IDE0060
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
private static void DoNothing<T>(T item) private static void DoNothing<T>(T item)
{ } {
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)] [Benchmark(Baseline = true)]
public void CompareWithLess() public void CompareWithLess()
@@ -4,6 +4,8 @@ using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics; using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics.X86;
#pragma warning disable xUnit1026, RCS1163, IDE0060
namespace Recyclable.Collections.Benchmarks.POC namespace Recyclable.Collections.Benchmarks.POC
{ {
[MemoryDiagnoser] [MemoryDiagnoser]
@@ -124,7 +126,7 @@ namespace Recyclable.Collections.Benchmarks.POC
DoNothing(result, remainder); DoNothing(result, remainder);
} }
private void CalculateWithSubractWithWhileWithSse2(long i) private void CalculateWithSubtractWithWhileWithSse2(long i)
{ {
var state = Vector128.Create(i, 0); var state = Vector128.Create(i, 0);
int blockSize = BlockSize; int blockSize = BlockSize;
@@ -137,7 +139,7 @@ namespace Recyclable.Collections.Benchmarks.POC
DoNothing(state.GetElement(1), state.GetElement(0)); DoNothing(state.GetElement(1), state.GetElement(0));
} }
private void CalculateWithMixedApproachWithSubractWithWhile(long i) private void CalculateWithMixedApproachWithSubtractWithWhile(long i)
{ {
long remainder; long remainder;
if (i >= _cutoff) if (i >= _cutoff)
@@ -317,7 +319,7 @@ namespace Recyclable.Collections.Benchmarks.POC
long testNumber = TestNumber; long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--) for (long i = testNumber; i >= _lastCaseIndex; i--)
{ {
CalculateWithSubractWithWhileWithSse2(i); CalculateWithSubtractWithWhileWithSse2(i);
} }
} }
@@ -327,7 +329,7 @@ namespace Recyclable.Collections.Benchmarks.POC
long testNumber = TestNumber; long testNumber = TestNumber;
for (long i = testNumber; i >= _lastCaseIndex; i--) for (long i = testNumber; i >= _lastCaseIndex; i--)
{ {
CalculateWithMixedApproachWithSubractWithWhile(i); CalculateWithMixedApproachWithSubtractWithWhile(i);
} }
} }
@@ -4,6 +4,8 @@ using System.Collections;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#pragma warning disable CA1825, RCS1085, CA2208
namespace Recyclable.Collections.Benchmarks.POC namespace Recyclable.Collections.Benchmarks.POC
{ {
#region RecyclableLongList v1 #region RecyclableLongList v1
@@ -26,7 +28,10 @@ namespace Recyclable.Collections.Benchmarks.POC
protected ArrayPool<T[]> _memoryBlocksPool; protected ArrayPool<T[]> _memoryBlocksPool;
protected ArrayPool<T> _blockArrayPool; protected ArrayPool<T> _blockArrayPool;
#nullable disable
protected T[][] _memoryBlocks; protected T[][] _memoryBlocks;
#nullable restore
private long _capacity; private long _capacity;
public long Capacity public long Capacity
@@ -341,10 +346,10 @@ namespace Recyclable.Collections.Benchmarks.POC
? checked((long)BitOperations.RoundUpToPowerOf2((ulong)requestedCapacity)) ? checked((long)BitOperations.RoundUpToPowerOf2((ulong)requestedCapacity))
: requestedCapacity; : requestedCapacity;
newCapacity = Resize(ref _memoryBlocks!, _blockSize, _capacity, newCapacity, _memoryBlocksPool, _blockArrayPool); newCapacity = Resize(ref _memoryBlocks, _blockSize, _capacity, newCapacity, _memoryBlocksPool, _blockArrayPool);
if (newCapacity > 0 && _capacity == 0) if (newCapacity > 0 && _capacity == 0)
{ {
_blockSize = _memoryBlocks[0].Length; _blockSize = _memoryBlocks![0].Length;
_blockSizePow2Shift = MathUtils.GetPow2Shift(_blockSize); _blockSizePow2Shift = MathUtils.GetPow2Shift(_blockSize);
} }
@@ -361,8 +366,8 @@ namespace Recyclable.Collections.Benchmarks.POC
if (expectedItemsCount > 0) if (expectedItemsCount > 0)
{ {
_capacity = Resize(ref _memoryBlocks!, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool);
if (_memoryBlocks.Length > 0) if (_memoryBlocks!.Length > 0)
{ {
minBlockSize = _memoryBlocks[0].Length; minBlockSize = _memoryBlocks[0].Length;
} }
@@ -387,8 +392,8 @@ namespace Recyclable.Collections.Benchmarks.POC
if (expectedItemsCount > 0) if (expectedItemsCount > 0)
{ {
_capacity = Resize(ref _memoryBlocks!, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool);
if (_memoryBlocks.Length > 0) if (_memoryBlocks!.Length > 0)
{ {
minBlockSize = _memoryBlocks[0].Length; minBlockSize = _memoryBlocks[0].Length;
} }
@@ -6,6 +6,8 @@ using System.Collections;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#pragma warning disable CA1825, RCS1085, CA2208
namespace Recyclable.Collections.Benchmarks.POC namespace Recyclable.Collections.Benchmarks.POC
{ {
#region ItemRangesIterator V1 #region ItemRangesIterator V1
@@ -19,7 +21,6 @@ namespace Recyclable.Collections.Benchmarks.POC
public ItemRangeV1() public ItemRangeV1()
{ {
} }
public ItemRangeV1(int blockIndex, int startingItemIndex, long itemsToSearchCount) public ItemRangeV1(int blockIndex, int startingItemIndex, long itemsToSearchCount)
@@ -560,7 +561,7 @@ namespace Recyclable.Collections.Benchmarks.POC
{ {
break; break;
} }
}; }
} }
else else
{ {
@@ -1,5 +1,7 @@
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Attributes;
#pragma warning disable RCS1085
namespace Recyclable.Collections.Benchmarks.POC namespace Recyclable.Collections.Benchmarks.POC
{ {
public class RefVsInstanceMemberPocBenchmarks public class RefVsInstanceMemberPocBenchmarks
+8 -4
View File
@@ -1,12 +1,15 @@
using BenchmarkDotNet.Configs; using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running; using BenchmarkDotNet.Running;
using Recyclable.Collections.Benchmarks;
using Recyclable.Collections.Benchmarks.POC; using Recyclable.Collections.Benchmarks.POC;
public static class Program #pragma warning disable RCS1036, RCS1213, IDE0051
namespace Recyclable.Collections.Benchmarks
{ {
public static class Program
{
static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance) static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary ); .WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary);
static void RunAssemblyBenchmarks() static void RunAssemblyBenchmarks()
{ {
@@ -50,11 +53,12 @@ public static class Program
// ArraySizeLimitPocBenchmarks>(BenchmarkConfig); // ArraySizeLimitPocBenchmarks>(BenchmarkConfig);
} }
static void Main(string[] args) static void Main()
{ {
RunRecyclableCollectionsBenchmarks(); RunRecyclableCollectionsBenchmarks();
// RunPocBenchmarks(); // RunPocBenchmarks();
// RunSelectedBenchmarks(); // RunSelectedBenchmarks();
// RunAssemblyBenchmarks(); // RunAssemblyBenchmarks();
} }
}
} }
@@ -22,7 +22,7 @@ namespace Recyclable.Collections.Benchmarks
protected long[] TestObjects => _testObjects ?? throw new NullReferenceException("Something is wrong and the field is not initialized"); protected long[] TestObjects => _testObjects ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
protected List<long>? _testObjectsAsList; protected List<long>? _testObjectsAsList;
protected List<long> TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wront and the field is not initialized"); protected List<long> TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wrong and the field is not initialized");
protected PooledList<long>? _testObjectsAsPooledList; protected PooledList<long>? _testObjectsAsPooledList;
public PooledList<long> TestObjectsAsPooledList => _testObjectsAsPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized"); public PooledList<long> TestObjectsAsPooledList => _testObjectsAsPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized");
+2
View File
@@ -13,10 +13,12 @@ namespace Recyclable.Collections
_memory = memory; _memory = memory;
} }
#pragma warning disable RCS1085
private T[] _memory; private T[] _memory;
public T[] Memory { get => _memory; set => _memory = value; } public T[] Memory { get => _memory; set => _memory = value; }
private MemoryBucket<T>? _nextBucket; private MemoryBucket<T>? _nextBucket;
public MemoryBucket<T>? NextBucket { get => _nextBucket; set => _nextBucket = value; } public MemoryBucket<T>? NextBucket { get => _nextBucket; set => _nextBucket = value; }
#pragma warning restore RCS1085
} }
} }
@@ -271,7 +271,9 @@ namespace Recyclable.Collections
: RecyclableArrayPool<T>.Null; : RecyclableArrayPool<T>.Null;
} }
#pragma warning disable CA2208
public static void ThrowIndexOutOfRangeException(in string message) => throw new ArgumentOutOfRangeException("index", message); public static void ThrowIndexOutOfRangeException(in string message) => throw new ArgumentOutOfRangeException("index", message);
#pragma warning restore CA2208
public static void ThrowArgumentOutOfRangeException(in string argumentName, in string message) => throw new ArgumentOutOfRangeException(argumentName, message); public static void ThrowArgumentOutOfRangeException(in string argumentName, in string message) => throw new ArgumentOutOfRangeException(argumentName, message);
} }
} }
+4 -3
View File
@@ -12,9 +12,12 @@ namespace Recyclable.Collections
{ {
private static readonly ArrayPool<T[]> _defaultMemoryBlocksPool = ArrayPool<T[]>.Create(); private static readonly ArrayPool<T[]> _defaultMemoryBlocksPool = ArrayPool<T[]>.Create();
private static readonly ArrayPool<T> _defaultBlockArrayPool = ArrayPool<T>.Create(); private static readonly ArrayPool<T> _defaultBlockArrayPool = ArrayPool<T>.Create();
#pragma warning disable CA1825
private static readonly T[][] _emptyMemoryBlocksArray = new T[0][]; private static readonly T[][] _emptyMemoryBlocksArray = new T[0][];
private static readonly T[] _emptyBlockArray = new T[0]; private static readonly T[] _emptyBlockArray = new T[0];
#pragma warning restore CA1825
private static readonly bool _defaultIsNull = default(T) == null; private static readonly bool _defaultIsNull = default(T) == null;
private static readonly bool NeedsClearing = !typeof(T).IsValueType;
internal int _blockSize; internal int _blockSize;
internal byte _blockSizePow2BitShift; internal byte _blockSizePow2BitShift;
@@ -23,9 +26,7 @@ namespace Recyclable.Collections
internal int _nextItemIndex; internal int _nextItemIndex;
private int _reservedBlockCount; private int _reservedBlockCount;
private static readonly bool NeedsClearing = !typeof(T).IsValueType; private readonly ArrayPool<T[]> _memoryBlocksPool;
private ArrayPool<T[]> _memoryBlocksPool;
private ArrayPool<T> _blockArrayPool; private ArrayPool<T> _blockArrayPool;
#nullable disable #nullable disable
internal T[][] _memoryBlocks; internal T[][] _memoryBlocks;
@@ -0,0 +1,13 @@
using System.Runtime.CompilerServices;
namespace Recyclable.Collections
{
internal static class RecyclableQueueHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ToArrayIndex(long index, int blockSize) => (int)(index / blockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToItemIndex(long index, int blockSize) => index % blockSize;
}
}
+9 -12
View File
@@ -95,18 +95,12 @@ namespace Recyclable.Collections
set => SetItem(index, value); set => SetItem(index, value);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ToArrayIndex(long index, int blockSize) => (int)(index / blockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ToItemIndex(long index, int blockSize) => index % blockSize;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetItem(long index, T value) private void SetItem(long index, T value)
{ {
long absoluteIndex = GetAbsoluteIndex(index, RemovedCount); long absoluteIndex = GetAbsoluteIndex(index, RemovedCount);
int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize); int arrayIndex = RecyclableQueueHelpers.ToArrayIndex(absoluteIndex, BlockSize);
long itemIndex = ToItemIndex(absoluteIndex, BlockSize); long itemIndex = RecyclableQueueHelpers.ToItemIndex(absoluteIndex, BlockSize);
Memory[arrayIndex][itemIndex] = value; Memory[arrayIndex][itemIndex] = value;
} }
@@ -114,8 +108,8 @@ namespace Recyclable.Collections
private T GetItem(long index) private T GetItem(long index)
{ {
long absoluteIndex = GetAbsoluteIndex(index, RemovedCount); long absoluteIndex = GetAbsoluteIndex(index, RemovedCount);
int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize); int arrayIndex = RecyclableQueueHelpers.ToArrayIndex(absoluteIndex, BlockSize);
long itemIndex = ToItemIndex(absoluteIndex, BlockSize); long itemIndex = RecyclableQueueHelpers.ToItemIndex(absoluteIndex, BlockSize);
return Memory[arrayIndex][itemIndex]; return Memory[arrayIndex][itemIndex];
} }
@@ -156,10 +150,10 @@ namespace Recyclable.Collections
{ {
RemoveBlock(Memory.Count - 1); RemoveBlock(Memory.Count - 1);
} }
#pragma warning disable RCS1075 // We want to try returning as many arrays, as possible, before the list is cleared.
catch (Exception) catch (Exception)
#pragma warning restore RCS1075
{ {
// We want to try returning as many arrays, as possible, before
// the list is cleared.
} }
} }
} }
@@ -171,6 +165,7 @@ namespace Recyclable.Collections
} }
} }
#pragma warning disable CS0618
public bool Contains(T item) => RecyclableListHelpers<T>.Contains(Memory, item); public bool Contains(T item) => RecyclableListHelpers<T>.Contains(Memory, item);
public void CopyTo(T[] array, int arrayIndex) => RecyclableListHelpers<T>.CopyTo(Memory, RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex); public void CopyTo(T[] array, int arrayIndex) => RecyclableListHelpers<T>.CopyTo(Memory, RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex);
public IEnumerable<T> Enumerate() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount); public IEnumerable<T> Enumerate() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount);
@@ -178,6 +173,8 @@ namespace Recyclable.Collections
IEnumerator IEnumerable.GetEnumerator() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount).GetEnumerator();
public int IndexOf(T item) => (int)GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); public int IndexOf(T item) => (int)GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer));
public long LongIndexOf(T item) => GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); public long LongIndexOf(T item) => GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer));
#pragma warning restore CS0618
public void Insert(int index, T item) => throw new NotSupportedException(); public void Insert(int index, T item) => throw new NotSupportedException();
public void RemoveAt(int index) => throw new NotSupportedException(); public void RemoveAt(int index) => throw new NotSupportedException();
public bool TryDequeue(out T? item) => TryDequeue(this, out item); public bool TryDequeue(out T? item) => TryDequeue(this, out item);
@@ -1,6 +1,8 @@
using FluentAssertions; using FluentAssertions;
using Recyclable.Collections; using Recyclable.Collections;
#pragma warning disable xUnit1026, RCS1163, IDE0060, RCS1235
namespace Recyclable.CollectionsTests namespace Recyclable.CollectionsTests
{ {
public class RecyclableListTests public class RecyclableListTests
@@ -1,6 +1,8 @@
using FluentAssertions; using FluentAssertions;
using Recyclable.Collections; using Recyclable.Collections;
#pragma warning disable xUnit1026, RCS1163, IDE0060, RCS1235
namespace Recyclable.CollectionsTests namespace Recyclable.CollectionsTests
{ {
public class RecyclableLongListTests public class RecyclableLongListTests
@@ -352,7 +354,7 @@ namespace Recyclable.CollectionsTests
[Theory] [Theory]
[MemberData(nameof(RecyclableLongListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableLongListTestData))] [MemberData(nameof(RecyclableLongListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableLongListTestData))]
//[InlineData("Debuging case", new[] { 1L, 2, }, 2, 2)] //[InlineData("Debugging case", new[] { 1L, 2, }, 2, 2)]
public void CopyToShouldCopyAllItemsInTheCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize) public void CopyToShouldCopyAllItemsInTheCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare