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