-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathReadOnlySequence.cs
More file actions
100 lines (84 loc) · 3.7 KB
/
Copy pathReadOnlySequence.cs
File metadata and controls
100 lines (84 loc) · 3.7 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Memory
{
internal class BufferSegment<T> : ReadOnlySequenceSegment<T>
{
public BufferSegment(System.ReadOnlyMemory<T> memory) => Memory = memory;
public BufferSegment<T> Append(System.ReadOnlyMemory<T> memory)
{
var segment = new BufferSegment<T>(memory)
{
RunningIndex = RunningIndex + Memory.Length
};
Next = segment;
return segment;
}
}
[BenchmarkCategory(Categories.Libraries)]
public class ReadOnlySequence
{
public enum SequenceKind { Single, Multiple };
[Params(SequenceKind.Single, SequenceKind.Multiple)]
public SequenceKind Segment { get; set; }
private ReadOnlySequence<byte> _sequence;
private SequencePosition _start;
private SequencePosition _end;
[GlobalSetup]
public void GlobalSetup()
{
System.Memory<byte> memory = new System.Memory<byte>(Enumerable.Repeat((byte)1, 10000).ToArray());
if (Segment == SequenceKind.Single)
{
_sequence = new ReadOnlySequence<byte>(memory);
_start = _sequence.GetPosition(10);
_end = _sequence.GetPosition(9990);
}
else
{
BufferSegment<byte> firstSegment = new BufferSegment<byte>(memory.Slice(0, memory.Length / 2));
BufferSegment<byte> secondSegment = firstSegment.Append(memory.Slice(memory.Length / 2, memory.Length / 2));
_sequence = new ReadOnlySequence<byte>(firstSegment, 0, secondSegment, firstSegment.Memory.Length);
_start = _sequence.GetPosition(10);
_end = _sequence.GetPosition(9990);
}
}
[Benchmark]
public ReadOnlySequence<byte> Slice_StartPosition() => _sequence.Slice(_start);
[Benchmark]
public ReadOnlySequence<byte> Slice_Start() => _sequence.Slice(0);
[Benchmark]
public ReadOnlySequence<byte> Slice_Start_And_Length() => _sequence.Slice(0, 10000);
[Benchmark]
public ReadOnlySequence<byte> Slice_Start_And_EndPosition() => _sequence.Slice(0, _end);
[Benchmark]
public ReadOnlySequence<byte> Slice_StartPosition_And_Length() => _sequence.Slice(_start, 3);
[Benchmark]
public ReadOnlySequence<byte> Slice_StartPosition_And_EndPosition() => _sequence.Slice(_start, _end);
[Benchmark]
public ReadOnlySequence<byte> Slice_Repeat()
{
ReadOnlySequence<byte> localSequence = _sequence.Slice(0, 10000);
localSequence = localSequence.Slice(0, 5000);
localSequence = localSequence.Slice(0, 2500);
localSequence = localSequence.Slice(0, 1250);
localSequence = localSequence.Slice(0, 625);
return localSequence;
}
[Benchmark]
public ReadOnlySequence<byte> Slice_Repeat_StartPosition_And_EndPosition()
{
ReadOnlySequence<byte> localSequence = _sequence.Slice(_start, _end);
localSequence = localSequence.Slice(_start, localSequence.End);
localSequence = localSequence.Slice(_start, localSequence.End);
localSequence = localSequence.Slice(_start, localSequence.End);
localSequence = localSequence.Slice(_start, localSequence.End);
return localSequence;
}
}
}