From a0eb38a405c9d186b775d620dccbd69a3d90e5c3 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 1 Jul 2023 17:01:27 +0200 Subject: [PATCH] Cleanup. Hints and warnings either disabled or fixed. Typos fixed. --- README.md | 2 +- .../Benchmarks/Create.cs | 2 + .../POC/DelegateVsComparerPoc.Benchmarks.cs | 6 +- .../LessOperatorvsAndOperatorBenchmarks.cs | 9 ++- .../POC/ModuloPoc.Benchmarks.cs | 10 +-- ...leLongListPoc.Benchmarks.EnsureCapacity.cs | 17 +++-- ...ecyclableLongListPoc.Benchmarks.IndexOf.cs | 7 +- .../POC/RefVsInstanceMemberPoc.Benchmarks.cs | 2 + Recyclable.Collections.Benchmarks/Program.cs | 68 ++++++++++--------- .../RecyclableBenchmarkBase.cs | 2 +- Recyclable.Collections/MemoryBucket.cs | 2 + .../RecyclableLongList.Helpers.cs | 2 + Recyclable.Collections/RecyclableLongList.cs | 7 +- Recyclable.Collections/RecyclableQueue.cs | 21 +++--- .../RecyclableListTests.cs | 2 + .../RecyclableLongListTests.cs | 4 +- 16 files changed, 95 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 38f1748..e680832 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ 1. 🅿️ Implement `ILongList` interface 1. 🅿️ `RecyclableList` 1. 🅿️ `RecyclableLongList` -1. 🅿️ Implement `RecyclableQeueue` +1. 🅿️ Implement `RecyclableQueue` 1. 🅿️ Port `RecyclableLongList` optimizations to `RecyclableQueue` 1. 🅿️ Release 0.0.4 1. 🅿️ Implement `RecyclableStack` diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs index 7395de1..4a85c5a 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs @@ -1,5 +1,7 @@ using Collections.Pooled; +#pragma warning disable CA1822, CA1825 + namespace Recyclable.Collections.Benchmarks { public partial class RecyclableCollectionsBenchmarks diff --git a/Recyclable.Collections.Benchmarks/POC/DelegateVsComparerPoc.Benchmarks.cs b/Recyclable.Collections.Benchmarks/POC/DelegateVsComparerPoc.Benchmarks.cs index 09e18b1..fea010f 100644 --- a/Recyclable.Collections.Benchmarks/POC/DelegateVsComparerPoc.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/DelegateVsComparerPoc.Benchmarks.cs @@ -53,7 +53,7 @@ namespace Recyclable.Collections.Benchmarks.POC private struct MyComparerStruct : IComparer { - public int Compare(T? x, T? y) => Comparer.Default.Compare(x, y); + public readonly int Compare(T? x, T? y) => Comparer.Default.Compare(x, y); } private class MyComparer : IComparer @@ -132,7 +132,7 @@ namespace Recyclable.Collections.Benchmarks.POC } } - private void TestWithDelegate(Compare compareFunc) + private void TestWithDelegate(Compare compareFunc) { var testData = _testObjects!.ToArray(); for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++) @@ -177,7 +177,7 @@ namespace Recyclable.Collections.Benchmarks.POC public void Delegate() { - TestWithDelegate((x, y) => (x - y) switch + TestWithDelegate((x, y) => (x - y) switch { > 0 => 1, < 0 => -1, diff --git a/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs b/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs index 3683729..6781143 100644 --- a/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs @@ -1,12 +1,14 @@ using BenchmarkDotNet.Attributes; using System.Runtime.CompilerServices; +#pragma warning disable CA1822 + namespace Recyclable.Collections.Benchmarks.POC { public class LessOperatorVsAndOperatorBenchmarks { 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()); public void Setup() @@ -17,10 +19,13 @@ namespace Recyclable.Collections.Benchmarks.POC public void Cleanup() { } +#pragma warning disable RCS1163, IDE0060 [MethodImpl(MethodImplOptions.NoInlining)] private static void DoNothing(T item) - { } + { + } +#pragma warning restore RCS1163 [Benchmark(Baseline = true)] public void CompareWithLess() diff --git a/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs b/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs index 2fb2154..6923785 100644 --- a/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/ModuloPoc.Benchmarks.cs @@ -4,6 +4,8 @@ using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +#pragma warning disable xUnit1026, RCS1163, IDE0060 + namespace Recyclable.Collections.Benchmarks.POC { [MemoryDiagnoser] @@ -124,7 +126,7 @@ namespace Recyclable.Collections.Benchmarks.POC DoNothing(result, remainder); } - private void CalculateWithSubractWithWhileWithSse2(long i) + private void CalculateWithSubtractWithWhileWithSse2(long i) { var state = Vector128.Create(i, 0); int blockSize = BlockSize; @@ -137,7 +139,7 @@ namespace Recyclable.Collections.Benchmarks.POC DoNothing(state.GetElement(1), state.GetElement(0)); } - private void CalculateWithMixedApproachWithSubractWithWhile(long i) + private void CalculateWithMixedApproachWithSubtractWithWhile(long i) { long remainder; if (i >= _cutoff) @@ -317,7 +319,7 @@ namespace Recyclable.Collections.Benchmarks.POC long testNumber = TestNumber; for (long i = testNumber; i >= _lastCaseIndex; i--) { - CalculateWithSubractWithWhileWithSse2(i); + CalculateWithSubtractWithWhileWithSse2(i); } } @@ -327,7 +329,7 @@ namespace Recyclable.Collections.Benchmarks.POC long testNumber = TestNumber; for (long i = testNumber; i >= _lastCaseIndex; i--) { - CalculateWithMixedApproachWithSubractWithWhile(i); + CalculateWithMixedApproachWithSubtractWithWhile(i); } } diff --git a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs index 7358c77..91910eb 100644 --- a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs +++ b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.EnsureCapacity.cs @@ -4,6 +4,8 @@ using System.Collections; using System.Numerics; using System.Runtime.CompilerServices; +#pragma warning disable CA1825, RCS1085, CA2208 + namespace Recyclable.Collections.Benchmarks.POC { #region RecyclableLongList v1 @@ -26,7 +28,10 @@ namespace Recyclable.Collections.Benchmarks.POC protected ArrayPool _memoryBlocksPool; protected ArrayPool _blockArrayPool; + +#nullable disable protected T[][] _memoryBlocks; +#nullable restore private long _capacity; public long Capacity @@ -341,10 +346,10 @@ namespace Recyclable.Collections.Benchmarks.POC ? checked((long)BitOperations.RoundUpToPowerOf2((ulong)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) { - _blockSize = _memoryBlocks[0].Length; + _blockSize = _memoryBlocks![0].Length; _blockSizePow2Shift = MathUtils.GetPow2Shift(_blockSize); } @@ -361,8 +366,8 @@ namespace Recyclable.Collections.Benchmarks.POC if (expectedItemsCount > 0) { - _capacity = Resize(ref _memoryBlocks!, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); - if (_memoryBlocks.Length > 0) + _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); + if (_memoryBlocks!.Length > 0) { minBlockSize = _memoryBlocks[0].Length; } @@ -387,8 +392,8 @@ namespace Recyclable.Collections.Benchmarks.POC if (expectedItemsCount > 0) { - _capacity = Resize(ref _memoryBlocks!, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); - if (_memoryBlocks.Length > 0) + _capacity = Resize(ref _memoryBlocks, (int)BitOperations.RoundUpToPowerOf2((uint)minBlockSize), 0, expectedItemsCount.Value, memoryBlocksPool, blockArrayPool); + if (_memoryBlocks!.Length > 0) { minBlockSize = _memoryBlocks[0].Length; } diff --git a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.IndexOf.cs b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.IndexOf.cs index 59598e5..c003bf4 100644 --- a/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.IndexOf.cs +++ b/Recyclable.Collections.Benchmarks/POC/RecyclableLongListPoc.Benchmarks.IndexOf.cs @@ -6,6 +6,8 @@ using System.Collections; using System.Numerics; using System.Runtime.CompilerServices; +#pragma warning disable CA1825, RCS1085, CA2208 + namespace Recyclable.Collections.Benchmarks.POC { #region ItemRangesIterator V1 @@ -19,7 +21,6 @@ namespace Recyclable.Collections.Benchmarks.POC public ItemRangeV1() { - } public ItemRangeV1(int blockIndex, int startingItemIndex, long itemsToSearchCount) @@ -560,7 +561,7 @@ namespace Recyclable.Collections.Benchmarks.POC { break; } - }; + } } else { @@ -3802,7 +3803,7 @@ namespace Recyclable.Collections.Benchmarks.POC [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] private long DoIndexOfParallel(T item) - { + { var synchronizationContext = new ParallelSynchronizationContextV4(1); // (long)(_longCount * 0.329) => most efficient step, based on benchmarks Iterate(synchronizationContext, this, item, ScheduleIndexOfTasks); diff --git a/Recyclable.Collections.Benchmarks/POC/RefVsInstanceMemberPoc.Benchmarks.cs b/Recyclable.Collections.Benchmarks/POC/RefVsInstanceMemberPoc.Benchmarks.cs index bad353e..4219f49 100644 --- a/Recyclable.Collections.Benchmarks/POC/RefVsInstanceMemberPoc.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/RefVsInstanceMemberPoc.Benchmarks.cs @@ -1,5 +1,7 @@ using BenchmarkDotNet.Attributes; +#pragma warning disable RCS1085 + namespace Recyclable.Collections.Benchmarks.POC { public class RefVsInstanceMemberPocBenchmarks diff --git a/Recyclable.Collections.Benchmarks/Program.cs b/Recyclable.Collections.Benchmarks/Program.cs index a49f9a4..a604d56 100644 --- a/Recyclable.Collections.Benchmarks/Program.cs +++ b/Recyclable.Collections.Benchmarks/Program.cs @@ -1,39 +1,42 @@ using BenchmarkDotNet.Configs; using BenchmarkDotNet.Running; -using Recyclable.Collections.Benchmarks; using Recyclable.Collections.Benchmarks.POC; -public static class Program +#pragma warning disable RCS1036, RCS1213, IDE0051 + +namespace Recyclable.Collections.Benchmarks { - static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance) - .WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary ); - - static void RunAssemblyBenchmarks() + public static class Program { - _ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(new[] { "-f*" }); - } + static IConfig BenchmarkConfig { get; } = ManualConfig.Create(DefaultConfig.Instance) + .WithOptions(ConfigOptions.DisableOptimizationsValidator | ConfigOptions.JoinSummary); - private static void RunRecyclableCollectionsBenchmarks() - { - _ = BenchmarkRunner.Run(BenchmarkConfig); - } + static void RunAssemblyBenchmarks() + { + _ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(new[] { "-f*" }); + } - static void RunSelectedBenchmarks() - { - // **************** - // *** Template *** - // **************** - var benchmark = new RecyclableCollectionsBenchmarks(); - benchmark.Setup(); + private static void RunRecyclableCollectionsBenchmarks() + { + _ = BenchmarkRunner.Run(BenchmarkConfig); + } - // foreach (var _ in Enumerable.Range(1, 1000)) - // { - benchmark.List_ForEach(); - benchmark.RecyclableList_ForEach(); - // } + static void RunSelectedBenchmarks() + { + // **************** + // *** Template *** + // **************** + var benchmark = new RecyclableCollectionsBenchmarks(); + benchmark.Setup(); - benchmark.Cleanup(); - } + // foreach (var _ in Enumerable.Range(1, 1000)) + // { + benchmark.List_ForEach(); + benchmark.RecyclableList_ForEach(); + // } + + benchmark.Cleanup(); + } static void RunPocBenchmarks() { @@ -50,11 +53,12 @@ public static class Program // ArraySizeLimitPocBenchmarks>(BenchmarkConfig); } - static void Main(string[] args) - { - RunRecyclableCollectionsBenchmarks(); - // RunPocBenchmarks(); - // RunSelectedBenchmarks(); - // RunAssemblyBenchmarks(); + static void Main() + { + RunRecyclableCollectionsBenchmarks(); + // RunPocBenchmarks(); + // RunSelectedBenchmarks(); + // RunAssemblyBenchmarks(); + } } } diff --git a/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs b/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs index 602cd79..83f6eec 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableBenchmarkBase.cs @@ -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 List? _testObjectsAsList; - protected List TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wront and the field is not initialized"); + protected List TestObjectsAsList => _testObjectsAsList ?? throw new NullReferenceException("Something went wrong and the field is not initialized"); protected PooledList? _testObjectsAsPooledList; public PooledList TestObjectsAsPooledList => _testObjectsAsPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized"); diff --git a/Recyclable.Collections/MemoryBucket.cs b/Recyclable.Collections/MemoryBucket.cs index 720a33f..e2e49e4 100644 --- a/Recyclable.Collections/MemoryBucket.cs +++ b/Recyclable.Collections/MemoryBucket.cs @@ -13,10 +13,12 @@ namespace Recyclable.Collections _memory = memory; } +#pragma warning disable RCS1085 private T[] _memory; public T[] Memory { get => _memory; set => _memory = value; } private MemoryBucket? _nextBucket; public MemoryBucket? NextBucket { get => _nextBucket; set => _nextBucket = value; } +#pragma warning restore RCS1085 } } diff --git a/Recyclable.Collections/RecyclableLongList.Helpers.cs b/Recyclable.Collections/RecyclableLongList.Helpers.cs index b098f18..424f49e 100644 --- a/Recyclable.Collections/RecyclableLongList.Helpers.cs +++ b/Recyclable.Collections/RecyclableLongList.Helpers.cs @@ -271,7 +271,9 @@ namespace Recyclable.Collections : RecyclableArrayPool.Null; } +#pragma warning disable CA2208 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); } } diff --git a/Recyclable.Collections/RecyclableLongList.cs b/Recyclable.Collections/RecyclableLongList.cs index 6b54e2d..361393b 100644 --- a/Recyclable.Collections/RecyclableLongList.cs +++ b/Recyclable.Collections/RecyclableLongList.cs @@ -12,9 +12,12 @@ namespace Recyclable.Collections { private static readonly ArrayPool _defaultMemoryBlocksPool = ArrayPool.Create(); private static readonly ArrayPool _defaultBlockArrayPool = ArrayPool.Create(); +#pragma warning disable CA1825 private static readonly T[][] _emptyMemoryBlocksArray = 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 NeedsClearing = !typeof(T).IsValueType; internal int _blockSize; internal byte _blockSizePow2BitShift; @@ -23,9 +26,7 @@ namespace Recyclable.Collections internal int _nextItemIndex; private int _reservedBlockCount; - private static readonly bool NeedsClearing = !typeof(T).IsValueType; - - private ArrayPool _memoryBlocksPool; + private readonly ArrayPool _memoryBlocksPool; private ArrayPool _blockArrayPool; #nullable disable internal T[][] _memoryBlocks; diff --git a/Recyclable.Collections/RecyclableQueue.cs b/Recyclable.Collections/RecyclableQueue.cs index 8d1216e..b2bbbcd 100644 --- a/Recyclable.Collections/RecyclableQueue.cs +++ b/Recyclable.Collections/RecyclableQueue.cs @@ -95,18 +95,12 @@ namespace Recyclable.Collections 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)] private void SetItem(long index, T value) { long absoluteIndex = GetAbsoluteIndex(index, RemovedCount); - int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize); - long itemIndex = ToItemIndex(absoluteIndex, BlockSize); + int arrayIndex = RecyclableQueueHelpers.ToArrayIndex(absoluteIndex, BlockSize); + long itemIndex = RecyclableQueueHelpers.ToItemIndex(absoluteIndex, BlockSize); Memory[arrayIndex][itemIndex] = value; } @@ -114,8 +108,8 @@ namespace Recyclable.Collections private T GetItem(long index) { long absoluteIndex = GetAbsoluteIndex(index, RemovedCount); - int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize); - long itemIndex = ToItemIndex(absoluteIndex, BlockSize); + int arrayIndex = RecyclableQueueHelpers.ToArrayIndex(absoluteIndex, BlockSize); + long itemIndex = RecyclableQueueHelpers.ToItemIndex(absoluteIndex, BlockSize); return Memory[arrayIndex][itemIndex]; } @@ -156,10 +150,10 @@ namespace Recyclable.Collections { 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) +#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.Contains(Memory, item); public void CopyTo(T[] array, int arrayIndex) => RecyclableListHelpers.CopyTo(Memory, RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex); public IEnumerable Enumerate() => RecyclableListHelpers.Enumerate(Memory, BlockSize, LongCount); @@ -178,6 +173,8 @@ namespace Recyclable.Collections IEnumerator IEnumerable.GetEnumerator() => RecyclableListHelpers.Enumerate(Memory, BlockSize, LongCount).GetEnumerator(); public int IndexOf(T item) => (int)GetRelativeIndex(RecyclableListHelpers.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); public long LongIndexOf(T item) => GetRelativeIndex(RecyclableListHelpers.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); +#pragma warning restore CS0618 + public void Insert(int index, T item) => throw new NotSupportedException(); public void RemoveAt(int index) => throw new NotSupportedException(); public bool TryDequeue(out T? item) => TryDequeue(this, out item); diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index 28a5432..7f38864 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -1,6 +1,8 @@ using FluentAssertions; using Recyclable.Collections; +#pragma warning disable xUnit1026, RCS1163, IDE0060, RCS1235 + namespace Recyclable.CollectionsTests { public class RecyclableListTests diff --git a/Recyclable.CollectionsTests/RecyclableLongListTests.cs b/Recyclable.CollectionsTests/RecyclableLongListTests.cs index 2125171..e459c75 100644 --- a/Recyclable.CollectionsTests/RecyclableLongListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableLongListTests.cs @@ -1,6 +1,8 @@ using FluentAssertions; using Recyclable.Collections; +#pragma warning disable xUnit1026, RCS1163, IDE0060, RCS1235 + namespace Recyclable.CollectionsTests { public class RecyclableLongListTests @@ -352,7 +354,7 @@ namespace Recyclable.CollectionsTests [Theory] [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 testData, long itemsCount, int targetBlockSize) { // Prepare