-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDictAccessTests.cs
310 lines (270 loc) · 9.03 KB
/
DictAccessTests.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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.Profiling;
using UnityEngine.TestTools.Constraints;
using Is = UnityEngine.TestTools.Constraints.Is;
namespace MH.GCTests
{
public class DictAccess
{
public struct Data : IEquatable<Data>
{
public int x;
public int y;
public Data(int x_, int y_) { x = x_; y = y_; }
public bool Equals(Data other)
{
return this.x == other.x && this.y == other.y;
}
}
public class CData : IEquatable<CData>
{
public int x;
public int y;
public CData(int x_, int y_) { x = x_; y = y_; }
public bool Equals(CData other)
{
return this.x == other.x && this.y == other.y;
}
}
public class AData : IEquatable<AData>
{
public int x;
public int y;
public AData(int x_, int y_) { x = x_; y = y_; }
// public override bool Equals(object obj)
// {
// if ( object.ReferenceEquals(this, obj) )
// return true;
// if (! (obj is AData) )
// return false;
// var o = (AData)obj;
// return x == o.x && y == o.y;
// }
public bool Equals(AData o) //this will do too
{
return x == o.x && y == o.y;
}
public override int GetHashCode()
{
return x << 16 + y;
}
}
const int ELEM_COUNT = 1000;
const int LOOP_COUNT = 1000; //don't know why, but if the mem allocation is too small, the profiler.GetMonoUsedSizeLong cannot detect it
[OneTimeSetUp]
public void Init()
{
GC.Collect();
Profiler.enabled = true;
}
[OneTimeTearDown]
public void Fini()
{
Profiler.enabled = false;
}
///<summary>
/// Without comparer, it will cause boxing when compare keys
///
/// it seems very slow to access struct keys without explicit comparer
///</summary>
[Test]
public void BAD_AccessStructKey()
{
var cache = new Dictionary<Data, int>();
for(int i=0; i<ELEM_COUNT; ++i)
cache.Add(new Data(i, i), i);
Assert.That( () =>
{
int v = 0;
for(int j=0; j<ELEM_COUNT; ++j)
v += cache[new Data(j,j)];
},
Is.AllocatingGCMemory()
);
// GC.Collect();
// long startMem = Profiler.GetMonoUsedSizeLong();
// //------------------//
// for(int i=0; i<LOOP_COUNT; ++i)
// {
// cont.Clear();
// for(int j=0; j<ELEM_COUNT; ++j)
// {
// cont.Add(cache[new Data(j,j)]);
// }
// }
// long mem1 = Profiler.GetMonoUsedSizeLong();
// Assert.That(mem1, Is.GreaterThan(startMem));
// //------------------//
// Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
}
[Test]
public void OK_AccessVector3Key()
{
var cache = new Dictionary<Vector3, int>();
for(int i=0; i<ELEM_COUNT; ++i)
cache.Add(new Vector3(i, i, i), i);
Assert.That( () =>
{
int v = 0;
for(int j=0; j<ELEM_COUNT; ++j)
v += cache[new Vector3(j,j,j)];
},
Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_AccessIntKey()
{
var cache = new Dictionary<int, int>();
for(int i=0; i<ELEM_COUNT; ++i)
cache.Add(i, i);
Assert.That( () =>
{
int v = 0;
for(int j=0; j<ELEM_COUNT; ++j)
v += cache[j];
},
Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_AccessStringKey()
{
var cache = new Dictionary<string, int>(EqualityComparer<String>.Default);
var strings = new List<string>();
for(int i=0; i<ELEM_COUNT; ++i)
{
strings.Add(i.ToString());
cache.Add(strings[i], i);
}
Assert.That( () =>
{
EqualityComparer<String>.Default.GetHashCode(strings[0]);
}, Is.Not.AllocatingGCMemory()
);
//---------EQ.Defaul.GetHashCode(string var) will NOT generate gc---------//
string s = "helloworld";
EqualityComparer<string>.Default.GetHashCode(s);
Assert.That( () =>
{
EqualityComparer<string>.Default.GetHashCode(s);
}, Is.Not.AllocatingGCMemory()
);
//---------EQ.Default.GetHashCode(string literal / constant string) will generate gc---------//
EqualityComparer<string>.Default.GetHashCode("helloworld");
Assert.That( () =>
{
EqualityComparer<string>.Default.GetHashCode("helloworld");
}, Is.AllocatingGCMemory()
);
Assert.That( () =>
{
int v = 0;
for(int j=0; j<ELEM_COUNT; ++j)
v += cache[strings[j]];
},
Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_AccessStructKeyWithExplicitComparer()
{
var cache = new Dictionary<Data, int>( new _DataKeyComparer() );
var cont = new List<int>(ELEM_COUNT);
for(int i=0; i<ELEM_COUNT; ++i)
cache.Add(new Data(i, i), i);
cont.Clear();
Assert.That(
() => {
for(int j=0; j<ELEM_COUNT; ++j)
{
cont.Add(cache[new Data(j,j)]);
}
},
Is.Not.AllocatingGCMemory()
);
}
public class _DataKeyComparer : EqualityComparer<Data>
{
public override bool Equals(Data x, Data y)
{
return x.Equals(y);
}
public override int GetHashCode(Data obj)
{
return obj.x << 16 + obj.y;
}
}
[Test]
public void OK_AccessClassKeyWithExplicitComparer()
{
var cache = new Dictionary<CData, int>(new _CDataComparer());
var cont = new List<CData>();
for(int i=0; i<ELEM_COUNT; ++i)
{
var o0 = new CData(i, i);
cache.Add( o0, i );
var o1 = new CData(i, i);
cont.Add(o1);
Assert.That(cache.ContainsKey(o1));
Assert.That(o0.Equals(o1));
}
int k = 0;
Assert.That( () =>
{
for(int j=0; j<ELEM_COUNT; ++j)
{
var cdata = cont[j];
var v = cache[cdata];
k += v;
}
},
Is.Not.AllocatingGCMemory()
);
Assert.That(k, Is.EqualTo((ELEM_COUNT-1) * ELEM_COUNT / 2));
}
public class _CDataComparer : EqualityComparer<CData>
{
public override bool Equals(CData x, CData y)
{
return x.Equals(y);
}
public override int GetHashCode(CData obj)
{
return obj.x << 16 + obj.y;
}
}
[Test]
public void OK_AccessClassKeyWithOverrideEqualAndHashCode()
{
var cache = new Dictionary<AData, int>();
var cont = new List<AData>();
for(int i=0; i<ELEM_COUNT; ++i)
{
var o0 = new AData(i, i);
cache.Add( o0, i );
var o1 = new AData(i, i);
cont.Add(o1);
Assert.That(cache.ContainsKey(o1));
Assert.That(o0.Equals(o1));
}
int k = 0;
Assert.That( () =>
{
for(int j=0; j<ELEM_COUNT; ++j)
{
var data = cont[j];
var v = cache[data];
k += v;
}
},
Is.Not.AllocatingGCMemory()
);
Assert.That(k, Is.EqualTo((ELEM_COUNT-1) * ELEM_COUNT / 2));
}
}
}