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