| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/smallhash.h |
| Date: | 2026-04-26 02:35:59 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 259 | 263 | 98.5% |
| Branches: | 88 | 110 | 80.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef CVMFS_SMALLHASH_H_ | ||
| 6 | #define CVMFS_SMALLHASH_H_ | ||
| 7 | |||
| 8 | #ifndef __STDC_FORMAT_MACROS | ||
| 9 | // NOLINTNEXTLINE | ||
| 10 | #define __STDC_FORMAT_MACROS | ||
| 11 | #endif | ||
| 12 | |||
| 13 | #include "duplex_testing.h" | ||
| 14 | #include <inttypes.h> | ||
| 15 | #include <pthread.h> | ||
| 16 | #include <stdint.h> | ||
| 17 | |||
| 18 | #include <algorithm> | ||
| 19 | #include <cassert> | ||
| 20 | #include <cstdlib> | ||
| 21 | #include <new> | ||
| 22 | |||
| 23 | #include "util/atomic.h" | ||
| 24 | #include "util/murmur.hxx" | ||
| 25 | #include "util/prng.h" | ||
| 26 | #include "util/smalloc.h" | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Hash table with linear probing as collision resolution. Works only for | ||
| 30 | * a fixed (maximum) number of elements, i.e. no resizing. Load factor fixed | ||
| 31 | * to 0.7. | ||
| 32 | */ | ||
| 33 | template<class Key, class Value, class Derived> | ||
| 34 | class SmallHashBase { | ||
| 35 | FRIEND_TEST(T_Smallhash, InsertAndCopyMd5Slow); | ||
| 36 | |||
| 37 | public: | ||
| 38 | static const double kLoadFactor; // mainly useless for the dynamic version | ||
| 39 | static const double kThresholdGrow; // only used for resizable version | ||
| 40 | static const double kThresholdShrink; // only used for resizable version | ||
| 41 | |||
| 42 | 175756 | SmallHashBase() { | |
| 43 | 175756 | keys_ = NULL; | |
| 44 | 175756 | values_ = NULL; | |
| 45 | 175756 | hasher_ = NULL; | |
| 46 | 175756 | bytes_allocated_ = 0; | |
| 47 | 175756 | num_collisions_ = 0; | |
| 48 | 175756 | max_collisions_ = 0; | |
| 49 | |||
| 50 | // Properly initialized by Init() | ||
| 51 | 175756 | capacity_ = 0; | |
| 52 | 175756 | initial_capacity_ = 0; | |
| 53 | 175756 | size_ = 0; | |
| 54 | 175756 | } | |
| 55 | |||
| 56 | 175618 | ~SmallHashBase() { DeallocMemory(keys_, values_, capacity_); } | |
| 57 | |||
| 58 | 175746 | void Init(uint32_t expected_size, Key empty, | |
| 59 | uint32_t (*hasher)(const Key &key)) { | ||
| 60 | 175746 | hasher_ = hasher; | |
| 61 | 175746 | empty_key_ = empty; | |
| 62 | 175746 | capacity_ = static_cast<uint32_t>(static_cast<double>(expected_size) | |
| 63 | 175746 | / kLoadFactor); | |
| 64 | 175746 | initial_capacity_ = capacity_; | |
| 65 | 175746 | static_cast<Derived *>(this)->SetThresholds(); // No-op for fixed size | |
| 66 | 175746 | AllocMemory(); | |
| 67 | 175746 | this->DoClear(false); | |
| 68 | 175746 | } | |
| 69 | |||
| 70 | 56646071 | bool Lookup(const Key &key, Value *value) const { | |
| 71 | uint32_t bucket; | ||
| 72 | uint32_t collisions; | ||
| 73 |
1/2✓ Branch 1 taken 56776869 times.
✗ Branch 2 not taken.
|
56646071 | const bool found = DoLookup(key, &bucket, &collisions); |
| 74 |
2/2✓ Branch 0 taken 37089627 times.
✓ Branch 1 taken 19687242 times.
|
56896802 | if (found) |
| 75 |
1/2✓ Branch 1 taken 94637 times.
✗ Branch 2 not taken.
|
37129461 | *value = values_[bucket]; |
| 76 | 56896802 | return found; | |
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Returns both the key and the value. That is useful if Key's equality | ||
| 81 | * operator implements an equivalence relation on Key. In this case, LookupEx | ||
| 82 | * returns the key representing the equivalence class that has been used | ||
| 83 | * during Insert(). | ||
| 84 | * Used to return a glue::InodeEx element when looking for an inode. | ||
| 85 | */ | ||
| 86 | 520 | bool LookupEx(Key *key, Value *value) const { | |
| 87 | 520 | uint32_t bucket = ScaleHash(*key); | |
| 88 |
2/2✓ Branch 1 taken 174 times.
✓ Branch 2 taken 86 times.
|
520 | while (!(keys_[bucket] == empty_key_)) { |
| 89 |
1/2✓ Branch 1 taken 174 times.
✗ Branch 2 not taken.
|
348 | if (keys_[bucket] == *key) { |
| 90 | 348 | *key = keys_[bucket]; | |
| 91 | 348 | *value = values_[bucket]; | |
| 92 | 348 | return true; | |
| 93 | } | ||
| 94 | ✗ | bucket = (bucket + 1) % capacity_; | |
| 95 | } | ||
| 96 | 172 | return false; | |
| 97 | } | ||
| 98 | |||
| 99 | 34073929 | bool Contains(const Key &key) const { | |
| 100 | uint32_t bucket; | ||
| 101 | uint32_t collisions; | ||
| 102 |
1/2✓ Branch 1 taken 34073247 times.
✗ Branch 2 not taken.
|
34073929 | const bool found = DoLookup(key, &bucket, &collisions); |
| 103 | 34073929 | return found; | |
| 104 | } | ||
| 105 | |||
| 106 | 481461678 | void Insert(const Key &key, const Value &value) { | |
| 107 | 481461678 | static_cast<Derived *>(this)->Grow(); // No-op if fixed-size | |
| 108 | 480574003 | const bool overwritten = DoInsert(key, value, true); | |
| 109 | 480234524 | size_ += !overwritten; // size + 1 if the key was not yet in the map | |
| 110 | 480234524 | } | |
| 111 | |||
| 112 | 47512855 | bool Erase(const Key &key) { | |
| 113 | uint32_t bucket; | ||
| 114 | uint32_t collisions; | ||
| 115 |
1/2✓ Branch 1 taken 47334755 times.
✗ Branch 2 not taken.
|
47512855 | const bool found = DoLookup(key, &bucket, &collisions); |
| 116 |
1/2✓ Branch 0 taken 47349848 times.
✗ Branch 1 not taken.
|
47339569 | if (found) { |
| 117 | 47354326 | keys_[bucket] = empty_key_; | |
| 118 | 47354326 | size_--; | |
| 119 | 47354326 | bucket = (bucket + 1) % capacity_; | |
| 120 |
3/3✓ Branch 0 taken 69219983 times.
✓ Branch 1 taken 33747792 times.
✓ Branch 2 taken 14371885 times.
|
117344138 | while (!(keys_[bucket] == empty_key_)) { |
| 121 | 70253212 | Key rehash = keys_[bucket]; | |
| 122 | 70253212 | keys_[bucket] = empty_key_; | |
| 123 |
1/2✓ Branch 1 taken 69989812 times.
✗ Branch 2 not taken.
|
70253212 | DoInsert(rehash, values_[bucket], false); |
| 124 | 69989812 | bucket = (bucket + 1) % capacity_; | |
| 125 | } | ||
| 126 |
1/2✓ Branch 1 taken 47125719 times.
✗ Branch 2 not taken.
|
47090926 | static_cast<Derived *>(this)->Shrink(); // No-op if fixed-size |
| 127 | } | ||
| 128 | 47163788 | return found; | |
| 129 | } | ||
| 130 | |||
| 131 | 1004 | void Clear() { DoClear(true); } | |
| 132 | |||
| 133 | 6121 | uint64_t bytes_allocated() const { return bytes_allocated_; } | |
| 134 | 1359 | static double GetEntrySize() { | |
| 135 | 2718 | const double unit = sizeof(Key) + sizeof(Value); | |
| 136 | 2718 | return unit / kLoadFactor; | |
| 137 | } | ||
| 138 | |||
| 139 | void GetCollisionStats(uint64_t *num_collisions, | ||
| 140 | uint32_t *max_collisions) const { | ||
| 141 | *num_collisions = num_collisions_; | ||
| 142 | *max_collisions = max_collisions_; | ||
| 143 | } | ||
| 144 | |||
| 145 | // Careful with the direct access TODO: iterator | ||
| 146 | uint32_t capacity() const { return capacity_; } | ||
| 147 | 210192 | Key empty_key() const { return empty_key_; } | |
| 148 | 451200 | Key *keys() const { return keys_; } | |
| 149 | 602 | Value *values() const { return values_; } | |
| 150 | |||
| 151 | // Only needed by compat | ||
| 152 | ✗ | void SetHasher(uint32_t (*hasher)(const Key &key)) { hasher_ = hasher; } | |
| 153 | |||
| 154 | protected: | ||
| 155 | 780803790 | uint32_t ScaleHash(const Key &key) const { | |
| 156 | 780803790 | const double bucket = (static_cast<double>(hasher_(key)) | |
| 157 | 780713776 | * static_cast<double>(capacity_) | |
| 158 | / static_cast<double>(static_cast<uint32_t>(-1))); | ||
| 159 | 780713776 | return static_cast<uint32_t>(bucket) % capacity_; | |
| 160 | } | ||
| 161 | |||
| 162 | 202239 | void AllocMemory() { | |
| 163 | 202239 | keys_ = static_cast<Key *>(smmap(capacity_ * sizeof(Key))); | |
| 164 | 202239 | values_ = static_cast<Value *>(smmap(capacity_ * sizeof(Value))); | |
| 165 |
2/2✓ Branch 0 taken 744349993 times.
✓ Branch 1 taken 103268 times.
|
1480805392 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 166 | 1480603153 | /*keys_[i] =*/new (keys_ + i) Key(); | |
| 167 | } | ||
| 168 |
2/2✓ Branch 0 taken 745647273 times.
✓ Branch 1 taken 113493 times.
|
1483420402 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 169 | 1483197713 | /*values_[i] =*/new (values_ + i) Value(); | |
| 170 | } | ||
| 171 | 222689 | bytes_allocated_ = (sizeof(Key) + sizeof(Value)) * capacity_; | |
| 172 | 222689 | } | |
| 173 | |||
| 174 | 202111 | void DeallocMemory(Key *k, Value *v, uint32_t c) { | |
| 175 |
2/2✓ Branch 0 taken 745998700 times.
✓ Branch 1 taken 103203 times.
|
1484105408 | for (uint32_t i = 0; i < c; ++i) { |
| 176 | 1483903297 | k[i].~Key(); | |
| 177 | } | ||
| 178 |
2/2✓ Branch 0 taken 746132700 times.
✓ Branch 1 taken 103203 times.
|
1484373408 | for (uint32_t i = 0; i < c; ++i) { |
| 179 | 1484171297 | v[i].~Value(); | |
| 180 | } | ||
| 181 |
2/2✓ Branch 0 taken 103198 times.
✓ Branch 1 taken 5 times.
|
202111 | if (k) |
| 182 | 202101 | smunmap(k); | |
| 183 |
2/2✓ Branch 0 taken 103198 times.
✓ Branch 1 taken 5 times.
|
202111 | if (v) |
| 184 | 202101 | smunmap(v); | |
| 185 | 202111 | k = NULL; | |
| 186 | 202111 | v = NULL; | |
| 187 | 202111 | } | |
| 188 | |||
| 189 | // Returns true iff the key is overwritten | ||
| 190 | 574789285 | bool DoInsert(const Key &key, const Value &value, | |
| 191 | const bool count_collisions) { | ||
| 192 | uint32_t bucket; | ||
| 193 | uint32_t collisions; | ||
| 194 |
1/2✓ Branch 1 taken 329098655 times.
✗ Branch 2 not taken.
|
574789285 | const bool overwritten = DoLookup(key, &bucket, &collisions); |
| 195 |
2/2✓ Branch 0 taken 259186051 times.
✓ Branch 1 taken 69912604 times.
|
574571841 | if (count_collisions) { |
| 196 | 480526239 | num_collisions_ += collisions; | |
| 197 | 480526239 | max_collisions_ = std::max(collisions, max_collisions_); | |
| 198 | } | ||
| 199 | 574320607 | keys_[bucket] = key; | |
| 200 |
1/2✓ Branch 1 taken 45833003 times.
✗ Branch 2 not taken.
|
574320607 | values_[bucket] = value; |
| 201 | 574320607 | return overwritten; | |
| 202 | } | ||
| 203 | |||
| 204 | 781177646 | bool DoLookup(const Key &key, uint32_t *bucket, uint32_t *collisions) const { | |
| 205 | 781177646 | *bucket = ScaleHash(key); | |
| 206 | 780309356 | *collisions = 0; | |
| 207 |
3/3✓ Branch 0 taken 23966371171 times.
✓ Branch 1 taken 407785735 times.
✓ Branch 2 taken 115141235 times.
|
25106629947 | while (!(keys_[*bucket] == empty_key_)) { |
| 208 |
3/3✓ Branch 0 taken 85049684 times.
✓ Branch 1 taken 23933067036 times.
✓ Branch 2 taken 143013718 times.
|
24535169560 | if (keys_[*bucket] == key) |
| 209 | 208848969 | return true; | |
| 210 | 24326320591 | *bucket = (*bucket + 1) % capacity_; | |
| 211 | 24326320591 | (*collisions)++; | |
| 212 | } | ||
| 213 | 571460387 | return false; | |
| 214 | } | ||
| 215 | |||
| 216 | 202327 | void DoClear(const bool reset_capacity) { | |
| 217 |
2/2✓ Branch 0 taken 1004 times.
✓ Branch 1 taken 102352 times.
|
202327 | if (reset_capacity) |
| 218 | 1257 | static_cast<Derived *>(this)->ResetCapacity(); // No-op if fixed-size | |
| 219 |
2/2✓ Branch 0 taken 746137948 times.
✓ Branch 1 taken 103356 times.
|
1484261270 | for (uint32_t i = 0; i < capacity_; ++i) |
| 220 | 1484058943 | keys_[i] = empty_key_; | |
| 221 | 202327 | size_ = 0; | |
| 222 | 202327 | } | |
| 223 | |||
| 224 | // Methods for resizable version | ||
| 225 | void SetThresholds() { } | ||
| 226 | void Grow() { } | ||
| 227 | void Shrink() { } | ||
| 228 | void ResetCapacity() { } | ||
| 229 | |||
| 230 | // Separate key and value arrays for better locality | ||
| 231 | Key *keys_; | ||
| 232 | Value *values_; | ||
| 233 | uint32_t capacity_; | ||
| 234 | uint32_t initial_capacity_; | ||
| 235 | uint32_t size_; | ||
| 236 | uint32_t (*hasher_)(const Key &key); | ||
| 237 | uint64_t bytes_allocated_; | ||
| 238 | uint64_t num_collisions_; | ||
| 239 | uint32_t max_collisions_; /**< maximum collisions for a single insert */ | ||
| 240 | Key empty_key_; | ||
| 241 | }; | ||
| 242 | |||
| 243 | |||
| 244 | template<class Key, class Value> | ||
| 245 | class SmallHashFixed | ||
| 246 | : public SmallHashBase<Key, Value, SmallHashFixed<Key, Value> > { | ||
| 247 | friend class SmallHashBase<Key, Value, SmallHashFixed<Key, Value> >; | ||
| 248 | |||
| 249 | protected: | ||
| 250 | // No-ops | ||
| 251 | 6033 | void SetThresholds() { } | |
| 252 | 1271961 | void Grow() { } | |
| 253 | 48348 | void Shrink() { } | |
| 254 | 88 | void ResetCapacity() { } | |
| 255 | }; | ||
| 256 | |||
| 257 | |||
| 258 | template<class Key, class Value> | ||
| 259 | class SmallHashDynamic | ||
| 260 | : public SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > { | ||
| 261 | friend class SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> >; | ||
| 262 | |||
| 263 | public: | ||
| 264 | typedef SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > Base; | ||
| 265 | static const double kThresholdGrow; | ||
| 266 | static const double kThresholdShrink; | ||
| 267 | |||
| 268 | 169723 | SmallHashDynamic() : Base() { | |
| 269 | 169723 | num_migrates_ = 0; | |
| 270 | |||
| 271 | // Properly set by Init | ||
| 272 | 169723 | threshold_grow_ = 0; | |
| 273 | 169723 | threshold_shrink_ = 0; | |
| 274 | 169723 | } | |
| 275 | |||
| 276 | SmallHashDynamic(const SmallHashDynamic<Key, Value> &other) : Base() { | ||
| 277 | num_migrates_ = 0; | ||
| 278 | CopyFrom(other); | ||
| 279 | } | ||
| 280 | |||
| 281 | 317 | SmallHashDynamic<Key, Value> &operator=( | |
| 282 | const SmallHashDynamic<Key, Value> &other) { | ||
| 283 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
|
317 | if (&other == this) |
| 284 | ✗ | return *this; | |
| 285 | |||
| 286 | 317 | CopyFrom(other); | |
| 287 | 317 | return *this; | |
| 288 | } | ||
| 289 | |||
| 290 | 68612695 | uint32_t capacity() const { return Base::capacity_; } | |
| 291 | 319503960 | uint32_t size() const { return Base::size_; } | |
| 292 | uint32_t num_migrates() const { return num_migrates_; } | ||
| 293 | |||
| 294 | protected: | ||
| 295 | 196206 | void SetThresholds() { | |
| 296 | 196206 | threshold_grow_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 297 | 196206 | * kThresholdGrow); | |
| 298 | 196206 | threshold_shrink_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 299 | 196206 | * kThresholdShrink); | |
| 300 | 196206 | } | |
| 301 | |||
| 302 | 479687058 | void Grow() { | |
| 303 |
2/2✓ Branch 1 taken 9200 times.
✓ Branch 2 taken 257878988 times.
|
479687058 | if (size() > threshold_grow_) |
| 304 | 18400 | Migrate(capacity() * 2); | |
| 305 | 479396023 | } | |
| 306 | |||
| 307 | 47234242 | void Shrink() { | |
| 308 |
2/2✓ Branch 1 taken 33797269 times.
✓ Branch 2 taken 13392030 times.
|
47234242 | if (size() < threshold_shrink_) { |
| 309 | 33801747 | const uint32_t target_capacity = capacity() / 2; | |
| 310 |
2/2✓ Branch 0 taken 3435 times.
✓ Branch 1 taken 33766300 times.
|
33774213 | if (target_capacity >= Base::initial_capacity_) |
| 311 | 3435 | Migrate(target_capacity); | |
| 312 | } | ||
| 313 | 47166248 | } | |
| 314 | |||
| 315 | 1169 | void ResetCapacity() { | |
| 316 | 1169 | Base::DeallocMemory(Base::keys_, Base::values_, Base::capacity_); | |
| 317 | 1169 | Base::capacity_ = Base::initial_capacity_; | |
| 318 | 1169 | Base::AllocMemory(); | |
| 319 | 1169 | SetThresholds(); | |
| 320 | 1169 | } | |
| 321 | |||
| 322 | private: | ||
| 323 | // Returns a random permutation of indices [0..N-1] that is allocated | ||
| 324 | // by smmap (Knuth's shuffle algorithm) | ||
| 325 | 7202 | uint32_t *ShuffleIndices(const uint32_t N) { | |
| 326 | 7202 | uint32_t *shuffled = static_cast<uint32_t *>(smmap(N * sizeof(uint32_t))); | |
| 327 | // Init with identity | ||
| 328 |
2/2✓ Branch 0 taken 55870261 times.
✓ Branch 1 taken 3601 times.
|
111747724 | for (unsigned i = 0; i < N; ++i) |
| 329 | 111740522 | shuffled[i] = i; | |
| 330 | // Shuffle (no shuffling for the last element) | ||
| 331 |
1/2✓ Branch 0 taken 52638320 times.
✗ Branch 1 not taken.
|
105235512 | for (unsigned i = 0; i < N - 1; ++i) { |
| 332 | 105276640 | const uint32_t swap_idx = i + g_prng.Next(N - i); | |
| 333 | 105228310 | const uint32_t tmp = shuffled[i]; | |
| 334 | 105228310 | shuffled[i] = shuffled[swap_idx]; | |
| 335 | 105228310 | shuffled[swap_idx] = tmp; | |
| 336 | } | ||
| 337 | 317 | return shuffled; | |
| 338 | } | ||
| 339 | |||
| 340 | 25270 | void Migrate(const uint32_t new_capacity) { | |
| 341 | 25270 | Key *old_keys = Base::keys_; | |
| 342 | 25270 | Value *old_values = Base::values_; | |
| 343 | 25270 | const uint32_t old_capacity = capacity(); | |
| 344 | 25270 | const uint32_t old_size = size(); | |
| 345 | |||
| 346 | 25270 | Base::capacity_ = new_capacity; | |
| 347 | 25270 | SetThresholds(); | |
| 348 | 25280 | Base::AllocMemory(); | |
| 349 | 25280 | Base::DoClear(false); | |
| 350 |
2/2✓ Branch 0 taken 3440 times.
✓ Branch 1 taken 9200 times.
|
25280 | if (new_capacity < old_capacity) { |
| 351 | 6880 | uint32_t *shuffled_indices = ShuffleIndices(old_capacity); | |
| 352 |
1/2✓ Branch 0 taken 48219250 times.
✗ Branch 1 not taken.
|
96434550 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 353 |
2/3✓ Branch 0 taken 12107420 times.
✓ Branch 1 taken 36111830 times.
✗ Branch 2 not taken.
|
96438500 | if (old_keys[shuffled_indices[i]] != Base::empty_key_) { |
| 354 | 24214840 | Base::Insert(old_keys[shuffled_indices[i]], | |
| 355 | 24214840 | old_values[shuffled_indices[i]]); | |
| 356 | } | ||
| 357 | } | ||
| 358 | ✗ | smunmap(shuffled_indices); | |
| 359 | } else { | ||
| 360 |
2/2✓ Branch 0 taken 118056799 times.
✓ Branch 1 taken 4185 times.
|
236121968 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 361 |
3/3✓ Branch 0 taken 52473494 times.
✓ Branch 1 taken 53553943 times.
✓ Branch 2 taken 12029362 times.
|
236113598 | if (old_keys[i] != Base::empty_key_) |
| 362 | 177135506 | Base::Insert(old_keys[i], old_values[i]); | |
| 363 | } | ||
| 364 | } | ||
| 365 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12640 times.
|
15250 | assert(size() == old_size); |
| 366 | |||
| 367 | 25280 | Base::DeallocMemory(old_keys, old_values, old_capacity); | |
| 368 | 25280 | num_migrates_++; | |
| 369 | 25280 | } | |
| 370 | |||
| 371 | 317 | void CopyFrom(const SmallHashDynamic<Key, Value> &other) { | |
| 372 | 317 | uint32_t *shuffled_indices = ShuffleIndices(other.capacity_); | |
| 373 |
2/2✓ Branch 0 taken 6884556 times.
✓ Branch 1 taken 161 times.
|
6888149 | for (uint32_t i = 0; i < other.capacity_; ++i) { |
| 374 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 5001638 times.
✓ Branch 2 taken 1882918 times.
|
6887832 | if (other.keys_[shuffled_indices[i]] != other.empty_key_) { |
| 375 | 5000000 | this->Insert(other.keys_[shuffled_indices[i]], | |
| 376 | 5000000 | other.values_[shuffled_indices[i]]); | |
| 377 | } | ||
| 378 | } | ||
| 379 | 317 | smunmap(shuffled_indices); | |
| 380 | 317 | } | |
| 381 | |||
| 382 | uint32_t num_migrates_; | ||
| 383 | uint32_t threshold_grow_; | ||
| 384 | uint32_t threshold_shrink_; | ||
| 385 | static Prng g_prng; | ||
| 386 | }; | ||
| 387 | |||
| 388 | |||
| 389 | /** | ||
| 390 | * Distributes the key-value pairs over $n$ dynamic hash maps with individual | ||
| 391 | * mutexes. Hence low mutex contention, and benefits from multiple processors. | ||
| 392 | */ | ||
| 393 | template<class Key, class Value> | ||
| 394 | class MultiHash { | ||
| 395 | public: | ||
| 396 | 50 | MultiHash() { | |
| 397 | 50 | num_hashmaps_ = 0; | |
| 398 | 50 | hashmaps_ = NULL; | |
| 399 | 50 | locks_ = NULL; | |
| 400 | 50 | } | |
| 401 | |||
| 402 | 50 | void Init(const uint8_t num_hashmaps, const Key &empty_key, | |
| 403 | uint32_t (*hasher)(const Key &key)) { | ||
| 404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | assert(num_hashmaps > 0); |
| 405 | 50 | const uint8_t N = num_hashmaps; | |
| 406 | 50 | num_hashmaps_ = N; | |
| 407 |
3/4✓ Branch 2 taken 2100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2100 times.
✓ Branch 5 taken 50 times.
|
2150 | hashmaps_ = new SmallHashDynamic<Key, Value>[N](); |
| 408 | 50 | locks_ = static_cast<pthread_mutex_t *>( | |
| 409 | 50 | smalloc(N * sizeof(pthread_mutex_t))); | |
| 410 |
2/2✓ Branch 0 taken 2100 times.
✓ Branch 1 taken 50 times.
|
2150 | for (uint8_t i = 0; i < N; ++i) { |
| 411 | 2100 | int retval = pthread_mutex_init(&locks_[i], NULL); | |
| 412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2100 times.
|
2100 | assert(retval == 0); |
| 413 | 2100 | hashmaps_[i].Init(128, empty_key, hasher); | |
| 414 | } | ||
| 415 | 50 | } | |
| 416 | |||
| 417 | 50 | ~MultiHash() { | |
| 418 |
2/2✓ Branch 0 taken 2100 times.
✓ Branch 1 taken 50 times.
|
2150 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 419 | 2100 | pthread_mutex_destroy(&locks_[i]); | |
| 420 | } | ||
| 421 | 50 | free(locks_); | |
| 422 |
3/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2100 times.
✓ Branch 3 taken 50 times.
|
2150 | delete[] hashmaps_; |
| 423 | 50 | } | |
| 424 | |||
| 425 | 5000000 | bool Lookup(const Key &key, Value *value) { | |
| 426 | 5000000 | uint8_t target = SelectHashmap(key); | |
| 427 | 5000000 | Lock(target); | |
| 428 | 5000000 | const bool result = hashmaps_[target].Lookup(key, value); | |
| 429 | 5000000 | Unlock(target); | |
| 430 | 5000000 | return result; | |
| 431 | } | ||
| 432 | |||
| 433 | 18509460 | void Insert(const Key &key, const Value &value) { | |
| 434 | 18509460 | uint8_t target = SelectHashmap(key); | |
| 435 | 18048260 | Lock(target); | |
| 436 | 18723720 | hashmaps_[target].Insert(key, value); | |
| 437 | 17714130 | Unlock(target); | |
| 438 | 19016360 | } | |
| 439 | |||
| 440 | 8992970 | void Erase(const Key &key) { | |
| 441 | 8992970 | uint8_t target = SelectHashmap(key); | |
| 442 | 8557050 | Lock(target); | |
| 443 | 9113375 | hashmaps_[target].Erase(key); | |
| 444 | 8342435 | Unlock(target); | |
| 445 | 9255710 | } | |
| 446 | |||
| 447 | 5 | void Clear() { | |
| 448 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 5 times.
|
215 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 449 | 210 | Lock(i); | |
| 450 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 5 times.
|
215 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 451 | 210 | hashmaps_[i].Clear(); | |
| 452 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 5 times.
|
215 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 453 | 210 | Unlock(i); | |
| 454 | 5 | } | |
| 455 | |||
| 456 | 50 | uint8_t num_hashmaps() const { return num_hashmaps_; } | |
| 457 | |||
| 458 | 40 | void GetSizes(uint32_t *sizes) { | |
| 459 |
2/2✓ Branch 0 taken 1680 times.
✓ Branch 1 taken 40 times.
|
1720 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 460 | 1680 | Lock(i); | |
| 461 | 1680 | sizes[i] = hashmaps_[i].size(); | |
| 462 | 1680 | Unlock(i); | |
| 463 | } | ||
| 464 | 40 | } | |
| 465 | |||
| 466 | void GetCollisionStats(uint64_t *num_collisions, uint32_t *max_collisions) { | ||
| 467 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { | ||
| 468 | Lock(i); | ||
| 469 | hashmaps_[i].GetCollisionStats(&num_collisions[i], &max_collisions[i]); | ||
| 470 | Unlock(i); | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
| 474 | private: | ||
| 475 | 32306475 | inline uint8_t SelectHashmap(const Key &key) { | |
| 476 | 32306475 | uint32_t hash = MurmurHash2(&key, sizeof(key), 0x37); | |
| 477 | 31749015 | double bucket = static_cast<double>(hash) | |
| 478 | 31749015 | * static_cast<double>(num_hashmaps_) | |
| 479 | / static_cast<double>(static_cast<uint32_t>(-1)); | ||
| 480 | 31749015 | return static_cast<uint32_t>(bucket) % num_hashmaps_; | |
| 481 | } | ||
| 482 | |||
| 483 | 31474870 | inline void Lock(const uint8_t target) { | |
| 484 | 31474870 | int retval = pthread_mutex_lock(&locks_[target]); | |
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34123355 times.
|
34123355 | assert(retval == 0); |
| 486 | 34123355 | } | |
| 487 | |||
| 488 | 31058620 | inline void Unlock(const uint8_t target) { | |
| 489 | 31058620 | int retval = pthread_mutex_unlock(&locks_[target]); | |
| 490 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34212520 times.
|
34212520 | assert(retval == 0); |
| 491 | 34212520 | } | |
| 492 | |||
| 493 | uint8_t num_hashmaps_; | ||
| 494 | SmallHashDynamic<Key, Value> *hashmaps_; | ||
| 495 | pthread_mutex_t *locks_; | ||
| 496 | }; | ||
| 497 | |||
| 498 | |||
| 499 | // initialize the static fields | ||
| 500 | template<class Key, class Value> | ||
| 501 | Prng SmallHashDynamic<Key, Value>::g_prng; | ||
| 502 | |||
| 503 | template<class Key, class Value, class Derived> | ||
| 504 | const double SmallHashBase<Key, Value, Derived>::kLoadFactor = 0.75; | ||
| 505 | |||
| 506 | template<class Key, class Value> | ||
| 507 | const double SmallHashDynamic<Key, Value>::kThresholdGrow = 0.75; | ||
| 508 | |||
| 509 | template<class Key, class Value> | ||
| 510 | const double SmallHashDynamic<Key, Value>::kThresholdShrink = 0.25; | ||
| 511 | |||
| 512 | #endif // CVMFS_SMALLHASH_H_ | ||
| 513 |