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

This commit is contained in:
mlemanczyk committed 2023-07-01 17:01:27 +02:00
1 parent 2317f53d51
commit a0eb38a405
16 files changed
+95 -68

No files matched your search

+1 -1
View File
@@ -143,7 +143,7 @@
1. 🅿️ Implement `ILongList<T>` interface
1. 🅿️ `RecyclableList<T>`
1. 🅿️ `RecyclableLongList<T>`
1. 🅿️ Implement `RecyclableQeueue<T>`
1. 🅿️ Implement `RecyclableQueue<T>`
1. 🅿️ Port `RecyclableLongList<T>` optimizations to `RecyclableQueue<T>`
1. 🅿️ Release 0.0.4
1. 🅿️ Implement `RecyclableStack<T>`
@@ -1,5 +1,7 @@
using Collections.Pooled;
#pragma warning disable CA1822, CA1825
namespace Recyclable.Collections.Benchmarks
{
public partial class RecyclableCollectionsBenchmarks
@@ -53,7 +53,7 @@ namespace Recyclable.Collections.Benchmarks.POC
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>
@@ -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();
for (var itemIdx = 0; itemIdx < testData.Length - 1; itemIdx++)
@@ -177,7 +177,7 @@ namespace Recyclable.Collections.Benchmarks.POC
public void Delegate()
{
TestWithDelegate<long>((x, y) => (x - y) switch
TestWithDelegate((x, y) => (x - y) switch
{
> 0 => 1,
< 0 => -1,
@@ -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>(T item)
{ }
{
}
#pragma warning restore RCS1163
[Benchmark(Baseline = true)]
public void CompareWithLess()
@@ -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);
}
}
@@ -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<T[]> _memoryBlocksPool;
protected ArrayPool<T> _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;
}
@@ -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);
@@ -1,5 +1,7 @@
using BenchmarkDotNet.Attributes;
#pragma warning disable RCS1085
namespace Recyclable.Collections.Benchmarks.POC
{
public class RefVsInstanceMemberPocBenchmarks
+36 -32
View File
@@ -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<RecyclableCollectionsBenchmarks>(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<RecyclableCollectionsBenchmarks>(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();
}
}
}
@@ -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<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;
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;
}
#pragma warning disable RCS1085
private T[] _memory;
public T[] Memory { get => _memory; set => _memory = value; }
private MemoryBucket<T>? _nextBucket;
public MemoryBucket<T>? NextBucket { get => _nextBucket; set => _nextBucket = value; }
#pragma warning restore RCS1085
}
}
@@ -271,7 +271,9 @@ namespace Recyclable.Collections
: RecyclableArrayPool<T>.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);
}
}
+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> _defaultBlockArrayPool = ArrayPool<T>.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<T[]> _memoryBlocksPool;
private readonly ArrayPool<T[]> _memoryBlocksPool;
private ArrayPool<T> _blockArrayPool;
#nullable disable
internal T[][] _memoryBlocks;
+9 -12
View File
@@ -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<T>.Contains(Memory, item);
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);
@@ -178,6 +173,8 @@ namespace Recyclable.Collections
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 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 RemoveAt(int index) => throw new NotSupportedException();
public bool TryDequeue(out T? item) => TryDequeue(this, out item);
@@ -1,6 +1,8 @@
using FluentAssertions;
using Recyclable.Collections;
#pragma warning disable xUnit1026, RCS1163, IDE0060, RCS1235
namespace Recyclable.CollectionsTests
{
public class RecyclableListTests
@@ -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<long> testData, long itemsCount, int targetBlockSize)
{
// Prepare