-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
438 lines (403 loc) · 19.6 KB
/
index.html
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<!DOCTYPE html>
<html>
<head>
<base href="html-docs/" />
<meta charset='utf-8' />
<meta name="description" content="ValueUtils : ValueUtils efficiently implements .Equals() and .GetHashCode() for you. " />
<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">
<title>ValueUtils</title>
</head>
<body>
<!-- HEADER -->
<div id="header_wrap" class="outer">
<header class="inner">
<a id="forkme_banner" href="https://github.com/EamonNerbonne/ValueUtils">View on GitHub</a>
<a id="nuget_banner" href="https://www.nuget.org/packages/ValueUtils/">View on nuget</a>
<h1 id="project_title">ValueUtils</h1>
<h2 id="project_tagline">ValueUtils efficiently implements <code>.Equals()</code> and <code>.GetHashCode()</code> for you. </h2>
<section id="downloads">
<a class="zip_download_link" href="https://github.com/EamonNerbonne/ValueUtils/zipball/master">Download this project as a .zip file</a>
<a class="tar_download_link" href="https://github.com/EamonNerbonne/ValueUtils/tarball/master">Download this project as a tar.gz file</a>
</section>
</header>
</div>
<!-- MAIN CONTENT -->
<div id="main_content_wrap" class="outer">
<section id="main_content" class="inner">
<h1>
<a name="valueutils" class="anchor" href="#valueutils"><span class="octicon octicon-link"></span></a>About ValueUtils</h1>
<p><a href="http://eamonnerbonne.github.io/ValueUtils/">ValueUtils</a> implements <code>Equals</code> and <code>GetHashCode</code> for you. By using runtime code-generation, the performance overhead is kept small; <code>ValueObject<></code> generally outperforms alternatives such as <code>Tuple<></code>, <code>struct</code> or anonymous types (see benchmarks below). </p>
<p>The library is available on nuget (for import or direct download) as <a href="https://www.nuget.org/packages/ValueUtils/">ValueUtils</a>. Though it's implemented in C#, it's just as applicable to VB.NET classes. </p>
<p><b>Contributions welcome!</b> If you've found a bug, are missing a feature, or just have a question, please do create a new <a href="https://github.com/EamonNerbonne/ValueUtils/issues">github issue</a>, <a href="https://github.com/EamonNerbonne/ValueUtils/pulls">pull request</a>, or send an email to 'eamon (at) nerbonne (dot) org'.</p>
<h2>
<a name="usage" class="anchor" href="#usage"><span class="octicon octicon-link"></span></a>Usage:</h2>
<h3>
<a name="the-easy-way" class="anchor" href="#the-easy-way"><span class="octicon octicon-link"></span></a>The easy way</h3>
<p>The easiest way to use value semantics is to derive from <code>ValueObject<></code>, for example:</p>
<div class="highlight highlight-C#"><pre><span class="k">using</span> <span class="nn">ValueUtils</span><span class="p">;</span>
<span class="k">sealed</span> <span class="k">class</span> <span class="nc">MyValueObject</span> <span class="p">:</span> <span class="n">ValueObject</span><span class="p"><</span><span class="n">MyValueObject</span><span class="p">></span> <span class="p">{</span>
<span class="k">public</span> <span class="kt">int</span> <span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">;</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">X</span><span class="p">,</span><span class="n">Y</span><span class="p">,</span> <span class="n">Z</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
<p>A class deriving from <code>ValueObject<T></code> implements <code>IEquatable<T></code> and has <code>Equals(object)</code>, <code>Equals(T)</code>, <code>GetHashCode()</code> and the <code>==</code> and <code>!=</code> operators implemented in terms of their fields.</p>
<h3>
<a name="explicit-usage" class="anchor" href="#explicit-usage"><span class="octicon octicon-link"></span></a>Explicit usage</h3>
<p>You can also generate delegates (or an IEqualityComparer<>) for hashing and equality comparison for any type (also types in other assemblies you don't control). Given the following example class:</p>
<div class="highlight highlight-C#"><pre><span class="k">class</span> <span class="nc">ExampleClass</span> <span class="p">{</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">myMember</span><span class="p">;</span>
<span class="k">protected</span> <span class="k">readonly</span> <span class="n">DateTime</span> <span class="n">supports_readonly_too</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">private_int</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
<p>The generated hash function can be explicitly used as follows:</p>
<div class="highlight highlight-C#"><pre><span class="k">using</span> <span class="nn">ValueUtils</span><span class="p">;</span>
<span class="n">Func</span><span class="p"><</span><span class="n">ExampleClass</span><span class="p">,</span> <span class="kt">int</span><span class="p">></span> <span class="n">hashfunc</span> <span class="p">=</span> <span class="n">FieldwiseHasher</span><span class="p"><</span><span class="n">ExampleClass</span><span class="p">>.</span><span class="n">Instance</span><span class="p">;</span>
<span class="c1">//or call immediately with type-inference</span>
<span class="kt">int</span> <span class="n">hashcode</span> <span class="p">=</span> <span class="n">FieldwiseHasher</span><span class="p">.</span><span class="n">Hash</span><span class="p">(</span><span class="n">my_example_object</span><span class="p">);</span>
</pre></div>
<p>The generated equality function can be explicitly used as follows:</p>
<div class="highlight highlight-C#"><pre><span class="k">using</span> <span class="nn">ValueUtils</span><span class="p">;</span>
<span class="n">Func</span><span class="p"><</span><span class="n">ExampleClass</span><span class="p">,</span> <span class="n">ExampleClass</span><span class="p">,</span> <span class="kt">bool</span><span class="p">></span> <span class="n">equalityComparer</span> <span class="p">=</span> <span class="n">FieldwiseEquality</span><span class="p"><</span><span class="n">ExampleClass</span><span class="p">>.</span><span class="n">Instance</span><span class="p">;</span>
<span class="c1">//or call immediately with type-inference</span>
<span class="kt">bool</span> <span class="n">areEqual</span> <span class="p">=</span> <span class="n">FieldwiseEquality</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="n">my_example_object</span><span class="p">,</span> <span class="n">another_example_object</span><span class="p">);</span>
</pre></div>
<h3>
<a name="usage-in-structs" class="anchor" href="#usage-in-structs"><span class="octicon octicon-link"></span></a>Usage in <code>struct</code>s</h3>
<p>The above delegates are considerably faster than the built-in <code>ValueType</code>-provided defaults for <code>struct</code>s (which use reflection every call), which is why they're a good fit to help implement <code>GetHashCode</code> and <code>Equals</code> for your own structs. Unfortunately, you can't use inheritance to mix in the generated code, so you'll need to use the explicit calls described above. For example:</p>
<div class="highlight highlight-C#"><pre><span class="k">struct</span> <span class="nc">ExampleStruct</span> <span class="p">:</span> <span class="n">IEquatable</span><span class="p"><</span><span class="n">ExampleStruct</span><span class="p">></span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">some</span><span class="p">,</span> <span class="n">members</span><span class="p">,</span> <span class="n">here</span><span class="p">;</span>
<span class="c1">//...</span>
<span class="k">public</span> <span class="kt">bool</span> <span class="nf">Equals</span><span class="p">(</span><span class="n">ExampleStruct</span> <span class="n">other</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">FieldwiseEquality</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="n">other</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">override</span> <span class="kt">bool</span> <span class="nf">Equals</span><span class="p">(</span><span class="kt">object</span> <span class="n">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">obj</span> <span class="k">is</span> <span class="n">ExampleStruct</span> <span class="p">&&</span> <span class="n">Equals</span><span class="p">((</span><span class="n">ExampleStruct</span><span class="p">)</span><span class="n">obj</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">override</span> <span class="kt">int</span> <span class="nf">GetHashCode</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">FieldwiseHasher</span><span class="p">.</span><span class="n">Hash</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<h2>
<a name="limitations-and-gotchas" class="anchor" href="#limitations-and-gotchas"><span class="octicon octicon-link"></span></a>Limitations and gotcha's</h2>
<p><strong>Cyclical data structures:</strong> <code>ValueObject<></code> supports self-referential types (like tree structures or a singly linked list), but does not support cyclical types - such as a doubly linked list. Whenever a cycle is encountered, the hash function and equals operations will not terminate (until the stack overflows).</p>
<p><strong>Inheritance:</strong> Equality is implemented on a per-type basis, and that means inheritance gets confusing. It's OK to <em>have</em> a base class (and base class fields will affect hash and equality), but if you use the base-class's equality and/or hash implementation on a subclass <em>instance</em> the code will seem to work but only consider the fields of the base class. Best practice: don't create sub-classes that add new fields; and if you do then at least never use the base-class equality+hashcode implementations. This is why ValueObject verifies that its subclasses must be sealed.</p>
<p><strong>Lazily constructed internals:</strong> <code>FieldwiseHasher</code> and <code>FieldwiseEquality</code> "work" on almost all types, including types with private members in other assemblies - however, if you don't know the internals, you can't be sure what's being included in the equality computations. In particular, if an object is lazily initialized, two semantically equivalent objects might compute as unequal simply because one is initialized and the other is not. In practice this is rarely a problem.</p>
<h2>
<a name="performance-and-hash-quality" class="anchor" href="#performance-and-hash-quality"><span class="octicon octicon-link"></span></a>Performance and hash-quality</h2>
<p><strong>TL;DR</strong> <code>ValueObject<></code> usually outperforms alternatives such as <code>Tuple<></code>, <code>struct</code> and anonymous types. Compared to hand-rolled implementations common operations such as <code>.ToDictionary</code> are around 15-25% slower (if your object contains "expensive" data such as large strings, the difference will become a lot smaller).</p>
<p>All performance measurements were done on an i7-4770k at a fixed clock rate of 4.0GHz. Timings are in nanoseconds per object. Datasets are all approximately 3000000 objects in size. Loops over the dataset were repeated until 10 seconds were up, then the fastest quartile average reported (this minimizes interference by other processes on my dev machine since random interference is almost always bad for performance, not good). Some hash generators (notably <code>struct</code>) are so poor that this wasn't feasible, those timings are omitted (NaN) below.</p>
<p>Note that even a perfect hash mix is expected to have 0.03-0.04% colliding buckets, so if you see numbers like that in the data below, a hash if functioning as expected. Numbers better(lower) than that are actually worrisome, because that means some kind of structure in the input is being exploited, and that likely means similar but slightly different data exists that will have lots of collisions. And of course, number much higher that that directly impact performance.</p>
<p>Quite a few tests use a simple pair of ints - this is relevant because this is pretty much a worst case for ValueObject. Although the generated code is fast, calling into that code requires a cast and a Delegate call, and those are (relatively) expensive operations in .NET - at least, compared to simple integer math that a pair-of-ints hashcode requires. With more complicated objects containing reference types the cost of the hashcode computation will start to matter more, and the overhead less.</p>
<div>
<table>
<thead>
<tr>
<th colspan="7">Realistic scenario with an enum, a string, a DateTime, an int? and 3 int fields.</th>
</tr>
<tr>
<th>Name</th>
<th>Collisions</th>
<th>Distinct Hashcodes</th>
<th>.ToDictionary()</th>
<th>.Distinct().Count()</th>
<th>.Equals()</th>
<th>.GetHashCode()</th>
</tr>
</thead>
<tbody>
<tr>
<td>ComplicatedManual</td>
<td>0.04%</td>
<td>2912961 / 2914000</td>
<td>218.8</td>
<td>199.6</td>
<td>6.9</td>
<td>17.4</td>
</tr>
<tr>
<td>ComplicatedValueObject</td>
<td>0.04%</td>
<td>2912977 / 2914000</td>
<td>250.2</td>
<td>230.5</td>
<td>21.4</td>
<td>42.1</td>
</tr>
<tr>
<td>Tuple</td>
<td>0.03%</td>
<td>2913001 / 2914000</td>
<td>482.2</td>
<td>494.8</td>
<td>257.6</td>
<td>263.5</td>
</tr>
<tr>
<td>ComplicatedStruct</td>
<td>100.00%</td>
<td>2 / 2914000</td>
<td>NaN</td>
<td>NaN</td>
<td>1002.3</td>
<td>97.2</td>
</tr>
<tr>
<td>Anonymous Type</td>
<td>0.03%</td>
<td>2913022 / 2914000</td>
<td>261.0</td>
<td>247.8</td>
<td>31.5</td>
<td>52.9</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="7">A simple pair of ints</th>
</tr>
<tr>
<th>Name</th>
<th>Collisions</th>
<th>Distinct Hashcodes</th>
<th>.ToDictionary()</th>
<th>.Distinct().Count()</th>
<th>.Equals()</th>
<th>.GetHashCode()</th>
</tr>
</thead>
<tbody>
<tr>
<td>IntPairManual</td>
<td>0.02%</td>
<td>2975318 / 2976000</td>
<td>159.3</td>
<td>133.6</td>
<td>3.8</td>
<td>1.7</td>
</tr>
<tr>
<td>IntPairValueObject</td>
<td>0.03%</td>
<td>2974963 / 2976000</td>
<td>199.3</td>
<td>181.9</td>
<td>20.0</td>
<td>16.9</td>
</tr>
<tr>
<td>Tuple</td>
<td>38.31%</td>
<td>1835788 / 2976000</td>
<td>353.7</td>
<td>289.2</td>
<td>98.4</td>
<td>54.9</td>
</tr>
<tr>
<td>IntPairStruct</td>
<td>56.61%</td>
<td>1291168 / 2976000</td>
<td>864.7</td>
<td>812.6</td>
<td>31.4</td>
<td>36.8</td>
</tr>
<tr>
<td>Anonymous Type</td>
<td>4.69%</td>
<td>2836344 / 2976000</td>
<td>185.2</td>
<td>158.2</td>
<td>15.3</td>
<td>13.5</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="7">Two ints with both the same value</th>
</tr>
<tr>
<th>Name</th>
<th>Collisions</th>
<th>Distinct Hashcodes</th>
<th>.ToDictionary()</th>
<th>.Distinct().Count()</th>
<th>.Equals()</th>
<th>.GetHashCode()</th>
</tr>
</thead>
<tbody>
<tr>
<td>IntPairManual</td>
<td>0.37%</td>
<td>2988915 / 3000000</td>
<td>188.5</td>
<td>155.6</td>
<td>3.6</td>
<td>1.7</td>
</tr>
<tr>
<td>IntPairValueObject</td>
<td>0.03%</td>
<td>2999012 / 3000000</td>
<td>200.8</td>
<td>194.6</td>
<td>19.7</td>
<td>16.5</td>
</tr>
<tr>
<td>Tuple</td>
<td>22.07%</td>
<td>2337827 / 3000000</td>
<td>145.2</td>
<td>140.5</td>
<td>76.4</td>
<td>55.1</td>
</tr>
<tr>
<td>IntPairStruct</td>
<td>100.00%</td>
<td>1 / 3000000</td>
<td>NaN</td>
<td>NaN</td>
<td>31.0</td>
<td>36.7</td>
</tr>
<tr>
<td>Anonymous Type</td>
<td>0.00%</td>
<td>3000000 / 3000000</td>
<td>144.5</td>
<td>106.3</td>
<td>12.1</td>
<td>13.5</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="7">Two ints such that (x,y) is present iif (y,x) is present in the dataset</th>
</tr>
<tr>
<th>Name</th>
<th>Collisions</th>
<th>Distinct Hashcodes</th>
<th>.ToDictionary()</th>
<th>.Distinct().Count()</th>
<th>.Equals()</th>
<th>.GetHashCode()</th>
</tr>
</thead>
<tbody>
<tr>
<td>IntPairManual</td>
<td>0.62%</td>
<td>3014881 / 3033584</td>
<td>154.3</td>
<td>140.5</td>
<td>3.6</td>
<td>1.7</td>
</tr>
<tr>
<td>IntPairValueObject</td>
<td>0.03%</td>
<td>3032561 / 3033584</td>
<td>192.8</td>
<td>174.6</td>
<td>19.6</td>
<td>17.0</td>
</tr>
<tr>
<td>Tuple</td>
<td>41.47%</td>
<td>1775545 / 3033584</td>
<td>457.5</td>
<td>432.1</td>
<td>76.0</td>
<td>54.8</td>
</tr>
<tr>
<td>IntPairStruct</td>
<td>74.50%</td>
<td>773500 / 3033584</td>
<td>804.6</td>
<td>775.8</td>
<td>31.0</td>
<td>36.6</td>
</tr>
<tr>
<td>Anonymous Type</td>
<td>0.79%</td>
<td>3009536 / 3033584</td>
<td>175.2</td>
<td>161.2</td>
<td>12.1</td>
<td>13.5</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="7">A reference to the type itself and two int fields. The dataset contains exactly one level of nesting such that the outer object is (x,y) when the inner is (y,x).</th>
</tr>
<tr>
<th>Name</th>
<th>Collisions</th>
<th>Distinct Hashcodes</th>
<th>.ToDictionary()</th>
<th>.Distinct().Count()</th>
<th>.Equals()</th>
<th>.GetHashCode()</th>
</tr>
</thead>
<tbody>
<tr>
<td>NastyNestedManual</td>
<td>24.14%</td>
<td>2267216 / 2988648</td>
<td>225.1</td>
<td>181.5</td>
<td>6.3</td>
<td>5.0</td>
</tr>
<tr>
<td>NastyNestedValueObject</td>
<td>0.03%</td>
<td>2987634 / 2988648</td>
<td>239.7</td>
<td>208.8</td>
<td>30.9</td>
<td>33.0</td>
</tr>
<tr>
<td>Tuple</td>
<td>57.80%</td>
<td>1261193 / 2988648</td>
<td>489.5</td>
<td>491.8</td>
<td>103.3</td>
<td>132.0</td>
</tr>
</tbody>
</table>
</div>
</section>
</div>
<!-- FOOTER -->
<div id="footer_wrap" class="outer">
<footer class="inner">
<p class="copyright">ValueUtils maintained by <a href="https://github.com/EamonNerbonne">EamonNerbonne</a></p>
</footer>
</div>
</body>
</html>