1 //===--- StringMap.cpp - String Hash table map implementation -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the StringMap class.
11 //===----------------------------------------------------------------------===//
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"
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"
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);
33 StringMapImpl::StringMapImpl(unsigned InitSize
, unsigned itemSize
) {
36 // If a size is specified, initialize the table with that many buckets.
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
));
45 // Otherwise, initialize it with zero buckets to avoid the allocation.
52 void StringMapImpl::init(unsigned InitSize
) {
53 assert((InitSize
& (InitSize
-1)) == 0 &&
54 "Init Size must be a power of 2 or zero!");
56 unsigned NewNumBuckets
= InitSize
? InitSize
: 16;
60 TheTable
= static_cast<StringMapEntryBase
**>(
61 safe_calloc(NewNumBuckets
+1,
62 sizeof(StringMapEntryBase
**) + sizeof(unsigned)));
64 // Set the member only if TheTable was successfully allocated
65 NumBuckets
= NewNumBuckets
;
67 // Allocate one extra bucket, set it to look filled so the iterators stop at
69 TheTable
[NumBuckets
] = (StringMapEntryBase
*)2;
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
77 unsigned StringMapImpl::LookupBucketFor(StringRef Name
) {
78 unsigned HTSize
= NumBuckets
;
79 if (HTSize
== 0) { // Hash table unallocated so far?
83 unsigned FullHashValue
= djbHash(Name
, 0);
84 unsigned BucketNo
= FullHashValue
& (HTSize
-1);
85 unsigned *HashTable
= (unsigned *)(TheTable
+ NumBuckets
+ 1);
87 unsigned ProbeAmt
= 1;
88 int FirstTombstone
= -1;
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
;
100 HashTable
[BucketNo
] = FullHashValue
;
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.
113 // Do the comparison like this because Name isn't necessarily
115 char *ItemStr
= (char*)BucketItem
+ItemSize
;
116 if (Name
== StringRef(ItemStr
, BucketItem
->getKeyLength())) {
122 // Okay, we didn't find the item. Probe to the next bucket.
123 BucketNo
= (BucketNo
+ProbeAmt
) & (HTSize
-1);
125 // Use quadratic probing, it has fewer clumping artifacts than linear
126 // probing and has good cache behavior in the common case.
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);
141 unsigned ProbeAmt
= 1;
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
))
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.
156 // Do the comparison like this because NameStart isn't necessarily
158 char *ItemStr
= (char*)BucketItem
+ItemSize
;
159 if (Key
== StringRef(ItemStr
, BucketItem
->getKeyLength())) {
165 // Okay, we didn't find the item. Probe to the next bucket.
166 BucketNo
= (BucketNo
+ProbeAmt
) & (HTSize
-1);
168 // Use quadratic probing, it has fewer clumping artifacts than linear
169 // probing and has good cache behavior in the common case.
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()));
180 assert(V
== V2
&& "Didn't find key?");
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;
189 StringMapEntryBase
*Result
= TheTable
[Bucket
];
190 TheTable
[Bucket
] = getTombstoneVal();
193 assert(NumItems
+ NumTombstones
<= NumBuckets
);
198 /// RehashTable - Grow the table, redistributing values into the buckets with
199 /// the appropriate mod-of-hashtable-size.
200 unsigned StringMapImpl::RehashTable(unsigned BucketNo
) {
202 unsigned *HashTable
= (unsigned *)(TheTable
+ NumBuckets
+ 1);
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
) <=
211 NewSize
= NumBuckets
;
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)));
222 unsigned *NewHashArray
= (unsigned *)(NewTableArray
+ NewSize
+ 1);
223 NewTableArray
[NewSize
] = (StringMapEntryBase
*)2;
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
;
237 NewBucketNo
= NewBucket
;
241 // Otherwise probe for a spot.
242 unsigned ProbeSize
= 1;
244 NewBucket
= (NewBucket
+ ProbeSize
++) & (NewSize
-1);
245 } while (NewTableArray
[NewBucket
]);
247 // Finally found a slot. Fill it in.
248 NewTableArray
[NewBucket
] = Bucket
;
249 NewHashArray
[NewBucket
] = FullHash
;
251 NewBucketNo
= NewBucket
;
257 TheTable
= NewTableArray
;
258 NumBuckets
= NewSize
;