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/DJB.h" 15 #include "llvm/Support/MathExtras.h" 16 17 using namespace llvm; 18 19 /// Returns the number of buckets to allocate to ensure that the DenseMap can 20 /// accommodate \p NumEntries without need to grow(). 21 static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) { 22 // Ensure that "NumEntries * 4 < NumBuckets * 3" 23 if (NumEntries == 0) 24 return 0; 25 // +1 is required because of the strict equality. 26 // For example if NumEntries is 48, we need to return 401. 27 return NextPowerOf2(NumEntries * 4 / 3 + 1); 28 } 29 30 StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) { 31 ItemSize = itemSize; 32 33 // If a size is specified, initialize the table with that many buckets. 34 if (InitSize) { 35 // The table will grow when the number of entries reach 3/4 of the number of 36 // buckets. To guarantee that "InitSize" number of entries can be inserted 37 // in the table without growing, we allocate just what is needed here. 38 init(getMinBucketToReserveForEntries(InitSize)); 39 return; 40 } 41 42 // Otherwise, initialize it with zero buckets to avoid the allocation. 43 TheTable = nullptr; 44 NumBuckets = 0; 45 NumItems = 0; 46 NumTombstones = 0; 47 } 48 49 void StringMapImpl::init(unsigned InitSize) { 50 assert((InitSize & (InitSize - 1)) == 0 && 51 "Init Size must be a power of 2 or zero!"); 52 53 unsigned NewNumBuckets = InitSize ? InitSize : 16; 54 NumItems = 0; 55 NumTombstones = 0; 56 57 TheTable = static_cast<StringMapEntryBase **>(safe_calloc( 58 NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned))); 59 60 // Set the member only if TheTable was successfully allocated 61 NumBuckets = NewNumBuckets; 62 63 // Allocate one extra bucket, set it to look filled so the iterators stop at 64 // end. 65 TheTable[NumBuckets] = (StringMapEntryBase *)2; 66 } 67 68 /// LookupBucketFor - Look up the bucket that the specified string should end 69 /// up in. If it already exists as a key in the map, the Item pointer for the 70 /// specified bucket will be non-null. Otherwise, it will be null. In either 71 /// case, the FullHashValue field of the bucket will be set to the hash value 72 /// of the string. 73 unsigned StringMapImpl::LookupBucketFor(StringRef Name) { 74 unsigned HTSize = NumBuckets; 75 if (HTSize == 0) { // Hash table unallocated so far? 76 init(16); 77 HTSize = NumBuckets; 78 } 79 unsigned FullHashValue = djbHash(Name, 0); 80 unsigned BucketNo = FullHashValue & (HTSize - 1); 81 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); 82 83 unsigned ProbeAmt = 1; 84 int FirstTombstone = -1; 85 while (true) { 86 StringMapEntryBase *BucketItem = TheTable[BucketNo]; 87 // If we found an empty bucket, this key isn't in the table yet, return it. 88 if (LLVM_LIKELY(!BucketItem)) { 89 // If we found a tombstone, we want to reuse the tombstone instead of an 90 // empty bucket. This reduces probing. 91 if (FirstTombstone != -1) { 92 HashTable[FirstTombstone] = FullHashValue; 93 return FirstTombstone; 94 } 95 96 HashTable[BucketNo] = FullHashValue; 97 return BucketNo; 98 } 99 100 if (BucketItem == getTombstoneVal()) { 101 // Skip over tombstones. However, remember the first one we see. 102 if (FirstTombstone == -1) 103 FirstTombstone = BucketNo; 104 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { 105 // If the full hash value matches, check deeply for a match. The common 106 // case here is that we are only looking at the buckets (for item info 107 // being non-null and for the full hash value) not at the items. This 108 // is important for cache locality. 109 110 // Do the comparison like this because Name isn't necessarily 111 // null-terminated! 112 char *ItemStr = (char *)BucketItem + ItemSize; 113 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) { 114 // We found a match! 115 return BucketNo; 116 } 117 } 118 119 // Okay, we didn't find the item. Probe to the next bucket. 120 BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1); 121 122 // Use quadratic probing, it has fewer clumping artifacts than linear 123 // probing and has good cache behavior in the common case. 124 ++ProbeAmt; 125 } 126 } 127 128 /// FindKey - Look up the bucket that contains the specified key. If it exists 129 /// in the map, return the bucket number of the key. Otherwise return -1. 130 /// This does not modify the map. 131 int StringMapImpl::FindKey(StringRef Key) const { 132 unsigned HTSize = NumBuckets; 133 if (HTSize == 0) 134 return -1; // Really empty table? 135 unsigned FullHashValue = djbHash(Key, 0); 136 unsigned BucketNo = FullHashValue & (HTSize - 1); 137 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); 138 139 unsigned ProbeAmt = 1; 140 while (true) { 141 StringMapEntryBase *BucketItem = TheTable[BucketNo]; 142 // If we found an empty bucket, this key isn't in the table yet, return. 143 if (LLVM_LIKELY(!BucketItem)) 144 return -1; 145 146 if (BucketItem == getTombstoneVal()) { 147 // Ignore tombstones. 148 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) { 149 // If the full hash value matches, check deeply for a match. The common 150 // case here is that we are only looking at the buckets (for item info 151 // being non-null and for the full hash value) not at the items. This 152 // is important for cache locality. 153 154 // Do the comparison like this because NameStart isn't necessarily 155 // null-terminated! 156 char *ItemStr = (char *)BucketItem + ItemSize; 157 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) { 158 // We found a match! 159 return BucketNo; 160 } 161 } 162 163 // Okay, we didn't find the item. Probe to the next bucket. 164 BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1); 165 166 // Use quadratic probing, it has fewer clumping artifacts than linear 167 // probing and has good cache behavior in the common case. 168 ++ProbeAmt; 169 } 170 } 171 172 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not 173 /// delete it. This aborts if the value isn't in the table. 174 void StringMapImpl::RemoveKey(StringMapEntryBase *V) { 175 const char *VStr = (char *)V + ItemSize; 176 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength())); 177 (void)V2; 178 assert(V == V2 && "Didn't find key?"); 179 } 180 181 /// RemoveKey - Remove the StringMapEntry for the specified key from the 182 /// table, returning it. If the key is not in the table, this returns null. 183 StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) { 184 int Bucket = FindKey(Key); 185 if (Bucket == -1) 186 return nullptr; 187 188 StringMapEntryBase *Result = TheTable[Bucket]; 189 TheTable[Bucket] = getTombstoneVal(); 190 --NumItems; 191 ++NumTombstones; 192 assert(NumItems + NumTombstones <= NumBuckets); 193 194 return Result; 195 } 196 197 /// RehashTable - Grow the table, redistributing values into the buckets with 198 /// the appropriate mod-of-hashtable-size. 199 unsigned StringMapImpl::RehashTable(unsigned BucketNo) { 200 unsigned NewSize; 201 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1); 202 203 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of 204 // the buckets are empty (meaning that many are filled with tombstones), 205 // grow/rehash the table. 206 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) { 207 NewSize = NumBuckets * 2; 208 } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <= 209 NumBuckets / 8)) { 210 NewSize = NumBuckets; 211 } else { 212 return BucketNo; 213 } 214 215 unsigned NewBucketNo = BucketNo; 216 // Allocate one extra bucket which will always be non-empty. This allows the 217 // iterators to stop at end. 218 auto NewTableArray = static_cast<StringMapEntryBase **>(safe_calloc( 219 NewSize + 1, sizeof(StringMapEntryBase *) + sizeof(unsigned))); 220 221 unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1); 222 NewTableArray[NewSize] = (StringMapEntryBase *)2; 223 224 // Rehash all the items into their new buckets. Luckily :) we already have 225 // the hash values available, so we don't have to rehash any strings. 226 for (unsigned I = 0, E = NumBuckets; I != E; ++I) { 227 StringMapEntryBase *Bucket = TheTable[I]; 228 if (Bucket && Bucket != getTombstoneVal()) { 229 // Fast case, bucket available. 230 unsigned FullHash = HashTable[I]; 231 unsigned NewBucket = FullHash & (NewSize - 1); 232 if (!NewTableArray[NewBucket]) { 233 NewTableArray[FullHash & (NewSize - 1)] = Bucket; 234 NewHashArray[FullHash & (NewSize - 1)] = FullHash; 235 if (I == BucketNo) 236 NewBucketNo = NewBucket; 237 continue; 238 } 239 240 // Otherwise probe for a spot. 241 unsigned ProbeSize = 1; 242 do { 243 NewBucket = (NewBucket + ProbeSize++) & (NewSize - 1); 244 } while (NewTableArray[NewBucket]); 245 246 // Finally found a slot. Fill it in. 247 NewTableArray[NewBucket] = Bucket; 248 NewHashArray[NewBucket] = FullHash; 249 if (I == BucketNo) 250 NewBucketNo = NewBucket; 251 } 252 } 253 254 free(TheTable); 255 256 TheTable = NewTableArray; 257 NumBuckets = NewSize; 258 NumTombstones = 0; 259 return NewBucketNo; 260 } 261