-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathFilterImmutable.cs
More file actions
130 lines (103 loc) · 3.58 KB
/
Copy pathFilterImmutable.cs
File metadata and controls
130 lines (103 loc) · 3.58 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
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
namespace DynamicData.Benchmarks.Cache;
[MemoryDiagnoser]
[MarkdownExporterAttribute.GitHub]
public class FilterImmutable
{
private readonly IReadOnlyList<IChangeSet<Item, int>> _addChangeSets;
private readonly IReadOnlyList<IChangeSet<Item, int>> _replaceChangeSets;
private readonly IReadOnlyList<IChangeSet<Item, int>> _removeChangeSets;
public FilterImmutable()
{
var source = new ChangeAwareCache<Item, int>(capacity: 1_000);
var addChangeSets = new List<IChangeSet<Item, int>>(capacity: 1_000);
for (var id = 1; id <= 1_000; ++id)
{
source.Add(
item: new Item()
{
Id = id,
IsIncluded = (id % 2) == 0
},
key: id);
addChangeSets.Add(source.CaptureChanges());
}
_addChangeSets = addChangeSets;
var replaceChangeSets = new List<IChangeSet<Item, int>>(capacity: 500);
for (var id = 2; id <= 1_000; id += 2)
{
source.AddOrUpdate(
item: new Item()
{
Id = id,
IsIncluded = (id % 4) != 0
},
key: id);
replaceChangeSets.Add(source.CaptureChanges());
}
_replaceChangeSets = replaceChangeSets;
var removeChangeSets = new List<IChangeSet<Item, int>>(capacity: 1_000);
for (var id = 1; id <= 1_000; ++id)
{
source.Remove(id);
removeChangeSets.Add(source.CaptureChanges());
}
_removeChangeSets = removeChangeSets;
}
[Benchmark]
public void Adds()
{
using var source = new Signal<IChangeSet<Item, int>>();
using var subscription = source
.FilterImmutable(static item => item.IsIncluded)
.Subscribe();
foreach (var changeSet in _addChangeSets)
source.OnNext(changeSet);
source.OnCompleted();
}
[Benchmark]
public void AddsAndReplacements()
{
using var source = new Signal<IChangeSet<Item, int>>();
using var subscription = source
.FilterImmutable(static item => item.IsIncluded)
.Subscribe();
foreach (var changeSet in _addChangeSets)
source.OnNext(changeSet);
foreach (var changeSet in _replaceChangeSets)
source.OnNext(changeSet);
source.OnCompleted();
}
[Benchmark]
public void AddsAndRemoves()
{
using var source = new Signal<IChangeSet<Item, int>>();
using var subscription = source
.FilterImmutable(static item => item.IsIncluded)
.Subscribe();
foreach (var changeSet in _addChangeSets)
source.OnNext(changeSet);
foreach (var changeSet in _removeChangeSets)
source.OnNext(changeSet);
source.OnCompleted();
}
[Benchmark]
public void AddsReplacementsAndRemoves()
{
using var source = new Signal<IChangeSet<Item, int>>();
using var subscription = source
.FilterImmutable(static item => item.IsIncluded)
.Subscribe();
foreach (var changeSet in _addChangeSets)
source.OnNext(changeSet);
foreach (var changeSet in _replaceChangeSets)
source.OnNext(changeSet);
foreach (var changeSet in _removeChangeSets)
source.OnNext(changeSet);
source.OnCompleted();
}
private sealed class Item
{
public required int Id { get; init; }
public required bool IsIncluded { get; init; }
}
}