forked from space-wizards/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentFetchBenchmark.cs
259 lines (222 loc) · 7.64 KB
/
ComponentFetchBenchmark.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
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
using Robust.Shared.Utility;
namespace Content.Benchmarks
{
[SimpleJob]
[Virtual]
public class ComponentFetchBenchmark
{
[Params(5000)] public int NEnt { get; set; }
private readonly Dictionary<(EntityUid, Type), BComponent>
_componentsFlat = new();
private readonly Dictionary<Type, Dictionary<EntityUid, BComponent>> _componentsPart =
new();
private UniqueIndex<Type, BComponent> _allComponents = new();
private readonly List<EntityUid> _lookupEntities = new();
[GlobalSetup]
public void Setup()
{
var random = new Random();
_componentsPart[typeof(BComponent1)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent2)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent3)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent4)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponentLookup)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent6)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent7)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent8)] = new Dictionary<EntityUid, BComponent>();
_componentsPart[typeof(BComponent9)] = new Dictionary<EntityUid, BComponent>();
for (var i = 0u; i < NEnt; i++)
{
var eId = new EntityUid(i);
if (random.Next(1) == 0)
{
_lookupEntities.Add(eId);
}
var comps = new List<BComponent>
{
new BComponent1(),
new BComponent2(),
new BComponent3(),
new BComponent4(),
new BComponent6(),
new BComponent7(),
new BComponent8(),
new BComponent9(),
};
if (random.Next(1000) == 0)
{
comps.Add(new BComponentLookup());
}
foreach (var comp in comps)
{
comp.Uid = eId;
var type = comp.GetType();
_componentsPart[type][eId] = comp;
_componentsFlat[(eId, type)] = comp;
_allComponents.Add(type, comp);
}
}
}
// These two benchmarks are find "needles in haystack" components.
// We try to look up a component that 0.1% of entities have on 1% of entities.
// Examples of this in the engine are VisibilityComponent lookups during PVS.
[Benchmark]
public void FindPart()
{
foreach (var entityUid in _lookupEntities)
{
var d = _componentsPart[typeof(BComponentLookup)];
d.TryGetValue(entityUid, out _);
}
}
[Benchmark]
public void FindFlat()
{
foreach (var entityUid in _lookupEntities)
{
_componentsFlat.TryGetValue((entityUid, typeof(BComponentLookup)), out _);
}
}
// Iteration benchmarks:
// We try to iterate every instance of a single component (BComponent1) and see which is faster.
[Benchmark]
public void IterPart()
{
var list = _componentsPart[typeof(BComponent1)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list.Values)
{
arr[i++] = c;
}
}
[Benchmark]
public void IterFlat()
{
var list = _allComponents[typeof(BComponent1)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list)
{
arr[i++] = c;
}
}
// We do the same as the iteration benchmarks but re-fetch the component every iteration.
// This is what entity systems mostly do via entity queries because crappy code.
[Benchmark]
public void IterFetchPart()
{
var list = _componentsPart[typeof(BComponent1)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list.Values)
{
var eId = c.Uid;
var d = _componentsPart[typeof(BComponent1)];
arr[i++] = d[eId];
}
}
[Benchmark]
public void IterFetchFlat()
{
var list = _allComponents[typeof(BComponent1)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list)
{
var eId = c.Uid;
arr[i++] = _componentsFlat[(eId, typeof(BComponent1))];
}
}
// Same as the previous benchmarks but with BComponentLookup instead.
// Which is only on 1% of entities.
[Benchmark]
public void IterFetchPartRare()
{
var list = _componentsPart[typeof(BComponentLookup)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list.Values)
{
var eId = c.Uid;
var d = _componentsPart[typeof(BComponentLookup)];
arr[i++] = d[eId];
}
}
[Benchmark]
public void IterFetchFlatRare()
{
var list = _allComponents[typeof(BComponentLookup)];
var arr = new BComponent[list.Count];
var i = 0;
foreach (var c in list)
{
var eId = c.Uid;
arr[i++] = _componentsFlat[(eId, typeof(BComponentLookup))];
}
}
private readonly struct EntityUid : IEquatable<EntityUid>
{
public readonly uint Value;
public EntityUid(uint value)
{
Value = value;
}
public bool Equals(EntityUid other)
{
return Value == other.Value;
}
public override bool Equals(object obj)
{
return obj is EntityUid other && Equals(other);
}
public override int GetHashCode()
{
return (int) Value;
}
public static bool operator ==(EntityUid left, EntityUid right)
{
return left.Equals(right);
}
public static bool operator !=(EntityUid left, EntityUid right)
{
return !left.Equals(right);
}
}
private abstract class BComponent
{
public EntityUid Uid;
}
private sealed class BComponent1 : BComponent
{
}
private sealed class BComponent2 : BComponent
{
}
private sealed class BComponent3 : BComponent
{
}
private sealed class BComponent4 : BComponent
{
}
private sealed class BComponentLookup : BComponent
{
}
private sealed class BComponent6 : BComponent
{
}
private sealed class BComponent7 : BComponent
{
}
private sealed class BComponent8 : BComponent
{
}
private sealed class BComponent9 : BComponent
{
}
}
}