forked from RMC-14/RMC-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityFetchBenchmark.cs
320 lines (255 loc) · 9.01 KB
/
EntityFetchBenchmark.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
319
320
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
using Robust.Shared.Utility;
namespace Content.Benchmarks
{
[SimpleJob]
[Virtual]
public class EntityFetchBenchmark
{
[Params(1000)] public int N { get; set; }
public int M { get; set; } = 10;
private readonly DictEntityStorage _dictStorage = new();
private readonly GenEntityStorage _genStorage = new();
private IEntityStorage<DictEntity, DictEntityUid> _dictStorageInterface;
private IEntityStorage<GenEntity, GenEntityUid> _genStorageInterface;
private DictEntityUid[] _toReadDict;
private DictEntity[] _toWriteDict;
private GenEntityUid[] _toReadGen;
private GenEntity[] _toWriteGen;
[GlobalSetup]
public void Setup()
{
_dictStorageInterface = _dictStorage;
_genStorageInterface = _genStorage;
var r = new Random();
var allocatedGen = new List<GenEntity>();
var allocatedDict = new List<DictEntity>();
for (var i = 0; i < N; i++)
{
allocatedGen.Add(_genStorage.NewEntity());
allocatedDict.Add(_dictStorage.NewEntity());
}
var delTo = N / 2;
for (var i = 0; i < delTo; i++)
{
var index = r.Next(allocatedDict.Count);
var gEnt = allocatedGen[index];
var dEnt = allocatedDict[index];
_genStorage.DeleteEntity(gEnt);
_dictStorage.DeleteEntity(dEnt);
allocatedGen.RemoveSwap(i);
allocatedDict.RemoveSwap(i);
}
for (var i = 0; i < N; i++)
{
allocatedGen.Add(_genStorage.NewEntity());
allocatedDict.Add(_dictStorage.NewEntity());
}
for (var i = 0; i < delTo; i++)
{
var index = r.Next(allocatedDict.Count);
var gEnt = allocatedGen[index];
var dEnt = allocatedDict[index];
_genStorage.DeleteEntity(gEnt);
_dictStorage.DeleteEntity(dEnt);
allocatedGen.RemoveSwap(i);
allocatedDict.RemoveSwap(i);
}
_toReadDict = new DictEntityUid[M];
_toWriteDict = new DictEntity[M];
_toReadGen = new GenEntityUid[M];
_toWriteGen = new GenEntity[M];
for (var i = 0; i < M; i++)
{
var index = r.Next(allocatedDict.Count);
_toReadDict[i] = allocatedDict[index].Uid;
_toReadGen[i] = allocatedGen[index].Uid;
}
}
[Benchmark]
public void BenchGenId()
{
for (var i = 0; i < M; i++)
{
var uid = _toReadGen[i];
if (_genStorage.TryGetEntity(uid, out var entity))
{
_toWriteGen[i] = entity;
}
}
}
[Benchmark]
public void BenchDict()
{
for (var i = 0; i < M; i++)
{
var uid = _toReadDict[i];
if (_dictStorage.TryGetEntity(uid, out var entity))
{
_toWriteDict[i] = entity;
}
}
}
[Benchmark]
public void BenchGenIdInterface()
{
for (var i = 0; i < M; i++)
{
var uid = _toReadGen[i];
if (_genStorageInterface.TryGetEntity(uid, out var entity))
{
_toWriteGen[i] = entity;
}
}
}
[Benchmark]
public void BenchDictInterface()
{
for (var i = 0; i < M; i++)
{
var uid = _toReadDict[i];
if (_dictStorageInterface.TryGetEntity(uid, out var entity))
{
_toWriteDict[i] = entity;
}
}
}
private sealed class DictEntityStorage : EntityStorage<DictEntity, DictEntityUid>
{
private int _nextValue;
private readonly Dictionary<DictEntityUid, DictEntity> _dict = new();
public override bool TryGetEntity(DictEntityUid entityUid, out DictEntity entity)
{
if (!_dict.TryGetValue(entityUid, out entity))
{
return false;
}
return !entity.Deleted;
}
public DictEntity NewEntity()
{
var e = new DictEntity(new DictEntityUid(_nextValue++));
_dict.Add(e.Uid, e);
return e;
}
public void DeleteEntity(DictEntity e)
{
DebugTools.Assert(!e.Deleted);
e.Deleted = true;
_dict.Remove(e.Uid);
}
}
private interface IEntityStorage<TEntity, TEntityUid>
{
public bool TryGetEntity(TEntityUid entityUid, out TEntity entity);
}
private abstract class EntityStorage<TEntity, TEntityUid> : IEntityStorage<TEntity, TEntityUid>
{
public abstract bool TryGetEntity(TEntityUid entityUid, out TEntity entity);
public TEntity GetEntity(TEntityUid entityUid)
{
if (!TryGetEntity(entityUid, out var entity))
throw new ArgumentException($"Failed to get entity {entityUid} from storage.");
return entity;
}
}
private sealed class GenEntityStorage : EntityStorage<GenEntity, GenEntityUid>
{
private (int generation, GenEntity entity)[] _entities = new (int, GenEntity)[1];
private readonly List<int> _availableSlots = new() { 0 };
public override bool TryGetEntity(GenEntityUid entityUid, out GenEntity entity)
{
var (generation, genEntity) = _entities[entityUid.Index];
entity = genEntity;
return generation == entityUid.Generation;
}
public GenEntity NewEntity()
{
if (_availableSlots.Count == 0)
{
// Reallocate
var oldEntities = _entities;
_entities = new (int, GenEntity)[_entities.Length * 2];
oldEntities.CopyTo(_entities, 0);
for (var i = oldEntities.Length; i < _entities.Length; i++)
{
_availableSlots.Add(i);
}
}
var index = _availableSlots.Pop();
ref var slot = ref _entities[index];
var slotEntity = new GenEntity(new GenEntityUid(slot.generation, index));
slot.entity = slotEntity;
return slotEntity;
}
public void DeleteEntity(GenEntity e)
{
DebugTools.Assert(!e.Deleted);
e.Deleted = true;
ref var slot = ref _entities[e.Uid.Index];
slot.entity = null;
slot.generation += 1;
_availableSlots.Add(e.Uid.Index);
}
}
private readonly struct DictEntityUid : IEquatable<DictEntityUid>
{
public readonly int Value;
public DictEntityUid(int value)
{
Value = value;
}
public bool Equals(DictEntityUid other)
{
return Value == other.Value;
}
public override bool Equals(object obj)
{
return obj is DictEntityUid other && Equals(other);
}
public override int GetHashCode()
{
return Value;
}
public static bool operator ==(DictEntityUid left, DictEntityUid right)
{
return left.Equals(right);
}
public static bool operator !=(DictEntityUid left, DictEntityUid right)
{
return !left.Equals(right);
}
}
private readonly struct GenEntityUid
{
public readonly int Generation;
public readonly int Index;
public GenEntityUid(int generation, int index)
{
Generation = generation;
Index = index;
}
}
private sealed class DictEntity
{
public DictEntity(DictEntityUid uid)
{
Uid = uid;
}
public DictEntityUid Uid { get; }
public bool Deleted { get; set; }
}
private sealed class GenEntity
{
public GenEntityUid Uid { get; }
public bool Deleted { get; set; }
public GenEntity(GenEntityUid uid)
{
Uid = uid;
}
}
}
}