Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
19 changes: 8 additions & 11 deletions collections-arrays/ArrayPool/ArrayPool.Benchmark/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

namespace ArrayPool.Benchmark;

[InProcess]
[MemoryDiagnoser]
public class Benchmark
{
private ArrayPool<int>? _arrayPool;
private readonly ArrayPool<int> _arrayPool = ArrayPool<int>.Shared;

[Params(100, 1000, 10000, 100000)]
public int ArraySize { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
_arrayPool = ArrayPool<int>.Shared;
}
[Params(16, 100, 1000, 10000, 100000)]
public int ArraySize { get; set; }

[Benchmark]
public void CreateArrayWithArrayPool()
Expand All @@ -26,8 +21,10 @@ public void CreateArrayWithArrayPool()
}

[Benchmark]
public void CreateArrayNewArray()
public int[] CreateArrayNewArray()
{
var array = new int[ArraySize];

return array;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ArrayPool.Benchmark;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<Benchmark>();
BenchmarkSwitcher.FromAssembly(typeof(Benchmark).Assembly).Run(args);
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.15.0" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
75 changes: 65 additions & 10 deletions collections-arrays/ArrayPool/ArrayPool.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Buffers;

namespace ArrayPool.Tests;

public class Tests
Expand All @@ -7,46 +9,99 @@ public void UsingArrayPoolShared_WhenIntType_ThenSuccessfulInitialization()
{
var defaultPool = Methods.GetDefaultArrayPool<int>();

Assert.That(defaultPool, Is.Not.EqualTo(null));
Assert.That(defaultPool, Is.Not.Null);
}

[Test]
public void UsingArrayPoolShared_WhenByteType_ThenSuccessfulInitialization()
{
var defaultPool = Methods.GetDefaultArrayPool<byte>();

Assert.That(defaultPool, Is.Not.EqualTo(null));
Assert.That(defaultPool, Is.Not.Null);
}

[Test]
public void UsingArrayPoolShared_WhenStringType_ThenSuccessfulInitialization()
{
var defaultPool = Methods.GetDefaultArrayPool<string>();

Assert.That(defaultPool, Is.Not.EqualTo(null));
Assert.That(defaultPool, Is.Not.Null);
}

[Test]
public void UsingCustomArrayPool_WhenIntType_ThenSuccessfulInitialization()
{
var defaultPool = Methods.CreateArrayPool<int>(10, 5);
var customPool = Methods.CreateArrayPool<int>(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<byte>(10, 5);
var customPool = Methods.CreateArrayPool<byte>(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<string>(10, 5);
var customPool = Methods.CreateArrayPool<string>(10, 5);

Assert.That(customPool, Is.Not.Null);
}

[Test]
public void UsingArrayPoolShared_WhenRentingTen_ThenArrayIsAtLeastTenLong()
{
var pool = Methods.GetDefaultArrayPool<int>();

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<int>(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<int>(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<int>(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));
}
}
}
2 changes: 1 addition & 1 deletion collections-arrays/ArrayPool/ArrayPool/ArrayPool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
31 changes: 24 additions & 7 deletions collections-arrays/ArrayPool/ArrayPool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
pool.Return(nextArray, clearArray: true);
Loading