-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArrayPoolBenchmarks.cs
More file actions
187 lines (166 loc) · 5.64 KB
/
ArrayPoolBenchmarks.cs
File metadata and controls
187 lines (166 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
using Platform.Collections.Arrays;
namespace Platform.Collections.Benchmarks
{
[SimpleJob]
[MemoryDiagnoser]
public class ArrayPoolBenchmarks
{
[Params(16, 64, 256, 1024, 4096, 16384)]
public int ArraySize { get; set; }
[Params(100, 1000, 10000)]
public int OperationCount { get; set; }
[Benchmark(Baseline = true)]
public void StandardArrayAllocation()
{
for (int i = 0; i < OperationCount; i++)
{
var array = new int[ArraySize];
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
}
[Benchmark]
public void PlatformArrayPool()
{
for (int i = 0; i < OperationCount; i++)
{
var array = ArrayPool.Allocate<int>(ArraySize);
try
{
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
finally
{
ArrayPool.Free(array);
}
}
}
[Benchmark]
public void SystemBuffersArrayPool()
{
var pool = System.Buffers.ArrayPool<int>.Shared;
for (int i = 0; i < OperationCount; i++)
{
var array = pool.Rent(ArraySize);
try
{
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
finally
{
pool.Return(array);
}
}
}
[Benchmark]
public void PlatformArrayPoolDisposable()
{
var platformPool = new Platform.Collections.Arrays.ArrayPool<int>();
for (int i = 0; i < OperationCount; i++)
{
using var disposable = platformPool.AllocateDisposable(ArraySize);
int[] array = disposable; // Implicit conversion
// Simulate some work
array[0] = i;
array[ArraySize - 1] = i;
}
}
[Benchmark]
public void PlatformArrayPoolReusabilityTest()
{
// Test how well the pool reuses arrays of the same size
var arrays = new int[10][];
for (int cycle = 0; cycle < OperationCount / 10; cycle++)
{
// Allocate multiple arrays
for (int i = 0; i < arrays.Length; i++)
{
arrays[i] = ArrayPool.Allocate<int>(ArraySize);
arrays[i][0] = i;
}
// Free them all
for (int i = 0; i < arrays.Length; i++)
{
ArrayPool.Free(arrays[i]);
}
}
}
[Benchmark]
public void SystemBuffersArrayPoolReusabilityTest()
{
// Test how well System.Buffers.ArrayPool reuses arrays
var pool = System.Buffers.ArrayPool<int>.Shared;
var arrays = new int[10][];
for (int cycle = 0; cycle < OperationCount / 10; cycle++)
{
// Rent multiple arrays
for (int i = 0; i < arrays.Length; i++)
{
arrays[i] = pool.Rent(ArraySize);
arrays[i][0] = i;
}
// Return them all
for (int i = 0; i < arrays.Length; i++)
{
pool.Return(arrays[i]);
}
}
}
[Benchmark]
public void PlatformArrayPoolInstanceReuse()
{
// Test behavior with a single instance
var pool = new Platform.Collections.Arrays.ArrayPool<int>();
for (int i = 0; i < OperationCount; i++)
{
var array1 = pool.Allocate(ArraySize);
var array2 = pool.Allocate(ArraySize);
array1[0] = i;
array2[0] = i;
pool.Free(array1);
pool.Free(array2);
}
}
[Benchmark]
public void PlatformArrayPoolVaryingSizes()
{
// Test performance with varying array sizes to stress the pool's size management
var sizes = new[] { 16, 64, 256, 1024, 16, 64, 256, 1024 };
for (int i = 0; i < OperationCount; i++)
{
var size = sizes[i % sizes.Length];
var array = ArrayPool.Allocate<int>(size);
array[0] = i;
ArrayPool.Free(array);
}
}
[Benchmark]
public void SystemBuffersArrayPoolVaryingSizes()
{
// Test System.Buffers.ArrayPool with varying sizes
var pool = System.Buffers.ArrayPool<int>.Shared;
var sizes = new[] { 16, 64, 256, 1024, 16, 64, 256, 1024 };
for (int i = 0; i < OperationCount; i++)
{
var size = sizes[i % sizes.Length];
var array = pool.Rent(size);
array[0] = i;
pool.Return(array);
}
}
[GlobalCleanup]
public void Cleanup()
{
// Note: Cannot access ThreadInstance from external assembly as it's internal
// Individual test instances will be garbage collected
}
}
}