-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
53 lines (43 loc) · 1.48 KB
/
Program.cs
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
// ArrayPool<T> Class
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1?view=netcore-3.0
using System.Buffers;
const int COLLECTION_SIZE = 1_000_000;
SharedPoolWithoutClearingArray();
SharedPoolWithClearingArray();
CustomPoolWithCaching();
// public abstract void Return (T[] array, bool clearArray = false);
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1.return?view=netcore-3.0
void SharedPoolWithoutClearingArray()
{
var sharedPool = ArrayPool<int>.Shared;
var array = sharedPool.Rent(COLLECTION_SIZE);
for (int i = 0; i < COLLECTION_SIZE; i++)
{
array[i] = i;
}
sharedPool.Return(array, false);
}
void SharedPoolWithClearingArray()
{
var sharedPool = ArrayPool<int>.Shared;
var array = sharedPool.Rent(COLLECTION_SIZE);
for (int i = 0; i < COLLECTION_SIZE; i++)
{
array[i] = i;
}
sharedPool.Return(array, true);
}
// ArrayPool<T>.Create Method
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1.create?view=net-8.0
// ArrayPool<T>.Rent(Int32) Method
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1.rent?view=net-8.0
void CustomPoolWithCaching()
{
ArrayPool<int> _cachedPool = ArrayPool<int>.Create();
var array = _cachedPool.Rent(COLLECTION_SIZE);
for (int i = 0; i < COLLECTION_SIZE; i++)
{
array[i] = i;
}
_cachedPool.Return(array, false);
}