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