-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrayPool.cs
More file actions
81 lines (68 loc) · 2 KB
/
MyArrayPool.cs
File metadata and controls
81 lines (68 loc) · 2 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
using System.Buffers;
using System.Collections.Concurrent;
using System.Numerics;
using System.Runtime.CompilerServices;
// [SkipLocalsInit]
static int ComputeSomething(byte[] array)
{
char[]? arrayPoolArray = null;
Span<char> tmp = array.Length < 256 ?
stackalloc char[array.Length] :
(arrayPoolArray = ArrayPool<char>.Shared.Rent(array.Length));
if (arrayPoolArray is not null)
{
ArrayPool<char>.Shared.Return(arrayPoolArray);
}
return 0;
}
static class MyArrayPool<T>
{
[ThreadStatic]
private static T[]?[] s_tls = new T[30][];
private static readonly ConcurrentQueue<T[]>[] s_arrays = Enumerable.Range(0, 30).Select(_ => new ConcurrentQueue<T[]>()).ToArray();
public static T[] Rent(int minimumLength)
{
// Array.MaxLength
ArgumentOutOfRangeException.ThrowIfNegative(minimumLength);
if (minimumLength == 0)
{
return Array.Empty<T>();
}
int index= BitOperations.Log2((uint) minimumLength - 1);
ref T[]? tls = ref s_tls[index];
if(tls is not null)
{
T[] tmpArray = tls;
tls = null;
return tmpArray;
}
ConcurrentQueue<T[]> queue = s_arrays[index];
if (queue.TryDequeue(out T[]? array))
{
return array;
}
return new T[BitOperations.RoundUpToPowerOf2((uint)minimumLength)];
}
public static void Return(T[] array)
{
ArgumentNullException.ThrowIfNull(array);
if (array.Length == 0)
{
return;
}
if (!BitOperations.IsPow2(array.Length))
{
// this array did not come from the pool
throw new Exception();
}
int index = BitOperations.Log2((uint) array.Length - 1);
ref T[]? tls = ref s_tls[index];
if(tls is null)
{
tls = array;
return;
}
ConcurrentQueue<T[]> queue = s_arrays[index];
queue.Enqueue(array);
}
}