LLVM 17.0.0git
StringMap.cpp
Go to the documentation of this file.
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"
15#include "llvm/Support/xxhash.h"
16
17using 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().
21static 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
30static 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
40static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
41 unsigned NumBuckets) {
42 return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
43}
44
45StringMapImpl::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.
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
64void 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.
84 // Hash table unallocated so far?
85 if (NumBuckets == 0)
86 init(16);
87 unsigned FullHashValue = xxHash64(Name);
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.
140 if (NumBuckets == 0)
141 return -1; // Really empty table?
142 unsigned FullHashValue = xxHash64(Key);
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.
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.
191 int Bucket = FindKey(Key);
192 if (Bucket == -1)
193 return nullptr;
194
195 StringMapEntryBase *Result = TheTable[Bucket];
196 TheTable[Bucket] = getTombstoneVal();
197 --NumItems;
200
201 return Result;
202}
203
204/// RehashTable - Grow the table, redistributing values into the buckets with
205/// the appropriate mod-of-hashtable-size.
206unsigned 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;
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}
This file defines the StringMap class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:210
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:209
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringMapEntryBase ** createTable(unsigned NewNumBuckets)
Definition: StringMap.cpp:30
static unsigned * getHashTable(StringMapEntryBase **TheTable, unsigned NumBuckets)
Definition: StringMap.cpp:40
static unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition: StringMap.cpp:21
StringMapEntryBase - Shared base class of StringMapEntry instances.
size_t getKeyLength() const
static StringMapEntryBase * getTombstoneVal()
Definition: StringMap.h:87
unsigned ItemSize
Definition: StringMap.h:41
int FindKey(StringRef Key) const
FindKey - Look up the bucket that contains the specified key.
Definition: StringMap.cpp:139
unsigned RehashTable(unsigned BucketNo=0)
RehashTable - Grow the table, redistributing values into the buckets with the appropriate mod-of-hash...
Definition: StringMap.cpp:206
unsigned NumItems
Definition: StringMap.h:39
unsigned LookupBucketFor(StringRef Key)
LookupBucketFor - Look up the bucket that the specified string should end up in.
Definition: StringMap.cpp:83
void RemoveKey(StringMapEntryBase *V)
RemoveKey - Remove the specified StringMapEntry from the table, but do not delete it.
Definition: StringMap.cpp:181
StringMapEntryBase ** TheTable
Definition: StringMap.h:37
StringMapImpl(unsigned itemSize)
Definition: StringMap.h:44
void init(unsigned Size)
Allocate the table with the specified number of buckets and otherwise setup the map as empty.
Definition: StringMap.cpp:64
unsigned NumBuckets
Definition: StringMap.h:38
unsigned NumTombstones
Definition: StringMap.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_calloc(size_t Count, size_t Sz)
Definition: MemAlloc.h:38
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:418
uint64_t xxHash64(llvm::StringRef Data)
Definition: xxhash.cpp:70