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/Compiler.h"
16 #include "llvm/Support/DJB.h"
17 #include "llvm/Support/MathExtras.h"
18 #include <cassert>
19 
20 using namespace llvm;
21 
22 /// Returns the number of buckets to allocate to ensure that the DenseMap can
23 /// accommodate \p NumEntries without need to grow().
24 static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
25   // Ensure that "NumEntries * 4 < NumBuckets * 3"
26   if (NumEntries == 0)
27     return 0;
28   // +1 is required because of the strict equality.
29   // For example if NumEntries is 48, we need to return 401.
30   return NextPowerOf2(NumEntries * 4 / 3 + 1);
31 }
32 
33 StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
34   ItemSize = itemSize;
35 
36   // If a size is specified, initialize the table with that many buckets.
37   if (InitSize) {
38     // The table will grow when the number of entries reach 3/4 of the number of
39     // buckets. To guarantee that "InitSize" number of entries can be inserted
40     // in the table without growing, we allocate just what is needed here.
41     init(getMinBucketToReserveForEntries(InitSize));
42     return;
43   }
44 
45   // Otherwise, initialize it with zero buckets to avoid the allocation.
46   TheTable = nullptr;
47   NumBuckets = 0;
48   NumItems = 0;
49   NumTombstones = 0;
50 }
51 
52 void StringMapImpl::init(unsigned InitSize) {
53   assert((InitSize & (InitSize-1)) == 0 &&
54          "Init Size must be a power of 2 or zero!");
55 
56   unsigned NewNumBuckets = InitSize ? InitSize : 16;
57   NumItems = 0;
58   NumTombstones = 0;
59 
60   TheTable = static_cast<StringMapEntryBase **>(
61       safe_calloc(NewNumBuckets+1,
62                   sizeof(StringMapEntryBase **) + sizeof(unsigned)));
63 
64   // Set the member only if TheTable was successfully allocated
65   NumBuckets = NewNumBuckets;
66 
67   // Allocate one extra bucket, set it to look filled so the iterators stop at
68   // end.
69   TheTable[NumBuckets] = (StringMapEntryBase*)2;
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   unsigned HTSize = NumBuckets;
79   if (HTSize == 0) {  // Hash table unallocated so far?
80     init(16);
81     HTSize = NumBuckets;
82   }
83   unsigned FullHashValue = djbHash(Name, 0);
84   unsigned BucketNo = FullHashValue & (HTSize-1);
85   unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
86 
87   unsigned ProbeAmt = 1;
88   int FirstTombstone = -1;
89   while (true) {
90     StringMapEntryBase *BucketItem = TheTable[BucketNo];
91     // If we found an empty bucket, this key isn't in the table yet, return it.
92     if (LLVM_LIKELY(!BucketItem)) {
93       // If we found a tombstone, we want to reuse the tombstone instead of an
94       // empty bucket.  This reduces probing.
95       if (FirstTombstone != -1) {
96         HashTable[FirstTombstone] = FullHashValue;
97         return FirstTombstone;
98       }
99 
100       HashTable[BucketNo] = FullHashValue;
101       return BucketNo;
102     }
103 
104     if (BucketItem == getTombstoneVal()) {
105       // Skip over tombstones.  However, remember the first one we see.
106       if (FirstTombstone == -1) FirstTombstone = BucketNo;
107     } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
108       // If the full hash value matches, check deeply for a match.  The common
109       // case here is that we are only looking at the buckets (for item info
110       // being non-null and for the full hash value) not at the items.  This
111       // is important for cache locality.
112 
113       // Do the comparison like this because Name isn't necessarily
114       // null-terminated!
115       char *ItemStr = (char*)BucketItem+ItemSize;
116       if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
117         // We found a match!
118         return BucketNo;
119       }
120     }
121 
122     // Okay, we didn't find the item.  Probe to the next bucket.
123     BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
124 
125     // Use quadratic probing, it has fewer clumping artifacts than linear
126     // probing and has good cache behavior in the common case.
127     ++ProbeAmt;
128   }
129 }
130 
131 /// FindKey - Look up the bucket that contains the specified key. If it exists
132 /// in the map, return the bucket number of the key.  Otherwise return -1.
133 /// This does not modify the map.
134 int StringMapImpl::FindKey(StringRef Key) const {
135   unsigned HTSize = NumBuckets;
136   if (HTSize == 0) return -1;  // Really empty table?
137   unsigned FullHashValue = djbHash(Key, 0);
138   unsigned BucketNo = FullHashValue & (HTSize-1);
139   unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
140 
141   unsigned ProbeAmt = 1;
142   while (true) {
143     StringMapEntryBase *BucketItem = TheTable[BucketNo];
144     // If we found an empty bucket, this key isn't in the table yet, return.
145     if (LLVM_LIKELY(!BucketItem))
146       return -1;
147 
148     if (BucketItem == getTombstoneVal()) {
149       // Ignore tombstones.
150     } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
151       // If the full hash value matches, check deeply for a match.  The common
152       // case here is that we are only looking at the buckets (for item info
153       // being non-null and for the full hash value) not at the items.  This
154       // is important for cache locality.
155 
156       // Do the comparison like this because NameStart isn't necessarily
157       // null-terminated!
158       char *ItemStr = (char*)BucketItem+ItemSize;
159       if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
160         // We found a match!
161         return BucketNo;
162       }
163     }
164 
165     // Okay, we didn't find the item.  Probe to the next bucket.
166     BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
167 
168     // Use quadratic probing, it has fewer clumping artifacts than linear
169     // probing and has good cache behavior in the common case.
170     ++ProbeAmt;
171   }
172 }
173 
174 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
175 /// delete it.  This aborts if the value isn't in the table.
176 void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
177   const char *VStr = (char*)V + ItemSize;
178   StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
179   (void)V2;
180   assert(V == V2 && "Didn't find key?");
181 }
182 
183 /// RemoveKey - Remove the StringMapEntry for the specified key from the
184 /// table, returning it.  If the key is not in the table, this returns null.
185 StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
186   int Bucket = FindKey(Key);
187   if (Bucket == -1) 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 **>(
220       safe_calloc(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