| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/smallhash.h |
| Date: | 2026-07-19 02:35:15 |
| 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 | #include "duplex_testing.h" | ||
| 9 | #include <inttypes.h> | ||
| 10 | #include <pthread.h> | ||
| 11 | #include <stdint.h> | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cassert> | ||
| 15 | #include <cstdlib> | ||
| 16 | #include <new> | ||
| 17 | |||
| 18 | #include "util/atomic.h" | ||
| 19 | #include "util/murmur.hxx" | ||
| 20 | #include "util/prng.h" | ||
| 21 | #include "util/smalloc.h" | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Hash table with linear probing as collision resolution. Works only for | ||
| 25 | * a fixed (maximum) number of elements, i.e. no resizing. Load factor fixed | ||
| 26 | * to 0.7. | ||
| 27 | */ | ||
| 28 | template<class Key, class Value, class Derived> | ||
| 29 | class SmallHashBase { | ||
| 30 | FRIEND_TEST(T_Smallhash, InsertAndCopyMd5Slow); | ||
| 31 | |||
| 32 | public: | ||
| 33 | static const double kLoadFactor; // mainly useless for the dynamic version | ||
| 34 | static const double kThresholdGrow; // only used for resizable version | ||
| 35 | static const double kThresholdShrink; // only used for resizable version | ||
| 36 | |||
| 37 | 101721 | SmallHashBase() { | |
| 38 | 101721 | keys_ = NULL; | |
| 39 | 101721 | values_ = NULL; | |
| 40 | 101721 | hasher_ = NULL; | |
| 41 | 101721 | bytes_allocated_ = 0; | |
| 42 | 101721 | num_collisions_ = 0; | |
| 43 | 101721 | max_collisions_ = 0; | |
| 44 | |||
| 45 | // Properly initialized by Init() | ||
| 46 | 101721 | capacity_ = 0; | |
| 47 | 101721 | initial_capacity_ = 0; | |
| 48 | 101721 | size_ = 0; | |
| 49 | 101721 | } | |
| 50 | |||
| 51 | 101583 | ~SmallHashBase() { DeallocMemory(keys_, values_, capacity_); } | |
| 52 | |||
| 53 | 101717 | void Init(uint32_t expected_size, Key empty, | |
| 54 | uint32_t (*hasher)(const Key &key)) { | ||
| 55 | 101717 | hasher_ = hasher; | |
| 56 | 101717 | empty_key_ = empty; | |
| 57 | 101717 | capacity_ = static_cast<uint32_t>(static_cast<double>(expected_size) | |
| 58 | 101717 | / kLoadFactor); | |
| 59 | 101717 | initial_capacity_ = capacity_; | |
| 60 | 101717 | static_cast<Derived *>(this)->SetThresholds(); // No-op for fixed size | |
| 61 | 101717 | AllocMemory(); | |
| 62 | 101717 | this->DoClear(false); | |
| 63 | 101717 | } | |
| 64 | |||
| 65 | 12594481 | bool Lookup(const Key &key, Value *value) const { | |
| 66 | uint32_t bucket; | ||
| 67 | uint32_t collisions; | ||
| 68 |
1/2✓ Branch 1 taken 12521744 times.
✗ Branch 2 not taken.
|
12594481 | const bool found = DoLookup(key, &bucket, &collisions); |
| 69 |
2/2✓ Branch 0 taken 9755014 times.
✓ Branch 1 taken 2766730 times.
|
12615391 | if (found) |
| 70 |
1/2✓ Branch 1 taken 103045 times.
✗ Branch 2 not taken.
|
9785999 | *value = values_[bucket]; |
| 71 | 12615391 | return found; | |
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Returns both the key and the value. That is useful if Key's equality | ||
| 76 | * operator implements an equivalence relation on Key. In this case, LookupEx | ||
| 77 | * returns the key representing the equivalence class that has been used | ||
| 78 | * during Insert(). | ||
| 79 | * Used to return a glue::InodeEx element when looking for an inode. | ||
| 80 | */ | ||
| 81 | 552 | bool LookupEx(Key *key, Value *value) const { | |
| 82 | 552 | uint32_t bucket = ScaleHash(*key); | |
| 83 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 92 times.
|
552 | while (!(keys_[bucket] == empty_key_)) { |
| 84 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
368 | if (keys_[bucket] == *key) { |
| 85 | 368 | *key = keys_[bucket]; | |
| 86 | 368 | *value = values_[bucket]; | |
| 87 | 368 | return true; | |
| 88 | } | ||
| 89 | ✗ | bucket = (bucket + 1) % capacity_; | |
| 90 | } | ||
| 91 | 184 | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | 46040412 | bool Contains(const Key &key) const { | |
| 95 | uint32_t bucket; | ||
| 96 | uint32_t collisions; | ||
| 97 |
1/2✓ Branch 1 taken 46040103 times.
✗ Branch 2 not taken.
|
46040412 | const bool found = DoLookup(key, &bucket, &collisions); |
| 98 | 46040412 | return found; | |
| 99 | } | ||
| 100 | |||
| 101 | 212398553 | void Insert(const Key &key, const Value &value) { | |
| 102 | 212398553 | static_cast<Derived *>(this)->Grow(); // No-op if fixed-size | |
| 103 | 211926367 | const bool overwritten = DoInsert(key, value, true); | |
| 104 | 211824808 | size_ += !overwritten; // size + 1 if the key was not yet in the map | |
| 105 | 211824808 | } | |
| 106 | |||
| 107 | 8417858 | bool Erase(const Key &key) { | |
| 108 | uint32_t bucket; | ||
| 109 | uint32_t collisions; | ||
| 110 |
1/2✓ Branch 1 taken 8210420 times.
✗ Branch 2 not taken.
|
8417858 | const bool found = DoLookup(key, &bucket, &collisions); |
| 111 |
1/2✓ Branch 0 taken 8211471 times.
✗ Branch 1 not taken.
|
8213973 | if (found) { |
| 112 | 8214879 | keys_[bucket] = empty_key_; | |
| 113 | 8214879 | size_--; | |
| 114 | 8214879 | bucket = (bucket + 1) % capacity_; | |
| 115 |
3/3✓ Branch 0 taken 59125383 times.
✓ Branch 1 taken 7916704 times.
✓ Branch 2 taken 378708 times.
|
67424203 | while (!(keys_[bucket] == empty_key_)) { |
| 116 | 59318704 | Key rehash = keys_[bucket]; | |
| 117 | 59318704 | keys_[bucket] = empty_key_; | |
| 118 |
1/2✓ Branch 1 taken 59209324 times.
✗ Branch 2 not taken.
|
59318704 | DoInsert(rehash, values_[bucket], false); |
| 119 | 59209324 | bucket = (bucket + 1) % capacity_; | |
| 120 | } | ||
| 121 |
1/2✓ Branch 1 taken 8162970 times.
✗ Branch 2 not taken.
|
8105499 | static_cast<Derived *>(this)->Shrink(); // No-op if fixed-size |
| 122 | } | ||
| 123 | 8217732 | return found; | |
| 124 | } | ||
| 125 | |||
| 126 | 627 | void Clear() { DoClear(true); } | |
| 127 | |||
| 128 | 3899 | uint64_t bytes_allocated() const { return bytes_allocated_; } | |
| 129 | 636 | static double GetEntrySize() { | |
| 130 | 1272 | const double unit = sizeof(Key) + sizeof(Value); | |
| 131 | 1272 | return unit / kLoadFactor; | |
| 132 | } | ||
| 133 | |||
| 134 | void GetCollisionStats(uint64_t *num_collisions, | ||
| 135 | uint32_t *max_collisions) const { | ||
| 136 | *num_collisions = num_collisions_; | ||
| 137 | *max_collisions = max_collisions_; | ||
| 138 | } | ||
| 139 | |||
| 140 | // Careful with the direct access TODO: iterator | ||
| 141 | uint32_t capacity() const { return capacity_; } | ||
| 142 | 161096 | Key empty_key() const { return empty_key_; } | |
| 143 | 329536 | Key *keys() const { return keys_; } | |
| 144 | 389 | Value *values() const { return values_; } | |
| 145 | |||
| 146 | // Only needed by compat | ||
| 147 | ✗ | void SetHasher(uint32_t (*hasher)(const Key &key)) { hasher_ = hasher; } | |
| 148 | |||
| 149 | protected: | ||
| 150 | 401718279 | uint32_t ScaleHash(const Key &key) const { | |
| 151 | 401718279 | const double bucket = (static_cast<double>(hasher_(key)) | |
| 152 | 401247444 | * static_cast<double>(capacity_) | |
| 153 | / static_cast<double>(static_cast<uint32_t>(-1))); | ||
| 154 | 401247444 | return static_cast<uint32_t>(bucket) % capacity_; | |
| 155 | } | ||
| 156 | |||
| 157 | 113063 | void AllocMemory() { | |
| 158 | 113063 | keys_ = static_cast<Key *>(smmap(capacity_ * sizeof(Key))); | |
| 159 | 113063 | values_ = static_cast<Value *>(smmap(capacity_ * sizeof(Value))); | |
| 160 |
2/2✓ Branch 0 taken 696086255 times.
✓ Branch 1 taken 57953 times.
|
1388655408 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 161 | 1388542349 | /*keys_[i] =*/new (keys_ + i) Key(); | |
| 162 | } | ||
| 163 |
2/2✓ Branch 0 taken 696360019 times.
✓ Branch 1 taken 55589 times.
|
1389198208 | for (uint32_t i = 0; i < capacity_; ++i) { |
| 164 | 1389089877 | /*values_[i] =*/new (values_ + i) Value(); | |
| 165 | } | ||
| 166 | 108331 | bytes_allocated_ = (sizeof(Key) + sizeof(Value)) * capacity_; | |
| 167 | 108331 | } | |
| 168 | |||
| 169 | 112929 | void DeallocMemory(Key *k, Value *v, uint32_t c) { | |
| 170 |
2/2✓ Branch 0 taken 696254167 times.
✓ Branch 1 taken 57887 times.
|
1388993832 | for (uint32_t i = 0; i < c; ++i) { |
| 171 | 1388880903 | k[i].~Key(); | |
| 172 | } | ||
| 173 |
2/2✓ Branch 0 taken 696329267 times.
✓ Branch 1 taken 57887 times.
|
1389144032 | for (uint32_t i = 0; i < c; ++i) { |
| 174 | 1389031103 | v[i].~Value(); | |
| 175 | } | ||
| 176 |
2/2✓ Branch 0 taken 57885 times.
✓ Branch 1 taken 2 times.
|
112929 | if (k) |
| 177 | 112925 | smunmap(k); | |
| 178 |
2/2✓ Branch 0 taken 57885 times.
✓ Branch 1 taken 2 times.
|
112929 | if (v) |
| 179 | 112925 | smunmap(v); | |
| 180 | 112929 | k = NULL; | |
| 181 | 112929 | v = NULL; | |
| 182 | 112929 | } | |
| 183 | |||
| 184 | // Returns true iff the key is overwritten | ||
| 185 | 280528037 | bool DoInsert(const Key &key, const Value &value, | |
| 186 | const bool count_collisions) { | ||
| 187 | uint32_t bucket; | ||
| 188 | uint32_t collisions; | ||
| 189 |
1/2✓ Branch 1 taken 168260701 times.
✗ Branch 2 not taken.
|
280528037 | const bool overwritten = DoLookup(key, &bucket, &collisions); |
| 190 |
2/2✓ Branch 0 taken 109072471 times.
✓ Branch 1 taken 59188230 times.
|
280370930 | if (count_collisions) { |
| 191 | 211910714 | num_collisions_ += collisions; | |
| 192 | 211910714 | max_collisions_ = std::max(collisions, max_collisions_); | |
| 193 | } | ||
| 194 | 280313658 | keys_[bucket] = key; | |
| 195 |
1/2✓ Branch 1 taken 49995035 times.
✗ Branch 2 not taken.
|
280313658 | values_[bucket] = value; |
| 196 | 280313658 | return overwritten; | |
| 197 | } | ||
| 198 | |||
| 199 | 401812162 | bool DoLookup(const Key &key, uint32_t *bucket, uint32_t *collisions) const { | |
| 200 | 401812162 | *bucket = ScaleHash(key); | |
| 201 | 401217127 | *collisions = 0; | |
| 202 |
3/3✓ Branch 0 taken 25953698906 times.
✓ Branch 1 taken 282626307 times.
✓ Branch 2 taken 60073215 times.
|
26651043630 | while (!(keys_[*bucket] == empty_key_)) { |
| 203 |
3/3✓ Branch 0 taken 17829119 times.
✓ Branch 1 taken 25985572755 times.
✓ Branch 2 taken 126380923 times.
|
26373100236 | if (keys_[*bucket] == key) |
| 204 | 123273733 | return true; | |
| 205 | 26249826503 | *bucket = (*bucket + 1) % capacity_; | |
| 206 | 26249826503 | (*collisions)++; | |
| 207 | } | ||
| 208 | 277943394 | return false; | |
| 209 | } | ||
| 210 | |||
| 211 | 113159 | void DoClear(const bool reset_capacity) { | |
| 212 |
2/2✓ Branch 0 taken 627 times.
✓ Branch 1 taken 57424 times.
|
113159 | if (reset_capacity) |
| 213 | 716 | static_cast<Derived *>(this)->ResetCapacity(); // No-op if fixed-size | |
| 214 |
2/2✓ Branch 0 taken 696638273 times.
✓ Branch 1 taken 58051 times.
|
1389628504 | for (uint32_t i = 0; i < capacity_; ++i) |
| 215 | 1389515345 | keys_[i] = empty_key_; | |
| 216 | 113159 | size_ = 0; | |
| 217 | 113159 | } | |
| 218 | |||
| 219 | // Methods for resizable version | ||
| 220 | void SetThresholds() { } | ||
| 221 | void Grow() { } | ||
| 222 | void Shrink() { } | ||
| 223 | void ResetCapacity() { } | ||
| 224 | |||
| 225 | // Separate key and value arrays for better locality | ||
| 226 | Key *keys_; | ||
| 227 | Value *values_; | ||
| 228 | uint32_t capacity_; | ||
| 229 | uint32_t initial_capacity_; | ||
| 230 | uint32_t size_; | ||
| 231 | uint32_t (*hasher_)(const Key &key); | ||
| 232 | uint64_t bytes_allocated_; | ||
| 233 | uint64_t num_collisions_; | ||
| 234 | uint32_t max_collisions_; /**< maximum collisions for a single insert */ | ||
| 235 | Key empty_key_; | ||
| 236 | }; | ||
| 237 | |||
| 238 | |||
| 239 | template<class Key, class Value> | ||
| 240 | class SmallHashFixed | ||
| 241 | : public SmallHashBase<Key, Value, SmallHashFixed<Key, Value> > { | ||
| 242 | friend class SmallHashBase<Key, Value, SmallHashFixed<Key, Value> >; | ||
| 243 | |||
| 244 | protected: | ||
| 245 | // No-ops | ||
| 246 | 3803 | void SetThresholds() { } | |
| 247 | 1285742 | void Grow() { } | |
| 248 | 52260 | void Shrink() { } | |
| 249 | 96 | void ResetCapacity() { } | |
| 250 | }; | ||
| 251 | |||
| 252 | |||
| 253 | template<class Key, class Value> | ||
| 254 | class SmallHashDynamic | ||
| 255 | : public SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > { | ||
| 256 | friend class SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> >; | ||
| 257 | |||
| 258 | public: | ||
| 259 | typedef SmallHashBase<Key, Value, SmallHashDynamic<Key, Value> > Base; | ||
| 260 | static const double kThresholdGrow; | ||
| 261 | static const double kThresholdShrink; | ||
| 262 | |||
| 263 | 97918 | SmallHashDynamic() : Base() { | |
| 264 | 97918 | num_migrates_ = 0; | |
| 265 | |||
| 266 | // Properly set by Init | ||
| 267 | 97918 | threshold_grow_ = 0; | |
| 268 | 97918 | threshold_shrink_ = 0; | |
| 269 | 97918 | } | |
| 270 | |||
| 271 | SmallHashDynamic(const SmallHashDynamic<Key, Value> &other) : Base() { | ||
| 272 | num_migrates_ = 0; | ||
| 273 | CopyFrom(other); | ||
| 274 | } | ||
| 275 | |||
| 276 | 138 | SmallHashDynamic<Key, Value> &operator=( | |
| 277 | const SmallHashDynamic<Key, Value> &other) { | ||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
|
138 | if (&other == this) |
| 279 | ✗ | return *this; | |
| 280 | |||
| 281 | 138 | CopyFrom(other); | |
| 282 | 138 | return *this; | |
| 283 | } | ||
| 284 | |||
| 285 | 6427992 | uint32_t capacity() const { return Base::capacity_; } | |
| 286 | 116394891 | uint32_t size() const { return Base::size_; } | |
| 287 | uint32_t num_migrates() const { return num_migrates_; } | ||
| 288 | |||
| 289 | protected: | ||
| 290 | 109256 | void SetThresholds() { | |
| 291 | 109256 | threshold_grow_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 292 | 109256 | * kThresholdGrow); | |
| 293 | 109256 | threshold_shrink_ = static_cast<uint32_t>(static_cast<double>(capacity()) | |
| 294 | 109260 | * kThresholdShrink); | |
| 295 | 109260 | } | |
| 296 | |||
| 297 | 210973057 | void Grow() { | |
| 298 |
2/2✓ Branch 1 taken 3962 times.
✓ Branch 2 taken 107840099 times.
|
210973057 | if (size() > threshold_grow_) |
| 299 | 7924 | Migrate(capacity() * 2); | |
| 300 | 210742796 | } | |
| 301 | |||
| 302 | 8143661 | void Shrink() { | |
| 303 |
2/2✓ Branch 1 taken 2863485 times.
✓ Branch 2 taken 5286434 times.
|
8143661 | if (size() < threshold_shrink_) { |
| 304 | 2866893 | const uint32_t target_capacity = capacity() / 2; | |
| 305 |
2/2✓ Branch 0 taken 1376 times.
✓ Branch 1 taken 2861469 times.
|
2866253 | if (target_capacity >= Base::initial_capacity_) |
| 306 | 1376 | Migrate(target_capacity); | |
| 307 | } | ||
| 308 | 8152687 | } | |
| 309 | |||
| 310 | 620 | void ResetCapacity() { | |
| 311 | 620 | Base::DeallocMemory(Base::keys_, Base::values_, Base::capacity_); | |
| 312 | 620 | Base::capacity_ = Base::initial_capacity_; | |
| 313 | 620 | Base::AllocMemory(); | |
| 314 | 620 | SetThresholds(); | |
| 315 | 620 | } | |
| 316 | |||
| 317 | private: | ||
| 318 | // Returns a random permutation of indices [0..N-1] that is allocated | ||
| 319 | // by smmap (Knuth's shuffle algorithm) | ||
| 320 | 2892 | uint32_t *ShuffleIndices(const uint32_t N) { | |
| 321 | 2892 | uint32_t *shuffled = static_cast<uint32_t *>(smmap(N * sizeof(uint32_t))); | |
| 322 | // Init with identity | ||
| 323 |
2/2✓ Branch 0 taken 22527888 times.
✓ Branch 1 taken 1446 times.
|
45058668 | for (unsigned i = 0; i < N; ++i) |
| 324 | 45055776 | shuffled[i] = i; | |
| 325 | // Shuffle (no shuffling for the last element) | ||
| 326 |
1/2✓ Branch 0 taken 21343924 times.
✗ Branch 1 not taken.
|
42681360 | for (unsigned i = 0; i < N - 1; ++i) { |
| 327 | 42687848 | const uint32_t swap_idx = i + g_prng.Next(N - i); | |
| 328 | 42678468 | const uint32_t tmp = shuffled[i]; | |
| 329 | 42678468 | shuffled[i] = shuffled[swap_idx]; | |
| 330 | 42678468 | shuffled[swap_idx] = tmp; | |
| 331 | } | ||
| 332 | 138 | return shuffled; | |
| 333 | } | ||
| 334 | |||
| 335 | 10672 | void Migrate(const uint32_t new_capacity) { | |
| 336 | 10672 | Key *old_keys = Base::keys_; | |
| 337 | 10672 | Value *old_values = Base::values_; | |
| 338 | 10672 | const uint32_t old_capacity = capacity(); | |
| 339 | 10676 | const uint32_t old_size = size(); | |
| 340 | |||
| 341 | 10676 | Base::capacity_ = new_capacity; | |
| 342 | 10676 | SetThresholds(); | |
| 343 | 10680 | Base::AllocMemory(); | |
| 344 | 10680 | Base::DoClear(false); | |
| 345 |
2/2✓ Branch 0 taken 1376 times.
✓ Branch 1 taken 3964 times.
|
10680 | if (new_capacity < old_capacity) { |
| 346 | 2752 | uint32_t *shuffled_indices = ShuffleIndices(old_capacity); | |
| 347 |
1/2✓ Branch 0 taken 19284578 times.
✗ Branch 1 not taken.
|
38552500 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 348 |
2/3✓ Branch 0 taken 4842480 times.
✓ Branch 1 taken 14442098 times.
✗ Branch 2 not taken.
|
38569156 | if (old_keys[shuffled_indices[i]] != Base::empty_key_) { |
| 349 | 9684960 | Base::Insert(old_keys[shuffled_indices[i]], | |
| 350 | 9684960 | old_values[shuffled_indices[i]]); | |
| 351 | } | ||
| 352 | } | ||
| 353 | ✗ | smunmap(shuffled_indices); | |
| 354 | } else { | ||
| 355 |
2/2✓ Branch 0 taken 37243172 times.
✓ Branch 1 taken 2906 times.
|
74492156 | for (uint32_t i = 0; i < old_capacity; ++i) { |
| 356 |
3/3✓ Branch 0 taken 21022390 times.
✓ Branch 1 taken 13915940 times.
✓ Branch 2 taken 2304842 times.
|
74486344 | if (old_keys[i] != Base::empty_key_) |
| 357 | 55879984 | Base::Insert(old_keys[i], old_values[i]); | |
| 358 | } | ||
| 359 | } | ||
| 360 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5340 times.
|
8564 | assert(size() == old_size); |
| 361 | |||
| 362 | 10680 | Base::DeallocMemory(old_keys, old_values, old_capacity); | |
| 363 | 10680 | num_migrates_++; | |
| 364 | 10680 | } | |
| 365 | |||
| 366 | 138 | void CopyFrom(const SmallHashDynamic<Key, Value> &other) { | |
| 367 | 138 | uint32_t *shuffled_indices = ShuffleIndices(other.capacity_); | |
| 368 |
2/2✓ Branch 0 taken 2753940 times.
✓ Branch 1 taken 70 times.
|
2755506 | for (uint32_t i = 0; i < other.capacity_; ++i) { |
| 369 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 2000714 times.
✓ Branch 2 taken 753226 times.
|
2755368 | if (other.keys_[shuffled_indices[i]] != other.empty_key_) { |
| 370 | 2000000 | this->Insert(other.keys_[shuffled_indices[i]], | |
| 371 | 2000000 | other.values_[shuffled_indices[i]]); | |
| 372 | } | ||
| 373 | } | ||
| 374 | 138 | smunmap(shuffled_indices); | |
| 375 | 138 | } | |
| 376 | |||
| 377 | uint32_t num_migrates_; | ||
| 378 | uint32_t threshold_grow_; | ||
| 379 | uint32_t threshold_shrink_; | ||
| 380 | static Prng g_prng; | ||
| 381 | }; | ||
| 382 | |||
| 383 | |||
| 384 | /** | ||
| 385 | * Distributes the key-value pairs over $n$ dynamic hash maps with individual | ||
| 386 | * mutexes. Hence low mutex contention, and benefits from multiple processors. | ||
| 387 | */ | ||
| 388 | template<class Key, class Value> | ||
| 389 | class MultiHash { | ||
| 390 | public: | ||
| 391 | 20 | MultiHash() { | |
| 392 | 20 | num_hashmaps_ = 0; | |
| 393 | 20 | hashmaps_ = NULL; | |
| 394 | 20 | locks_ = NULL; | |
| 395 | 20 | } | |
| 396 | |||
| 397 | 20 | void Init(const uint8_t num_hashmaps, const Key &empty_key, | |
| 398 | uint32_t (*hasher)(const Key &key)) { | ||
| 399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | assert(num_hashmaps > 0); |
| 400 | 20 | const uint8_t N = num_hashmaps; | |
| 401 | 20 | num_hashmaps_ = N; | |
| 402 |
3/4✓ Branch 2 taken 840 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 840 times.
✓ Branch 5 taken 20 times.
|
860 | hashmaps_ = new SmallHashDynamic<Key, Value>[N](); |
| 403 | 20 | locks_ = static_cast<pthread_mutex_t *>( | |
| 404 | 20 | smalloc(N * sizeof(pthread_mutex_t))); | |
| 405 |
2/2✓ Branch 0 taken 840 times.
✓ Branch 1 taken 20 times.
|
860 | for (uint8_t i = 0; i < N; ++i) { |
| 406 | 840 | int retval = pthread_mutex_init(&locks_[i], NULL); | |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
|
840 | assert(retval == 0); |
| 408 | 840 | hashmaps_[i].Init(128, empty_key, hasher); | |
| 409 | } | ||
| 410 | 20 | } | |
| 411 | |||
| 412 | 20 | ~MultiHash() { | |
| 413 |
2/2✓ Branch 0 taken 840 times.
✓ Branch 1 taken 20 times.
|
860 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 414 | 840 | pthread_mutex_destroy(&locks_[i]); | |
| 415 | } | ||
| 416 | 20 | free(locks_); | |
| 417 |
3/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 840 times.
✓ Branch 3 taken 20 times.
|
860 | delete[] hashmaps_; |
| 418 | 20 | } | |
| 419 | |||
| 420 | 2000000 | bool Lookup(const Key &key, Value *value) { | |
| 421 | 2000000 | uint8_t target = SelectHashmap(key); | |
| 422 | 2000000 | Lock(target); | |
| 423 | 2000000 | const bool result = hashmaps_[target].Lookup(key, value); | |
| 424 | 2000000 | Unlock(target); | |
| 425 | 2000000 | return result; | |
| 426 | } | ||
| 427 | |||
| 428 | 7466752 | void Insert(const Key &key, const Value &value) { | |
| 429 | 7466752 | uint8_t target = SelectHashmap(key); | |
| 430 | 7259802 | Lock(target); | |
| 431 | 7540792 | hashmaps_[target].Insert(key, value); | |
| 432 | 7123484 | Unlock(target); | |
| 433 | 7777922 | } | |
| 434 | |||
| 435 | 3597636 | void Erase(const Key &key) { | |
| 436 | 3597636 | uint8_t target = SelectHashmap(key); | |
| 437 | 3437026 | Lock(target); | |
| 438 | 3670128 | hashmaps_[target].Erase(key); | |
| 439 | 3289042 | Unlock(target); | |
| 440 | 3726658 | } | |
| 441 | |||
| 442 | 2 | void Clear() { | |
| 443 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
|
86 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 444 | 84 | Lock(i); | |
| 445 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
|
86 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 446 | 84 | hashmaps_[i].Clear(); | |
| 447 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
|
86 | for (uint8_t i = 0; i < num_hashmaps_; ++i) |
| 448 | 84 | Unlock(i); | |
| 449 | 2 | } | |
| 450 | |||
| 451 | 20 | uint8_t num_hashmaps() const { return num_hashmaps_; } | |
| 452 | |||
| 453 | 16 | void GetSizes(uint32_t *sizes) { | |
| 454 |
2/2✓ Branch 0 taken 672 times.
✓ Branch 1 taken 16 times.
|
688 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { |
| 455 | 672 | Lock(i); | |
| 456 | 672 | sizes[i] = hashmaps_[i].size(); | |
| 457 | 672 | Unlock(i); | |
| 458 | } | ||
| 459 | 16 | } | |
| 460 | |||
| 461 | void GetCollisionStats(uint64_t *num_collisions, uint32_t *max_collisions) { | ||
| 462 | for (uint8_t i = 0; i < num_hashmaps_; ++i) { | ||
| 463 | Lock(i); | ||
| 464 | hashmaps_[i].GetCollisionStats(&num_collisions[i], &max_collisions[i]); | ||
| 465 | Unlock(i); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | private: | ||
| 470 | 12972174 | inline uint8_t SelectHashmap(const Key &key) { | |
| 471 | 12972174 | uint32_t hash = MurmurHash2(&key, sizeof(key), 0x37); | |
| 472 | 12756080 | double bucket = static_cast<double>(hash) | |
| 473 | 12756080 | * static_cast<double>(num_hashmaps_) | |
| 474 | / static_cast<double>(static_cast<uint32_t>(-1)); | ||
| 475 | 12756080 | return static_cast<uint32_t>(bucket) % num_hashmaps_; | |
| 476 | } | ||
| 477 | |||
| 478 | 12653324 | inline void Lock(const uint8_t target) { | |
| 479 | 12653324 | int retval = pthread_mutex_lock(&locks_[target]); | |
| 480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13671248 times.
|
13671248 | assert(retval == 0); |
| 481 | 13671248 | } | |
| 482 | |||
| 483 | 12430660 | inline void Unlock(const uint8_t target) { | |
| 484 | 12430660 | int retval = pthread_mutex_unlock(&locks_[target]); | |
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13765466 times.
|
13765466 | assert(retval == 0); |
| 486 | 13765466 | } | |
| 487 | |||
| 488 | uint8_t num_hashmaps_; | ||
| 489 | SmallHashDynamic<Key, Value> *hashmaps_; | ||
| 490 | pthread_mutex_t *locks_; | ||
| 491 | }; | ||
| 492 | |||
| 493 | |||
| 494 | // initialize the static fields | ||
| 495 | template<class Key, class Value> | ||
| 496 | Prng SmallHashDynamic<Key, Value>::g_prng; | ||
| 497 | |||
| 498 | template<class Key, class Value, class Derived> | ||
| 499 | const double SmallHashBase<Key, Value, Derived>::kLoadFactor = 0.75; | ||
| 500 | |||
| 501 | template<class Key, class Value> | ||
| 502 | const double SmallHashDynamic<Key, Value>::kThresholdGrow = 0.75; | ||
| 503 | |||
| 504 | template<class Key, class Value> | ||
| 505 | const double SmallHashDynamic<Key, Value>::kThresholdShrink = 0.25; | ||
| 506 | |||
| 507 | #endif // CVMFS_SMALLHASH_H_ | ||
| 508 |