-
Notifications
You must be signed in to change notification settings - Fork 10
/
ValueListPool.cs
318 lines (269 loc) · 9.49 KB
/
ValueListPool.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace ListPool
{
/// <summary>
/// High-performance implementation of IList with zero heap allocations.
/// </summary>
/// <typeparam name="T"></typeparam>
public ref struct ValueListPool<T> where T : IEquatable<T>
{
public enum SourceType
{
UseAsInitialBuffer,
UseAsReferenceData,
Copy
}
private const int MinimumCapacity = 16;
private T[] _disposableBuffer;
private Span<T> _buffer;
/// <summary>
/// Construct ValueListPool with the indicated capacity.
/// </summary>
/// <param name="capacity">Required initial capacity</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValueListPool(int capacity)
{
_disposableBuffer = ArrayPool<T>.Shared.Rent(capacity < MinimumCapacity ? MinimumCapacity : capacity);
_buffer = _disposableBuffer;
Count = 0;
}
/// <summary>
/// Construct the ValueListPool using the giving source.
/// It can use the source as initial buffer in order to reuse the array or
/// use the data and wrapper it inside the ListPool or copy the data into new pooled array.
/// </summary>
/// <param name="source">source/buffer</param>
/// <param name="sourceType">Action to take with the source</param>
public ValueListPool(Span<T> source, SourceType sourceType)
{
if (sourceType == SourceType.UseAsInitialBuffer)
{
_buffer = source;
Count = 0;
_disposableBuffer = null;
}
else if (sourceType == SourceType.UseAsReferenceData)
{
_buffer = source;
Count = source.Length;
_disposableBuffer = null;
}
else
{
T[] disposableBuffer =
ArrayPool<T>.Shared.Rent(source.Length > MinimumCapacity ? source.Length : MinimumCapacity);
source.CopyTo(disposableBuffer);
_buffer = disposableBuffer;
_disposableBuffer = disposableBuffer;
Count = source.Length;
}
}
/// <summary>
/// Capacity of the underlying array.
/// </summary>
public int Capacity => _buffer.Length;
/// <summary>
/// Returns underlying array to the pool
/// </summary>
public void Dispose()
{
Count = 0;
T[] buffer = _disposableBuffer;
if (buffer != null)
ArrayPool<T>.Shared.Return(buffer);
}
/// <summary>
/// Count of items added.
/// </summary>
public int Count { get; private set; }
public bool IsReadOnly => false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
{
Span<T> buffer = _buffer;
int count = Count;
if (count < buffer.Length)
{
buffer[count] = item;
Count = count + 1;
}
else
{
AddWithResize(item);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear() => Count = 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Contains(T item) => IndexOf(item) > -1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly int IndexOf(T item) => _buffer.Slice(0, Count).IndexOf(item);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(Span<T> array) => _buffer.Slice(0, Count).CopyTo(array);
public bool Remove(T item)
{
if (item is null) return false;
int index = IndexOf(item);
if (index == -1) return false;
RemoveAt(index);
return true;
}
public void Insert(int index, T item)
{
int count = Count;
Span<T> buffer = _buffer;
if (buffer.Length == count)
{
int newCapacity = count * 2;
GrowBuffer(newCapacity);
buffer = _buffer;
}
if (index < count)
{
buffer.Slice(index, count).CopyTo(buffer.Slice(index + 1));
buffer[index] = item;
Count++;
}
else if (index == count)
{
buffer[index] = item;
Count++;
}
else throw new ArgumentOutOfRangeException(nameof(index));
}
public void RemoveAt(int index)
{
int count = Count;
Span<T> buffer = _buffer;
if (index >= count) throw new IndexOutOfRangeException(nameof(index));
count--;
buffer.Slice(index + 1).CopyTo(buffer.Slice(index));
Count = count;
}
#if NETSTANDARD2_1
[MaybeNull]
#endif
public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (index >= Count)
throw new IndexOutOfRangeException(nameof(index));
return ref _buffer[index];
}
}
public void AddRange(ReadOnlySpan<T> items)
{
int count = Count;
Span<T> buffer = _buffer;
bool isCapacityEnough = buffer.Length - items.Length - count > 0;
if (!isCapacityEnough)
{
GrowBuffer(buffer.Length + items.Length);
buffer = _disposableBuffer;
}
items.CopyTo(buffer.Slice(count));
Count += items.Length;
}
public void AddRange(T[] array)
{
int count = Count;
T[] disposableBuffer = _disposableBuffer;
Span<T> buffer = _buffer;
bool isCapacityEnough = buffer.Length - array.Length - count > 0;
if (!isCapacityEnough)
{
GrowBuffer(buffer.Length + array.Length);
disposableBuffer = _disposableBuffer;
array.CopyTo(disposableBuffer, count);
Count += array.Length;
return;
}
if (disposableBuffer != null)
{
array.CopyTo(disposableBuffer, count);
}
else
{
array.AsSpan().CopyTo(buffer.Slice(count));
}
Count += array.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<T> AsSpan() => _buffer.Slice(0, Count);
[MethodImpl(MethodImplOptions.NoInlining)]
private void AddWithResize(T item)
{
ArrayPool<T> arrayPool = ArrayPool<T>.Shared;
if (_disposableBuffer == null)
{
Span<T> oldBuffer = _buffer;
int newSize = oldBuffer.Length * 2;
T[] newBuffer = arrayPool.Rent(newSize > MinimumCapacity ? newSize : MinimumCapacity);
oldBuffer.CopyTo(newBuffer);
newBuffer[oldBuffer.Length] = item;
_disposableBuffer = newBuffer;
_buffer = newBuffer;
Count++;
}
else
{
T[] oldBuffer = _disposableBuffer;
T[] newBuffer = arrayPool.Rent(oldBuffer.Length * 2);
int count = oldBuffer.Length;
Array.Copy(oldBuffer, 0, newBuffer, 0, count);
newBuffer[count] = item;
_disposableBuffer = newBuffer;
_buffer = newBuffer;
Count = count + 1;
arrayPool.Return(oldBuffer);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowBuffer(int capacity)
{
ArrayPool<T> arrayPool = ArrayPool<T>.Shared;
T[] newBuffer = arrayPool.Rent(capacity);
Span<T> oldBuffer = _buffer;
oldBuffer.CopyTo(newBuffer);
_buffer = newBuffer;
if (_disposableBuffer != null)
{
arrayPool.Return(_disposableBuffer);
}
_disposableBuffer = newBuffer;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Enumerator GetEnumerator() => new Enumerator(_buffer.Slice(0, Count));
public ref struct Enumerator
{
private readonly Span<T> _source;
private int _index;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator(Span<T> source)
{
_source = source;
_index = -1;
}
#if NETSTANDARD2_1
[MaybeNull]
#endif
public readonly ref T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _source[_index];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => unchecked(++_index < _source.Length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
_index = -1;
}
}
}
}