-
Notifications
You must be signed in to change notification settings - Fork 272
/
ReadOnlySequenceTests.cs
240 lines (187 loc) · 10.6 KB
/
ReadOnlySequenceTests.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
using System.Linq;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Buffers.Tests
{
[BenchmarkCategory(Categories.Libraries)]
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(char))]
public class ReadOnlySequenceTests<T>
{
private const int Size = 10_000;
private T[] _array;
private BufferSegment<T> _startSegment, _endSegment;
private ReadOnlyMemory<T> _memory;
[GlobalSetup(Targets = new[] { nameof(IterateTryGetArray), nameof(IterateForEachArray), nameof(IterateGetPositionArray), nameof(FirstArray), "FirstSpanArray", nameof(SliceArray) })]
public void SetupArray() => _array = ValuesGenerator.Array<T>(Size);
[GlobalSetup(Targets = new[] { nameof(IterateTryGetSingleSegment), nameof(IterateForEachSingleSegment), nameof(IterateGetPositionSingleSegment), nameof(FirstSingleSegment), nameof(SliceSingleSegment), "FirstSpanSingleSegment" })]
public void SetupSingleSegment() => _startSegment = _endSegment = new BufferSegment<T>(new ReadOnlyMemory<T>(_array = ValuesGenerator.Array<T>(Size)));
[GlobalSetup(Targets = new[] { nameof(FirstMemory), nameof(SliceMemory), nameof(IterateTryGetMemory), nameof(IterateForEachMemory), nameof(IterateGetPositionMemory), "FirstSpanMemory" })]
public void SetupMemorySegment() => _memory = new ReadOnlyMemory<T>(_array = ValuesGenerator.Array<T>(Size));
[GlobalSetup(Targets = new[] { nameof(IterateTryGetTenSegments), nameof(IterateForEachTenSegments), nameof(IterateGetPositionTenSegments), nameof(FirstTenSegments), nameof(SliceTenSegments), "FirstSpanTenSegments" })]
public void SetupTenSegments()
{
const int segmentsCount = 10;
const int segmentSize = Size / segmentsCount;
SetupArray();
_startSegment = new BufferSegment<T>(new ReadOnlyMemory<T>(_array.Take(segmentSize).ToArray()));
_endSegment = _startSegment;
for (int i = 1; i < segmentsCount; i++)
{
_endSegment = _endSegment.Append(new ReadOnlyMemory<T>(_array.Skip(i * segmentSize).Take(Size / segmentsCount).ToArray()));
}
}
[Benchmark]
public int IterateTryGetArray() => IterateTryGet(new ReadOnlySequence<T>(_array));
[Benchmark]
public int IterateForEachArray() => IterateForEach(new ReadOnlySequence<T>(_array));
[Benchmark]
public int IterateGetPositionArray() => IterateGetPosition(new ReadOnlySequence<T>(_array));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstArray() => First(new ReadOnlySequence<T>(_array));
#if !NETFRAMEWORK
[Benchmark(OperationsPerInvoke = 16)]
public int FirstSpanArray() => FirstSpan(new ReadOnlySequence<T>(_array));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstSpanMemory() => FirstSpan(new ReadOnlySequence<T>(_memory));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstSpanSingleSegment()
=> FirstSpan(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstSpanTenSegments()
=> FirstSpan(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[MethodImpl(MethodImplOptions.NoInlining)]
private int FirstSpan(ReadOnlySequence<T> sequence)
{
int consume = 0;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
consume += sequence.FirstSpan.Length; consume += sequence.FirstSpan.Length;
return consume;
}
#endif
[Benchmark(OperationsPerInvoke = 10)]
public long SliceArray() => Slice(new ReadOnlySequence<T>(_array));
[Benchmark]
public int IterateTryGetMemory() => IterateTryGet(new ReadOnlySequence<T>(_memory));
[Benchmark]
public int IterateForEachMemory() => IterateForEach(new ReadOnlySequence<T>(_memory));
[Benchmark]
public int IterateGetPositionMemory() => IterateGetPosition(new ReadOnlySequence<T>(_memory));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstMemory() => First(new ReadOnlySequence<T>(_memory));
[Benchmark(OperationsPerInvoke = 10)]
public long SliceMemory() => Slice(new ReadOnlySequence<T>(_memory));
[Benchmark]
public int IterateTryGetSingleSegment()
=> IterateTryGet(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark]
public int IterateForEachSingleSegment()
=> IterateForEach(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark]
public int IterateGetPositionSingleSegment()
=> IterateGetPosition(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstSingleSegment()
=> First(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark(OperationsPerInvoke = 10)]
public long SliceSingleSegment()
=> Slice(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size));
[Benchmark]
public int IterateTryGetTenSegments()
=> IterateTryGet(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[Benchmark]
public int IterateForEachTenSegments()
=> IterateForEach(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[Benchmark]
public int IterateGetPositionTenSegments()
=> IterateGetPosition(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[Benchmark(OperationsPerInvoke = 16)]
public int FirstTenSegments()
=> First(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[Benchmark(OperationsPerInvoke = 10)]
public long SliceTenSegments()
=> Slice(new ReadOnlySequence<T>(startSegment: _startSegment, startIndex: 0, endSegment: _endSegment, endIndex: Size / 10));
[MethodImpl(MethodImplOptions.NoInlining)] // make sure that the method does not get inlined for any of the benchmarks and we compare apples to apples
private int IterateTryGet(ReadOnlySequence<T> sequence)
{
int consume = 0;
SequencePosition position = sequence.Start;
while (sequence.TryGet(ref position, out var memory))
consume += memory.Length;
return consume;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int IterateForEach(ReadOnlySequence<T> sequence)
{
int consume = 0;
foreach (ReadOnlyMemory<T> memory in sequence)
consume += memory.Length;
return consume;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int IterateGetPosition(ReadOnlySequence<T> sequence)
{
int consume = 0;
SequencePosition position = sequence.Start;
int offset = (int)(sequence.Length / 10);
SequencePosition end = sequence.GetPosition(0, sequence.End);
while (!position.Equals(end))
{
position = sequence.GetPosition(offset, position);
consume += position.GetInteger();
}
return consume;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int First(ReadOnlySequence<T> sequence)
{
int consume = 0;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
consume += sequence.First.Length; consume += sequence.First.Length;
return consume;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private long Slice(ReadOnlySequence<T> sequence)
{
long consume = 0;
consume += sequence.Slice(Size / 10 * 0, Size / 10).Length; // 1
consume += sequence.Slice(Size / 10 * 1, Size / 10).Length; // 2
consume += sequence.Slice(Size / 10 * 2, Size / 10).Length; // 3
consume += sequence.Slice(Size / 10 * 3, Size / 10).Length; // 4
consume += sequence.Slice(Size / 10 * 4, Size / 10).Length; // 5
consume += sequence.Slice(Size / 10 * 5, Size / 10).Length; // 6
consume += sequence.Slice(Size / 10 * 6, Size / 10).Length; // 7
consume += sequence.Slice(Size / 10 * 7, Size / 10).Length; // 8
consume += sequence.Slice(Size / 10 * 8, Size / 10).Length; // 9
consume += sequence.Slice(Size / 10 * 9, Size / 10).Length; // 10
return consume;
}
}
internal class BufferSegment<T> : ReadOnlySequenceSegment<T>
{
public BufferSegment(ReadOnlyMemory<T> memory) => Memory = memory;
public BufferSegment<T> Append(ReadOnlyMemory<T> memory)
{
var segment = new BufferSegment<T>(memory)
{
RunningIndex = RunningIndex + Memory.Length
};
Next = segment;
return segment;
}
}
}