ArraySizeLimitPocBenchmarks reworked.

This commit is contained in:
mlemanczyk committed 2023-04-11 20:38:25 +02:00
1 parent 098d862a3c
commit 9cb9aa8bf0
1 file changed
+23 -16
@@ -11,45 +11,52 @@ namespace Recyclable.Collections.Benchmarks.POC
benchmark.FindArraySizeLimit(); benchmark.FindArraySizeLimit();
} }
[Benchmark] private const int _maxArraySize = 2_147_483_605;
[Benchmark(OperationsPerInvoke = 1)]
public void Max_Allowed_Size_2_147_483_605()
{
var arr = ArrayPool<object>.Shared.Rent(_maxArraySize);
}
[Benchmark(OperationsPerInvoke = 1)]
public void FindArraySizeLimit() public void FindArraySizeLimit()
{ {
var pool = ArrayPool<object>.Shared; var pool = ArrayPool<object>.Shared;
var bufferSize = int.MaxValue; var bufferSize = int.MaxValue / 2;
var oldValue = 0; var oldValue = 0;
while (true) while (true)
{ {
try try
{ {
Console.WriteLine($"Allocating {bufferSize}"); Console.Write($"Allocating {bufferSize}...");
var arr = pool.Rent((int)bufferSize); var arr = pool.Rent((int)bufferSize);
var nextValue = (oldValue - bufferSize) >> 1; Console.WriteLine("success");
if (nextValue == 0)
{
break;
}
oldValue = bufferSize;
bufferSize += nextValue;
pool.Return(arr); pool.Return(arr);
arr = null;
var nextValue = bufferSize - oldValue;
oldValue = bufferSize;
bufferSize = checked(bufferSize + nextValue);
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true);
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine($"Error during array creation when {nameof(bufferSize)} = {bufferSize}"); Console.WriteLine($"error");
var nextValue = (oldValue - bufferSize) >> 1; var nextValue = (bufferSize - oldValue) >> 1;
if (nextValue == 0) if (nextValue == 0)
{ {
break; break;
} }
oldValue = bufferSize; bufferSize = oldValue + nextValue;
bufferSize -= nextValue;
} }
} }
Console.WriteLine("All done"); Console.WriteLine("All done");
throw new Exception($"Everything was successful. You can create {oldValue} byte big arrays. Exception is logged to prevent more benchmarks.");
} }
} }
} }