| 1 | //===--- StringMap.cpp - String Hash table map implementation -------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the StringMap class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/ADT/StringMap.h" |
| 14 | #include "llvm/Support/MathExtras.h" |
| 15 | #include "llvm/Support/ReverseIteration.h" |
| 16 | #include "llvm/Support/xxhash.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | /// Returns the number of buckets to allocate to ensure that the DenseMap can |
| 21 | /// accommodate \p NumEntries without need to grow(). |
| 22 | static inline unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { |
| 23 | // Ensure that "NumEntries * 4 < NumBuckets * 3" |
| 24 | if (NumEntries == 0) |
| 25 | return 0; |
| 26 | // +1 is required because of the strict equality. |
| 27 | // For example if NumEntries is 48, we need to return 401. |
| 28 | return NextPowerOf2(A: NumEntries * 4 / 3 + 1); |
| 29 | } |
| 30 | |
| 31 | static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) { |
| 32 | auto **Table = static_cast<StringMapEntryBase **>(safe_calloc( |
| 33 | Count: NewNumBuckets + 1, Sz: sizeof(StringMapEntryBase **) + sizeof(unsigned))); |
| 34 | |
| 35 | // Allocate one extra bucket, set it to look filled so the iterators stop at |
| 36 | // end. |
| 37 | Table[NewNumBuckets] = (StringMapEntryBase *)2; |
| 38 | return Table; |
| 39 | } |
| 40 | |
| 41 | static inline unsigned *getHashTable(StringMapEntryBase **TheTable, |
| 42 | unsigned NumBuckets) { |
| 43 | return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1); |
| 44 | } |
| 45 | |
| 46 | uint32_t StringMapImpl::hash(StringRef Key) { return xxh3_64bits(data: Key); } |
| 47 | |
| 48 | StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) |
| 49 | : ItemSize(itemSize) { |
| 50 | // If a size is specified, initialize the table with that many buckets. |
| 51 | if (InitSize) { |
| 52 | // The table will grow when the number of entries reach 3/4 of the number of |
| 53 | // buckets. To guarantee that "InitSize" number of entries can be inserted |
| 54 | // in the table without growing, we allocate just what is needed here. |
| 55 | init(Size: getMinBucketToReserveForEntries(NumEntries: InitSize)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void StringMapImpl::init(unsigned InitSize) { |
| 60 | assert((InitSize & (InitSize - 1)) == 0 && |
| 61 | "Init Size must be a power of 2 or zero!" ); |
| 62 | |
| 63 | unsigned NewNumBuckets = InitSize ? InitSize : 16; |
| 64 | NumItems = 0; |
| 65 | |
| 66 | TheTable = createTable(NewNumBuckets); |
| 67 | |
| 68 | // Set the member only if TheTable was successfully allocated |
| 69 | NumBuckets = NewNumBuckets; |
| 70 | } |
| 71 | |
| 72 | /// LookupBucketFor - Look up the bucket that the specified string should end |
| 73 | /// up in. If it already exists as a key in the map, the Item pointer for the |
| 74 | /// specified bucket will be non-null. Otherwise, it will be null. In either |
| 75 | /// case, the FullHashValue field of the bucket will be set to the hash value |
| 76 | /// of the string. |
| 77 | unsigned StringMapImpl::LookupBucketFor(StringRef Name, |
| 78 | uint32_t FullHashValue) { |
| 79 | #ifdef EXPENSIVE_CHECKS |
| 80 | assert(FullHashValue == hash(Name)); |
| 81 | #endif |
| 82 | // Hash table unallocated so far? |
| 83 | if (NumBuckets == 0) |
| 84 | init(InitSize: 16); |
| 85 | if constexpr (shouldReverseIterate()) |
| 86 | FullHashValue = ~FullHashValue; |
| 87 | unsigned BucketNo = FullHashValue & (NumBuckets - 1); |
| 88 | unsigned *HashTable = getHashTable(TheTable, NumBuckets); |
| 89 | |
| 90 | while (true) { |
| 91 | StringMapEntryBase *BucketItem = TheTable[BucketNo]; |
| 92 | // If we found an empty bucket, this key isn't in the table yet, return it. |
| 93 | if (LLVM_LIKELY(!BucketItem)) { |
| 94 | HashTable[BucketNo] = FullHashValue; |
| 95 | return BucketNo; |
| 96 | } |
| 97 | |
| 98 | if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { |
| 99 | // If the full hash value matches, check deeply for a match. The common |
| 100 | // case here is that we are only looking at the buckets (for item info |
| 101 | // being non-null and for the full hash value) not at the items. This |
| 102 | // is important for cache locality. |
| 103 | |
| 104 | // Do the comparison like this because Name isn't necessarily |
| 105 | // null-terminated! |
| 106 | char *ItemStr = (char *)BucketItem + ItemSize; |
| 107 | if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) { |
| 108 | // We found a match! |
| 109 | return BucketNo; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Okay, we didn't find the item. Probe to the next bucket. |
| 114 | BucketNo = (BucketNo + 1) & (NumBuckets - 1); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// FindKey - Look up the bucket that contains the specified key. If it exists |
| 119 | /// in the map, return the bucket number of the key. Otherwise return -1. |
| 120 | /// This does not modify the map. |
| 121 | int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const { |
| 122 | if (NumBuckets == 0) |
| 123 | return -1; // Really empty table? |
| 124 | #ifdef EXPENSIVE_CHECKS |
| 125 | assert(FullHashValue == hash(Key)); |
| 126 | #endif |
| 127 | if constexpr (shouldReverseIterate()) |
| 128 | FullHashValue = ~FullHashValue; |
| 129 | unsigned BucketNo = FullHashValue & (NumBuckets - 1); |
| 130 | unsigned *HashTable = getHashTable(TheTable, NumBuckets); |
| 131 | |
| 132 | while (true) { |
| 133 | StringMapEntryBase *BucketItem = TheTable[BucketNo]; |
| 134 | // If we found an empty bucket, this key isn't in the table yet, return. |
| 135 | if (LLVM_LIKELY(!BucketItem)) |
| 136 | return -1; |
| 137 | |
| 138 | if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { |
| 139 | // If the full hash value matches, check deeply for a match. The common |
| 140 | // case here is that we are only looking at the buckets (for item info |
| 141 | // being non-null and for the full hash value) not at the items. This |
| 142 | // is important for cache locality. |
| 143 | |
| 144 | // Do the comparison like this because NameStart isn't necessarily |
| 145 | // null-terminated! |
| 146 | char *ItemStr = (char *)BucketItem + ItemSize; |
| 147 | if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) { |
| 148 | // We found a match! |
| 149 | return BucketNo; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Okay, we didn't find the item. Probe to the next bucket. |
| 154 | BucketNo = (BucketNo + 1) & (NumBuckets - 1); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// RemoveKey - Remove the specified StringMapEntry from the table, but do not |
| 159 | /// delete it. This aborts if the value isn't in the table. |
| 160 | void StringMapImpl::RemoveKey(StringMapEntryBase *V) { |
| 161 | const char *VStr = (char *)V + ItemSize; |
| 162 | StringMapEntryBase *V2 = RemoveKey(Key: StringRef(VStr, V->getKeyLength())); |
| 163 | (void)V2; |
| 164 | assert(V == V2 && "Didn't find key?" ); |
| 165 | } |
| 166 | |
| 167 | // Knuth TAOCP 6.4 Algorithm R: walk forward sliding each following entry |
| 168 | // whose probe path crosses the hole. |
| 169 | void StringMapImpl::removeBucket(unsigned Bucket) { |
| 170 | unsigned *HashTable = getHashTable(TheTable, NumBuckets); |
| 171 | unsigned Mask = NumBuckets - 1; |
| 172 | unsigned I = Bucket, J = I; |
| 173 | while ((J = (J + 1) & Mask), TheTable[J]) { |
| 174 | unsigned Ideal = HashTable[J]; |
| 175 | if (((I - Ideal) & Mask) < ((J - Ideal) & Mask)) { |
| 176 | TheTable[I] = TheTable[J]; |
| 177 | HashTable[I] = HashTable[J]; |
| 178 | I = J; |
| 179 | } |
| 180 | } |
| 181 | TheTable[I] = nullptr; |
| 182 | --NumItems; |
| 183 | } |
| 184 | |
| 185 | StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) { |
| 186 | int Bucket = FindKey(Key); |
| 187 | if (Bucket == -1) |
| 188 | return nullptr; |
| 189 | |
| 190 | StringMapEntryBase *Result = TheTable[Bucket]; |
| 191 | removeBucket(Bucket); |
| 192 | return Result; |
| 193 | } |
| 194 | |
| 195 | /// RehashTable - Grow the table, redistributing values into the buckets with |
| 196 | /// the appropriate mod-of-hashtable-size. |
| 197 | unsigned StringMapImpl::RehashTable(unsigned BucketNo) { |
| 198 | unsigned NewSize; |
| 199 | // If the hash table is now more than 3/4 full, grow the table. |
| 200 | if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) { |
| 201 | NewSize = NumBuckets * 2; |
| 202 | } else { |
| 203 | return BucketNo; |
| 204 | } |
| 205 | |
| 206 | unsigned NewBucketNo = BucketNo; |
| 207 | auto **NewTableArray = createTable(NewNumBuckets: NewSize); |
| 208 | unsigned *NewHashArray = getHashTable(TheTable: NewTableArray, NumBuckets: NewSize); |
| 209 | unsigned *HashTable = getHashTable(TheTable, NumBuckets); |
| 210 | |
| 211 | // Rehash all the items into their new buckets. Luckily :) we already have |
| 212 | // the hash values available, so we don't have to rehash any strings. |
| 213 | for (unsigned I = 0, E = NumBuckets; I != E; ++I) { |
| 214 | StringMapEntryBase *Bucket = TheTable[I]; |
| 215 | if (Bucket) { |
| 216 | // If the bucket is not available, probe for a spot. |
| 217 | unsigned FullHash = HashTable[I]; |
| 218 | unsigned NewBucket = FullHash & (NewSize - 1); |
| 219 | while (NewTableArray[NewBucket]) |
| 220 | NewBucket = (NewBucket + 1) & (NewSize - 1); |
| 221 | |
| 222 | // Finally found a slot. Fill it in. |
| 223 | NewTableArray[NewBucket] = Bucket; |
| 224 | NewHashArray[NewBucket] = FullHash; |
| 225 | if (I == BucketNo) |
| 226 | NewBucketNo = NewBucket; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | free(ptr: TheTable); |
| 231 | |
| 232 | TheTable = NewTableArray; |
| 233 | NumBuckets = NewSize; |
| 234 | return NewBucketNo; |
| 235 | } |
| 236 | |