|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using Celerity.Collections; |
| 4 | +using Celerity.Hashing; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// The reference-type-key probe path, which the rest of the tracked suite does not cover: every other |
| 8 | +/// dictionary / set benchmark here keys on <c>int</c> or <c>long</c>. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// <para> |
| 12 | +/// A value-type key lets the JIT devirtualize and inline <c>EqualityComparer<T>.Default</c>, so the |
| 13 | +/// probe body is straight-line code. A reference-type key does not: the collection JITs as a |
| 14 | +/// <c>__Canon</c>-shared body and each comparison is a real interface dispatch. That makes the string-keyed |
| 15 | +/// tables the ones worth tracking for probe-path codegen work. |
| 16 | +/// </para> |
| 17 | +/// <para> |
| 18 | +/// <c>LookupMissing</c> is the probe-heavy arm on purpose: a hit stops at the matching slot, while a miss |
| 19 | +/// walks the whole cluster to the first vacant slot, so it pays the empty-slot test once per iteration. |
| 20 | +/// Both instances are built at the library default load factor — the shipping configuration, not a |
| 21 | +/// contrived one. |
| 22 | +/// </para> |
| 23 | +/// </remarks> |
| 24 | +[MemoryDiagnoser(false)] |
| 25 | +[CategoriesColumn] |
| 26 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 27 | +public class StringKeyProbeBenchmark |
| 28 | +{ |
| 29 | + private string[] keys = null!; |
| 30 | + private string[] missingKeys = null!; |
| 31 | + |
| 32 | + private Dictionary<string, int> dictionary = null!; |
| 33 | + private CelerityDictionary<string, int, StringXxHash3Hasher> celerityDictionary = null!; |
| 34 | + |
| 35 | + private HashSet<string> hashSet = null!; |
| 36 | + private CeleritySet<string, StringXxHash3Hasher> celeritySet = null!; |
| 37 | + |
| 38 | + [Params(1000, 100_000)] |
| 39 | + public int ItemCount; |
| 40 | + |
| 41 | + [GlobalSetup] |
| 42 | + public void Setup() |
| 43 | + { |
| 44 | + keys = new string[ItemCount]; |
| 45 | + missingKeys = new string[ItemCount]; |
| 46 | + for (int i = 0; i < ItemCount; i++) |
| 47 | + { |
| 48 | + // Identifier-shaped, guaranteed-distinct keys, matching the shape the |
| 49 | + // frozen-collection benchmarks use. |
| 50 | + keys[i] = "celerity/key/" + i + "/" + (i * 2654435761u); |
| 51 | + missingKeys[i] = "celerity/absent/" + i + "/" + (i * 2246822519u); |
| 52 | + } |
| 53 | + |
| 54 | + dictionary = new Dictionary<string, int>(ItemCount); |
| 55 | + celerityDictionary = new CelerityDictionary<string, int, StringXxHash3Hasher>(ItemCount); |
| 56 | + hashSet = new HashSet<string>(ItemCount); |
| 57 | + celeritySet = new CeleritySet<string, StringXxHash3Hasher>(ItemCount); |
| 58 | + |
| 59 | + for (int i = 0; i < ItemCount; i++) |
| 60 | + { |
| 61 | + dictionary[keys[i]] = i; |
| 62 | + celerityDictionary[keys[i]] = i; |
| 63 | + hashSet.Add(keys[i]); |
| 64 | + celeritySet.TryAdd(keys[i]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // ── Lookup (every key present) ──────────────────────────────────────────── |
| 69 | + |
| 70 | + [Benchmark(Baseline = true)] |
| 71 | + [BenchmarkCategory("Lookup")] |
| 72 | + public int Dictionary_Lookup() |
| 73 | + { |
| 74 | + int acc = 0; |
| 75 | + foreach (var key in keys) |
| 76 | + acc += dictionary[key]; |
| 77 | + return acc; |
| 78 | + } |
| 79 | + |
| 80 | + [Benchmark] |
| 81 | + [BenchmarkCategory("Lookup")] |
| 82 | + public int CelerityDictionary_Lookup() |
| 83 | + { |
| 84 | + int acc = 0; |
| 85 | + foreach (var key in keys) |
| 86 | + acc += celerityDictionary[key]; |
| 87 | + return acc; |
| 88 | + } |
| 89 | + |
| 90 | + // ── LookupMissing (every probe walks to a vacant slot) ──────────────────── |
| 91 | + |
| 92 | + [Benchmark(Baseline = true)] |
| 93 | + [BenchmarkCategory("LookupMissing")] |
| 94 | + public int Dictionary_LookupMissing() |
| 95 | + { |
| 96 | + int acc = 0; |
| 97 | + foreach (var key in missingKeys) |
| 98 | + if (dictionary.TryGetValue(key, out int value)) |
| 99 | + acc += value; |
| 100 | + return acc; |
| 101 | + } |
| 102 | + |
| 103 | + [Benchmark] |
| 104 | + [BenchmarkCategory("LookupMissing")] |
| 105 | + public int CelerityDictionary_LookupMissing() |
| 106 | + { |
| 107 | + int acc = 0; |
| 108 | + foreach (var key in missingKeys) |
| 109 | + if (celerityDictionary.TryGetValue(key, out int value)) |
| 110 | + acc += value; |
| 111 | + return acc; |
| 112 | + } |
| 113 | + |
| 114 | + // ── Contains (the set counterpart, every element present) ───────────────── |
| 115 | + |
| 116 | + [Benchmark(Baseline = true)] |
| 117 | + [BenchmarkCategory("Contains")] |
| 118 | + public int HashSet_Contains() |
| 119 | + { |
| 120 | + int hits = 0; |
| 121 | + foreach (var key in keys) |
| 122 | + if (hashSet.Contains(key)) |
| 123 | + hits++; |
| 124 | + return hits; |
| 125 | + } |
| 126 | + |
| 127 | + [Benchmark] |
| 128 | + [BenchmarkCategory("Contains")] |
| 129 | + public int CeleritySet_Contains() |
| 130 | + { |
| 131 | + int hits = 0; |
| 132 | + foreach (var key in keys) |
| 133 | + if (celeritySet.Contains(key)) |
| 134 | + hits++; |
| 135 | + return hits; |
| 136 | + } |
| 137 | +} |
0 commit comments