From 53d759586e2b4328df7019e487546b634cedebe3 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 13 May 2023 20:35:57 +0200 Subject: [PATCH] Small cleanup --- .../Benchmarks/Create.cs | 8 +- .../POC/ArraySizeLimitPoc.Benchmarks.cs | 10 +-- .../POC/PocBenchmarkBase.cs | 32 ++++---- Recyclable.Collections.Benchmarks/Program.cs | 4 +- Recyclable.Collections/ItemRange.cs | 2 +- .../ParallelSynchronizationContextPool.cs | 76 +++++++++---------- 6 files changed, 66 insertions(+), 66 deletions(-) diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs b/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs index 6c8e98b..710da7d 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/Create.cs @@ -4,25 +4,25 @@ namespace Recyclable.Collections.Benchmarks { public partial class RecyclableCollectionsBenchmarks { - public void List_Create() + public static void List_Create() { var list = new List(); DoNothing(list); } - public void PooledList_Create() + public static void PooledList_Create() { using var list = new PooledList(ClearMode.Auto); DoNothing(list); } - public void RecyclableArrayList_Create() + public static void RecyclableArrayList_Create() { using var list = new RecyclableArrayList(); DoNothing(list); } - public void RecyclableList_Create() + public static void RecyclableList_Create() { using var list = new RecyclableList(); DoNothing(list); diff --git a/Recyclable.Collections.Benchmarks/POC/ArraySizeLimitPoc.Benchmarks.cs b/Recyclable.Collections.Benchmarks/POC/ArraySizeLimitPoc.Benchmarks.cs index 960456e..e46231a 100644 --- a/Recyclable.Collections.Benchmarks/POC/ArraySizeLimitPoc.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/POC/ArraySizeLimitPoc.Benchmarks.cs @@ -14,7 +14,7 @@ namespace Recyclable.Collections.Benchmarks.POC { var benchmark = new ArraySizeLimitPocBenchmarks(); benchmark.Setup(); - benchmark.FindArraySizeLimit(); + FindArraySizeLimit(); benchmark.Cleanup(); } @@ -24,12 +24,12 @@ namespace Recyclable.Collections.Benchmarks.POC [Benchmark(OperationsPerInvoke = 1)] public void Max_Allowed_Size_2_147_483_605() { - long[] arr = ArrayPool.Shared.Rent(TestObjectCount); - DoNothing(arr); + long[] arr = ArrayPool.Shared.Rent(TestObjectCount); + DoNothing(arr); } [Benchmark(OperationsPerInvoke = 1)] - public void FindArraySizeLimit() + public static void FindArraySizeLimit() { var pool = ArrayPool.Shared; var bufferSize = int.MaxValue / 2; @@ -66,5 +66,5 @@ namespace Recyclable.Collections.Benchmarks.POC Console.WriteLine("All done"); throw new Exception($"Everything was successful. You can create {oldValue} byte big arrays. Exception is logged to prevent more benchmarks."); } - } + } } diff --git a/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs b/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs index 97d4c77..978be86 100644 --- a/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs +++ b/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs @@ -4,31 +4,31 @@ using System.Runtime.CompilerServices; namespace Recyclable.Collections.Benchmarks.POC { - public abstract class PocBenchmarkBase + public abstract class PocBenchmarkBase { public virtual TBenchmarkType? BenchmarkType { get; set; } public virtual TBenchmarkType? BaselineBenchmarkType { get; set; } private Action? _testMethod; - protected Action TestMethod - { - get => _testMethod ?? throw new OperationCanceledException("Skip - Custom benchmark run in progress"); - set => _testMethod = value; - } + protected Action TestMethod + { + get => _testMethod ?? throw new OperationCanceledException("Skip - Custom benchmark run in progress"); + set => _testMethod = value; + } private Action? _baselineMethod; - protected Action BaselineMethod - { - get => _baselineMethod ?? throw new OperationCanceledException("Skip - No baseline defined"); - set => _baselineMethod = value; - } - - [Benchmark] - public void RunWhen() => TestMethod.Invoke(); + protected Action BaselineMethod + { + get => _baselineMethod ?? throw new OperationCanceledException("Skip - No baseline defined"); + set => _baselineMethod = value; + } [Benchmark(Baseline = true)] public void Baseline() => BaselineMethod.Invoke(); + [Benchmark] + public void Actual() => TestMethod.Invoke(); + // 0 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524_288 // 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 // 1073741824 2147483648 @@ -64,12 +64,12 @@ namespace Recyclable.Collections.Benchmarks.POC protected static Exception CreateUnknownBenchmarkTypeException(T benchmarkType) { - return new InvalidOperationException($"******* UNKNOWN BENCHMARK TYPE {{{benchmarkType}}} *******"); + return new InvalidOperationException($"******* UNKNOWN BENCHMARK TYPE {{{benchmarkType}}} *******"); } protected static Exception CreateMethodNotFoundException(in string methodName, in string? className) { - return new MethodAccessException($"******* METHOD {{{methodName}}} NOT FOUND IN CLASS {{{className}}} *******"); + return new MethodAccessException($"******* METHOD {{{methodName}}} NOT FOUND IN CLASS {{{className}}} *******"); } protected virtual void PrepareData(T benchmarkType) { } diff --git a/Recyclable.Collections.Benchmarks/Program.cs b/Recyclable.Collections.Benchmarks/Program.cs index 43fc561..a835447 100644 --- a/Recyclable.Collections.Benchmarks/Program.cs +++ b/Recyclable.Collections.Benchmarks/Program.cs @@ -51,8 +51,8 @@ internal class Program private static void Main(string[] args) { RunRecyclableCollectionsBenchmarks(); - //RunPocBenchmarks(); - //RunSelectedBenchmarks(); + // RunPocBenchmarks(); + // RunSelectedBenchmarks(); //RunAssemblyBenchmarks(); } } diff --git a/Recyclable.Collections/ItemRange.cs b/Recyclable.Collections/ItemRange.cs index 4a1d273..892aa43 100644 --- a/Recyclable.Collections/ItemRange.cs +++ b/Recyclable.Collections/ItemRange.cs @@ -2,7 +2,7 @@ using Recyclable.Collections.Pools; namespace Recyclable.Collections { - public sealed class ItemRange + public sealed class ItemRange { public int BlockIndex; public int StartingItemIndex; diff --git a/Recyclable.Collections/Parallel/ParallelSynchronizationContextPool.cs b/Recyclable.Collections/Parallel/ParallelSynchronizationContextPool.cs index 88fa317..d4245bf 100644 --- a/Recyclable.Collections/Parallel/ParallelSynchronizationContextPool.cs +++ b/Recyclable.Collections/Parallel/ParallelSynchronizationContextPool.cs @@ -2,49 +2,49 @@ using Microsoft.Extensions.ObjectPool; namespace Recyclable.Collections.Parallel { - public static class ParallelSynchronizationContextPool - { - private static readonly SpinLock _updateLock = new(false); - public static readonly ObjectPool Shared = new DefaultObjectPool(new DefaultPooledObjectPolicy()); + public static class ParallelSynchronizationContextPool + { + private static readonly SpinLock _updateLock = new(false); + public static readonly ObjectPool Shared = new DefaultObjectPool(new DefaultPooledObjectPolicy()); - public static ParallelSynchronizationContext GetWithOneParticipant() - { - bool lockTaken = false; - _updateLock.Enter(ref lockTaken); - var context = Shared.Get(); - if (lockTaken) - { - _updateLock.Exit(false); - } + public static ParallelSynchronizationContext GetWithOneParticipant() + { + bool lockTaken = false; + _updateLock.Enter(ref lockTaken); + var context = Shared.Get(); + if (lockTaken) + { + _updateLock.Exit(false); + } - if (context.ItemFoundSignal._isSet) - { - context.ItemFoundSignal.Reset(); - } + if (context.ItemFoundSignal._isSet) + { + context.ItemFoundSignal.Reset(); + } - switch (context.AllDoneSignal.ParticipantsRemaining) - { - case > 1: - context.AllDoneSignal.RemoveParticipants(context.AllDoneSignal.ParticipantsRemaining - 1); - break; + switch (context.AllDoneSignal.ParticipantsRemaining) + { + case > 1: + context.AllDoneSignal.RemoveParticipants(context.AllDoneSignal.ParticipantsRemaining - 1); + break; - case 0: - context.AllDoneSignal.AddParticipant(); - break; - } + case 0: + _ = context.AllDoneSignal.AddParticipant(); + break; + } - if (context.Exception != null) - { - context.Exception = null; - } + if (context.Exception != null) + { + context.Exception = null; + } - if (context.FoundItemIndex >= 0) - { - context.FoundItemIndex = -1L; - } + if (context.FoundItemIndex >= 0) + { + context.FoundItemIndex = -1L; + } - context._returned = false; - return context; - } - } + context._returned = false; + return context; + } + } } \ No newline at end of file