From 0ed8b5519c9e59bc828571390918f26993ace783 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Fri, 13 Nov 2026 13:06:41 +0100 Subject: [PATCH] ArrayPool: move to .NET 11, try/finally sample, reuse tests, in-process benchmark Targets net11.0 with BenchmarkDotNet 0.15.8, NUnit 5.0.0, NUnit3TestAdapter 6.3.1, NUnit.Analyzers 4.15.0, Microsoft.NET.Test.Sdk 18.12.0 and coverlet.collector 10.1.0. The sample returns the rented array in a finally block, prints the array size on its own line and shows that a second rent sees the old values. The benchmark adds a 16-element case, returns the new array, uses BenchmarkSwitcher and runs in-process. Four new tests cover the rented length, old values, clearArray and the custom pool limit. --- .../ArrayPool.Benchmark.csproj | 4 +- .../ArrayPool.Benchmark/Benchmark.cs | 19 ++--- .../ArrayPool/ArrayPool.Benchmark/Program.cs | 2 +- .../ArrayPool.Tests/ArrayPool.Tests.csproj | 12 +-- .../ArrayPool/ArrayPool.Tests/Tests.cs | 75 ++++++++++++++++--- .../ArrayPool/ArrayPool/ArrayPool.csproj | 2 +- .../ArrayPool/ArrayPool/Program.cs | 31 ++++++-- 7 files changed, 107 insertions(+), 38 deletions(-) diff --git a/collections-arrays/ArrayPool/ArrayPool.Benchmark/ArrayPool.Benchmark.csproj b/collections-arrays/ArrayPool/ArrayPool.Benchmark/ArrayPool.Benchmark.csproj index 91079d19c7..1a81128a7d 100644 --- a/collections-arrays/ArrayPool/ArrayPool.Benchmark/ArrayPool.Benchmark.csproj +++ b/collections-arrays/ArrayPool/ArrayPool.Benchmark/ArrayPool.Benchmark.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net11.0 enable enable - + diff --git a/collections-arrays/ArrayPool/ArrayPool.Benchmark/Benchmark.cs b/collections-arrays/ArrayPool/ArrayPool.Benchmark/Benchmark.cs index d64579eabb..b76e5b8755 100644 --- a/collections-arrays/ArrayPool/ArrayPool.Benchmark/Benchmark.cs +++ b/collections-arrays/ArrayPool/ArrayPool.Benchmark/Benchmark.cs @@ -3,19 +3,14 @@ namespace ArrayPool.Benchmark; +[InProcess] [MemoryDiagnoser] public class Benchmark { - private ArrayPool? _arrayPool; + private readonly ArrayPool _arrayPool = ArrayPool.Shared; - [Params(100, 1000, 10000, 100000)] - public int ArraySize { get; set; } - - [GlobalSetup] - public void GlobalSetup() - { - _arrayPool = ArrayPool.Shared; - } + [Params(16, 100, 1000, 10000, 100000)] + public int ArraySize { get; set; } [Benchmark] public void CreateArrayWithArrayPool() @@ -26,8 +21,10 @@ public void CreateArrayWithArrayPool() } [Benchmark] - public void CreateArrayNewArray() + public int[] CreateArrayNewArray() { var array = new int[ArraySize]; + + return array; } -} \ No newline at end of file +} diff --git a/collections-arrays/ArrayPool/ArrayPool.Benchmark/Program.cs b/collections-arrays/ArrayPool/ArrayPool.Benchmark/Program.cs index 5e29ed286f..d49ca0e1a2 100644 --- a/collections-arrays/ArrayPool/ArrayPool.Benchmark/Program.cs +++ b/collections-arrays/ArrayPool/ArrayPool.Benchmark/Program.cs @@ -1,4 +1,4 @@ using ArrayPool.Benchmark; using BenchmarkDotNet.Running; -BenchmarkRunner.Run(); \ No newline at end of file +BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).Run(args); diff --git a/collections-arrays/ArrayPool/ArrayPool.Tests/ArrayPool.Tests.csproj b/collections-arrays/ArrayPool/ArrayPool.Tests/ArrayPool.Tests.csproj index 577ef81799..3dea2d91a2 100644 --- a/collections-arrays/ArrayPool/ArrayPool.Tests/ArrayPool.Tests.csproj +++ b/collections-arrays/ArrayPool/ArrayPool.Tests/ArrayPool.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net11.0 enable enable @@ -9,11 +9,11 @@ - - - - - + + + + + diff --git a/collections-arrays/ArrayPool/ArrayPool.Tests/Tests.cs b/collections-arrays/ArrayPool/ArrayPool.Tests/Tests.cs index b6d9a8e8ab..371f5d7333 100644 --- a/collections-arrays/ArrayPool/ArrayPool.Tests/Tests.cs +++ b/collections-arrays/ArrayPool/ArrayPool.Tests/Tests.cs @@ -1,3 +1,5 @@ +using System.Buffers; + namespace ArrayPool.Tests; public class Tests @@ -7,7 +9,7 @@ public void UsingArrayPoolShared_WhenIntType_ThenSuccessfulInitialization() { var defaultPool = Methods.GetDefaultArrayPool(); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(defaultPool, Is.Not.Null); } [Test] @@ -15,7 +17,7 @@ public void UsingArrayPoolShared_WhenByteType_ThenSuccessfulInitialization() { var defaultPool = Methods.GetDefaultArrayPool(); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(defaultPool, Is.Not.Null); } [Test] @@ -23,30 +25,83 @@ public void UsingArrayPoolShared_WhenStringType_ThenSuccessfulInitialization() { var defaultPool = Methods.GetDefaultArrayPool(); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(defaultPool, Is.Not.Null); } [Test] public void UsingCustomArrayPool_WhenIntType_ThenSuccessfulInitialization() { - var defaultPool = Methods.CreateArrayPool(10, 5); + var customPool = Methods.CreateArrayPool(10, 5); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(customPool, Is.Not.Null); } [Test] public void UsingCustomArrayPool_WhenByteType_ThenSuccessfulInitialization() { - var defaultPool = Methods.CreateArrayPool(10, 5); + var customPool = Methods.CreateArrayPool(10, 5); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(customPool, Is.Not.Null); } [Test] public void UsingCustomArrayPool_WhenStringType_ThenSuccessfulInitialization() { - var defaultPool = Methods.CreateArrayPool(10, 5); + var customPool = Methods.CreateArrayPool(10, 5); + + Assert.That(customPool, Is.Not.Null); + } + + [Test] + public void UsingArrayPoolShared_WhenRentingTen_ThenArrayIsAtLeastTenLong() + { + var pool = Methods.GetDefaultArrayPool(); + + var array = pool.Rent(10); + + Assert.That(array, Has.Length.GreaterThanOrEqualTo(10)); + + pool.Return(array); + } + + [Test] + public void UsingCustomArrayPool_WhenArrayReturnedWithoutClearing_ThenNextRentSeesOldValues() + { + var pool = Methods.CreateArrayPool(100, 10); + var array = pool.Rent(10); + array[0] = 42; + pool.Return(array); + + var nextArray = pool.Rent(10); + + Assert.That(nextArray, Is.SameAs(array)); + Assert.That(nextArray[0], Is.EqualTo(42)); + } + + [Test] + public void UsingCustomArrayPool_WhenArrayReturnedWithClearArray_ThenNextRentSeesZeros() + { + var pool = Methods.CreateArrayPool(100, 10); + var array = pool.Rent(10); + array[0] = 42; + pool.Return(array, clearArray: true); + + var nextArray = pool.Rent(10); + + Assert.That(nextArray, Is.SameAs(array)); + Assert.That(nextArray[0], Is.EqualTo(0)); + } + + [Test] + public void UsingCustomArrayPool_WhenRentingAboveMaxLength_ThenArrayIsNotReused() + { + var pool = Methods.CreateArrayPool(100, 10); + var array = pool.Rent(200); + pool.Return(array); + + var nextArray = pool.Rent(200); - Assert.That(defaultPool, Is.Not.EqualTo(null)); + Assert.That(array, Has.Length.EqualTo(200)); + Assert.That(nextArray, Is.Not.SameAs(array)); } -} \ No newline at end of file +} diff --git a/collections-arrays/ArrayPool/ArrayPool/ArrayPool.csproj b/collections-arrays/ArrayPool/ArrayPool/ArrayPool.csproj index f02677bf64..5bf6c9b8cf 100644 --- a/collections-arrays/ArrayPool/ArrayPool/ArrayPool.csproj +++ b/collections-arrays/ArrayPool/ArrayPool/ArrayPool.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net11.0 enable enable diff --git a/collections-arrays/ArrayPool/ArrayPool/Program.cs b/collections-arrays/ArrayPool/ArrayPool/Program.cs index 5068538b11..4f79a46c6e 100644 --- a/collections-arrays/ArrayPool/ArrayPool/Program.cs +++ b/collections-arrays/ArrayPool/ArrayPool/Program.cs @@ -6,14 +6,31 @@ var array = pool.Rent(arraySize); -for (var i = 0; i < arraySize; i++) - array[i] = i * 2; +try +{ + for (var i = 0; i < arraySize; i++) + array[i] = i * 2; + + Console.WriteLine("Array elements:"); + + for (var i = 0; i < arraySize; i++) + Console.Write(array[i] + " "); -Console.WriteLine("Array elements:"); + Console.WriteLine(); + Console.WriteLine($"Array size: {array.Length}"); +} +finally +{ + pool.Return(array); +} -for (var i = 0; i < arraySize; i++) - Console.Write(array[i] + " "); +var nextArray = pool.Rent(arraySize); + +Console.WriteLine("Next rented array:"); + +for (var i = 0; i < arraySize; i++) + Console.Write(nextArray[i] + " "); -Console.WriteLine($"Array size: {array.Length}"); +Console.WriteLine(); -pool.Return(array); \ No newline at end of file +pool.Return(nextArray, clearArray: true);