1 //===--- StringMap.cpp - String Hash table map implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the StringMap class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/Compiler.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!");
55 NumBuckets = InitSize ? InitSize : 16;
59 TheTable = (StringMapEntryBase **)calloc(NumBuckets+1,
60 sizeof(StringMapEntryBase **) +
63 // Allocate one extra bucket, set it to look filled so the iterators stop at
65 TheTable[NumBuckets] = (StringMapEntryBase*)2;
68 /// LookupBucketFor - Look up the bucket that the specified string should end
69 /// up in. If it already exists as a key in the map, the Item pointer for the
70 /// specified bucket will be non-null. Otherwise, it will be null. In either
71 /// case, the FullHashValue field of the bucket will be set to the hash value
73 unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
74 unsigned HTSize = NumBuckets;
75 if (HTSize == 0) { // Hash table unallocated so far?
79 unsigned FullHashValue = HashString(Name);
80 unsigned BucketNo = FullHashValue & (HTSize-1);
81 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
83 unsigned ProbeAmt = 1;
84 int FirstTombstone = -1;
86 StringMapEntryBase *BucketItem = TheTable[BucketNo];
87 // If we found an empty bucket, this key isn't in the table yet, return it.
88 if (LLVM_LIKELY(!BucketItem)) {
89 // If we found a tombstone, we want to reuse the tombstone instead of an
90 // empty bucket. This reduces probing.
91 if (FirstTombstone != -1) {
92 HashTable[FirstTombstone] = FullHashValue;
93 return FirstTombstone;
96 HashTable[BucketNo] = FullHashValue;
100 if (BucketItem == getTombstoneVal()) {
101 // Skip over tombstones. However, remember the first one we see.
102 if (FirstTombstone == -1) FirstTombstone = BucketNo;
103 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
104 // If the full hash value matches, check deeply for a match. The common
105 // case here is that we are only looking at the buckets (for item info
106 // being non-null and for the full hash value) not at the items. This
107 // is important for cache locality.
109 // Do the comparison like this because Name isn't necessarily
111 char *ItemStr = (char*)BucketItem+ItemSize;
112 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
118 // Okay, we didn't find the item. Probe to the next bucket.
119 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
121 // Use quadratic probing, it has fewer clumping artifacts than linear
122 // probing and has good cache behavior in the common case.
127 /// FindKey - Look up the bucket that contains the specified key. If it exists
128 /// in the map, return the bucket number of the key. Otherwise return -1.
129 /// This does not modify the map.
130 int StringMapImpl::FindKey(StringRef Key) const {
131 unsigned HTSize = NumBuckets;
132 if (HTSize == 0) return -1; // Really empty table?
133 unsigned FullHashValue = HashString(Key);
134 unsigned BucketNo = FullHashValue & (HTSize-1);
135 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
137 unsigned ProbeAmt = 1;
139 StringMapEntryBase *BucketItem = TheTable[BucketNo];
140 // If we found an empty bucket, this key isn't in the table yet, return.
141 if (LLVM_LIKELY(!BucketItem))
144 if (BucketItem == getTombstoneVal()) {
145 // Ignore tombstones.
146 } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
147 // If the full hash value matches, check deeply for a match. The common
148 // case here is that we are only looking at the buckets (for item info
149 // being non-null and for the full hash value) not at the items. This
150 // is important for cache locality.
152 // Do the comparison like this because NameStart isn't necessarily
154 char *ItemStr = (char*)BucketItem+ItemSize;
155 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
161 // Okay, we didn't find the item. Probe to the next bucket.
162 BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
164 // Use quadratic probing, it has fewer clumping artifacts than linear
165 // probing and has good cache behavior in the common case.
170 /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
171 /// delete it. This aborts if the value isn't in the table.
172 void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
173 const char *VStr = (char*)V + ItemSize;
174 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
176 assert(V == V2 && "Didn't find key?");
179 /// RemoveKey - Remove the StringMapEntry for the specified key from the
180 /// table, returning it. If the key is not in the table, this returns null.
181 StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
182 int Bucket = FindKey(Key);
183 if (Bucket == -1) return nullptr;
185 StringMapEntryBase *Result = TheTable[Bucket];
186 TheTable[Bucket] = getTombstoneVal();
189 assert(NumItems + NumTombstones <= NumBuckets);
194 /// RehashTable - Grow the table, redistributing values into the buckets with
195 /// the appropriate mod-of-hashtable-size.
196 unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
198 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
200 // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
201 // the buckets are empty (meaning that many are filled with tombstones),
202 // grow/rehash the table.
203 if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
204 NewSize = NumBuckets*2;
205 } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
207 NewSize = NumBuckets;
212 unsigned NewBucketNo = BucketNo;
213 // Allocate one extra bucket which will always be non-empty. This allows the
214 // iterators to stop at end.
215 StringMapEntryBase **NewTableArray =
216 (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) +
218 unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
219 NewTableArray[NewSize] = (StringMapEntryBase*)2;
221 // Rehash all the items into their new buckets. Luckily :) we already have
222 // the hash values available, so we don't have to rehash any strings.
223 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
224 StringMapEntryBase *Bucket = TheTable[I];
225 if (Bucket && Bucket != getTombstoneVal()) {
226 // Fast case, bucket available.
227 unsigned FullHash = HashTable[I];
228 unsigned NewBucket = FullHash & (NewSize-1);
229 if (!NewTableArray[NewBucket]) {
230 NewTableArray[FullHash & (NewSize-1)] = Bucket;
231 NewHashArray[FullHash & (NewSize-1)] = FullHash;
233 NewBucketNo = NewBucket;
237 // Otherwise probe for a spot.
238 unsigned ProbeSize = 1;
240 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
241 } while (NewTableArray[NewBucket]);
243 // Finally found a slot. Fill it in.
244 NewTableArray[NewBucket] = Bucket;
245 NewHashArray[NewBucket] = FullHash;
247 NewBucketNo = NewBucket;
253 TheTable = NewTableArray;
254 NumBuckets = NewSize;