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