1009b1c42SEd Schouten //===--- StringMap.cpp - String Hash table map implementation -------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the StringMap class.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten
13009b1c42SEd Schouten #include "llvm/ADT/StringMap.h"
14b915e9e0SDimitry Andric #include "llvm/Support/MathExtras.h"
157fa27ce4SDimitry Andric #include "llvm/Support/ReverseIteration.h"
167fa27ce4SDimitry Andric #include "llvm/Support/xxhash.h"
17b915e9e0SDimitry Andric
18009b1c42SEd Schouten using namespace llvm;
19009b1c42SEd Schouten
2001095a5dSDimitry Andric /// Returns the number of buckets to allocate to ensure that the DenseMap can
2101095a5dSDimitry Andric /// accommodate \p NumEntries without need to grow().
getMinBucketToReserveForEntries(unsigned NumEntries)22145449b1SDimitry Andric static inline unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
2301095a5dSDimitry Andric // Ensure that "NumEntries * 4 < NumBuckets * 3"
2401095a5dSDimitry Andric if (NumEntries == 0)
2501095a5dSDimitry Andric return 0;
2601095a5dSDimitry Andric // +1 is required because of the strict equality.
2701095a5dSDimitry Andric // For example if NumEntries is 48, we need to return 401.
2801095a5dSDimitry Andric return NextPowerOf2(NumEntries * 4 / 3 + 1);
2901095a5dSDimitry Andric }
3001095a5dSDimitry Andric
createTable(unsigned NewNumBuckets)31145449b1SDimitry Andric static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) {
32145449b1SDimitry Andric auto **Table = static_cast<StringMapEntryBase **>(safe_calloc(
33145449b1SDimitry Andric NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned)));
34145449b1SDimitry Andric
35145449b1SDimitry Andric // Allocate one extra bucket, set it to look filled so the iterators stop at
36145449b1SDimitry Andric // end.
37145449b1SDimitry Andric Table[NewNumBuckets] = (StringMapEntryBase *)2;
38145449b1SDimitry Andric return Table;
39145449b1SDimitry Andric }
40145449b1SDimitry Andric
getHashTable(StringMapEntryBase ** TheTable,unsigned NumBuckets)41145449b1SDimitry Andric static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
42145449b1SDimitry Andric unsigned NumBuckets) {
43145449b1SDimitry Andric return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
44145449b1SDimitry Andric }
45145449b1SDimitry Andric
hash(StringRef Key)46ac9a064cSDimitry Andric uint32_t StringMapImpl::hash(StringRef Key) { return xxh3_64bits(Key); }
47ac9a064cSDimitry Andric
StringMapImpl(unsigned InitSize,unsigned itemSize)48009b1c42SEd Schouten StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
49009b1c42SEd Schouten ItemSize = itemSize;
50009b1c42SEd Schouten
51009b1c42SEd Schouten // If a size is specified, initialize the table with that many buckets.
52009b1c42SEd Schouten if (InitSize) {
5301095a5dSDimitry Andric // The table will grow when the number of entries reach 3/4 of the number of
5401095a5dSDimitry Andric // buckets. To guarantee that "InitSize" number of entries can be inserted
5501095a5dSDimitry Andric // in the table without growing, we allocate just what is needed here.
5601095a5dSDimitry Andric init(getMinBucketToReserveForEntries(InitSize));
57009b1c42SEd Schouten return;
58009b1c42SEd Schouten }
59009b1c42SEd Schouten
60009b1c42SEd Schouten // Otherwise, initialize it with zero buckets to avoid the allocation.
615ca98fd9SDimitry Andric TheTable = nullptr;
62009b1c42SEd Schouten NumBuckets = 0;
63009b1c42SEd Schouten NumItems = 0;
64009b1c42SEd Schouten NumTombstones = 0;
65009b1c42SEd Schouten }
66009b1c42SEd Schouten
init(unsigned InitSize)67009b1c42SEd Schouten void StringMapImpl::init(unsigned InitSize) {
68009b1c42SEd Schouten assert((InitSize & (InitSize - 1)) == 0 &&
69009b1c42SEd Schouten "Init Size must be a power of 2 or zero!");
70044eb2f6SDimitry Andric
71044eb2f6SDimitry Andric unsigned NewNumBuckets = InitSize ? InitSize : 16;
72009b1c42SEd Schouten NumItems = 0;
73009b1c42SEd Schouten NumTombstones = 0;
74009b1c42SEd Schouten
75145449b1SDimitry Andric TheTable = createTable(NewNumBuckets);
76044eb2f6SDimitry Andric
77044eb2f6SDimitry Andric // Set the member only if TheTable was successfully allocated
78044eb2f6SDimitry Andric NumBuckets = NewNumBuckets;
79009b1c42SEd Schouten }
80009b1c42SEd Schouten
81009b1c42SEd Schouten /// LookupBucketFor - Look up the bucket that the specified string should end
82009b1c42SEd Schouten /// up in. If it already exists as a key in the map, the Item pointer for the
83009b1c42SEd Schouten /// specified bucket will be non-null. Otherwise, it will be null. In either
84009b1c42SEd Schouten /// case, the FullHashValue field of the bucket will be set to the hash value
85009b1c42SEd Schouten /// of the string.
LookupBucketFor(StringRef Name,uint32_t FullHashValue)86ac9a064cSDimitry Andric unsigned StringMapImpl::LookupBucketFor(StringRef Name,
87ac9a064cSDimitry Andric uint32_t FullHashValue) {
88ac9a064cSDimitry Andric #ifdef EXPENSIVE_CHECKS
89ac9a064cSDimitry Andric assert(FullHashValue == hash(Name));
90ac9a064cSDimitry Andric #endif
91145449b1SDimitry Andric // Hash table unallocated so far?
92145449b1SDimitry Andric if (NumBuckets == 0)
93009b1c42SEd Schouten init(16);
947fa27ce4SDimitry Andric if (shouldReverseIterate())
957fa27ce4SDimitry Andric FullHashValue = ~FullHashValue;
96145449b1SDimitry Andric unsigned BucketNo = FullHashValue & (NumBuckets - 1);
97145449b1SDimitry Andric unsigned *HashTable = getHashTable(TheTable, NumBuckets);
98009b1c42SEd Schouten
99009b1c42SEd Schouten unsigned ProbeAmt = 1;
100009b1c42SEd Schouten int FirstTombstone = -1;
101b915e9e0SDimitry Andric while (true) {
10263faed5bSDimitry Andric StringMapEntryBase *BucketItem = TheTable[BucketNo];
103009b1c42SEd Schouten // If we found an empty bucket, this key isn't in the table yet, return it.
1045ca98fd9SDimitry Andric if (LLVM_LIKELY(!BucketItem)) {
105009b1c42SEd Schouten // If we found a tombstone, we want to reuse the tombstone instead of an
106009b1c42SEd Schouten // empty bucket. This reduces probing.
107009b1c42SEd Schouten if (FirstTombstone != -1) {
10863faed5bSDimitry Andric HashTable[FirstTombstone] = FullHashValue;
109009b1c42SEd Schouten return FirstTombstone;
110009b1c42SEd Schouten }
111009b1c42SEd Schouten
11263faed5bSDimitry Andric HashTable[BucketNo] = FullHashValue;
113009b1c42SEd Schouten return BucketNo;
114009b1c42SEd Schouten }
115009b1c42SEd Schouten
116009b1c42SEd Schouten if (BucketItem == getTombstoneVal()) {
117009b1c42SEd Schouten // Skip over tombstones. However, remember the first one we see.
118cfca06d7SDimitry Andric if (FirstTombstone == -1)
119cfca06d7SDimitry Andric FirstTombstone = BucketNo;
120522600a2SDimitry Andric } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
121009b1c42SEd Schouten // If the full hash value matches, check deeply for a match. The common
122009b1c42SEd Schouten // case here is that we are only looking at the buckets (for item info
123009b1c42SEd Schouten // being non-null and for the full hash value) not at the items. This
124009b1c42SEd Schouten // is important for cache locality.
125009b1c42SEd Schouten
12659850d08SRoman Divacky // Do the comparison like this because Name isn't necessarily
127009b1c42SEd Schouten // null-terminated!
128009b1c42SEd Schouten char *ItemStr = (char *)BucketItem + ItemSize;
12959850d08SRoman Divacky if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
130009b1c42SEd Schouten // We found a match!
131009b1c42SEd Schouten return BucketNo;
132009b1c42SEd Schouten }
133009b1c42SEd Schouten }
134009b1c42SEd Schouten
135009b1c42SEd Schouten // Okay, we didn't find the item. Probe to the next bucket.
136145449b1SDimitry Andric BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
137009b1c42SEd Schouten
138009b1c42SEd Schouten // Use quadratic probing, it has fewer clumping artifacts than linear
139009b1c42SEd Schouten // probing and has good cache behavior in the common case.
140009b1c42SEd Schouten ++ProbeAmt;
141009b1c42SEd Schouten }
142009b1c42SEd Schouten }
143009b1c42SEd Schouten
144009b1c42SEd Schouten /// FindKey - Look up the bucket that contains the specified key. If it exists
145009b1c42SEd Schouten /// in the map, return the bucket number of the key. Otherwise return -1.
146009b1c42SEd Schouten /// This does not modify the map.
FindKey(StringRef Key,uint32_t FullHashValue) const147ac9a064cSDimitry Andric int StringMapImpl::FindKey(StringRef Key, uint32_t FullHashValue) const {
148145449b1SDimitry Andric if (NumBuckets == 0)
149cfca06d7SDimitry Andric return -1; // Really empty table?
150ac9a064cSDimitry Andric #ifdef EXPENSIVE_CHECKS
151ac9a064cSDimitry Andric assert(FullHashValue == hash(Key));
152ac9a064cSDimitry Andric #endif
1537fa27ce4SDimitry Andric if (shouldReverseIterate())
1547fa27ce4SDimitry Andric FullHashValue = ~FullHashValue;
155145449b1SDimitry Andric unsigned BucketNo = FullHashValue & (NumBuckets - 1);
156145449b1SDimitry Andric unsigned *HashTable = getHashTable(TheTable, NumBuckets);
157009b1c42SEd Schouten
158009b1c42SEd Schouten unsigned ProbeAmt = 1;
159b915e9e0SDimitry Andric while (true) {
16063faed5bSDimitry Andric StringMapEntryBase *BucketItem = TheTable[BucketNo];
161009b1c42SEd Schouten // If we found an empty bucket, this key isn't in the table yet, return.
1625ca98fd9SDimitry Andric if (LLVM_LIKELY(!BucketItem))
163009b1c42SEd Schouten return -1;
164009b1c42SEd Schouten
165009b1c42SEd Schouten if (BucketItem == getTombstoneVal()) {
166009b1c42SEd Schouten // Ignore tombstones.
167522600a2SDimitry Andric } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
168009b1c42SEd Schouten // If the full hash value matches, check deeply for a match. The common
169009b1c42SEd Schouten // case here is that we are only looking at the buckets (for item info
170009b1c42SEd Schouten // being non-null and for the full hash value) not at the items. This
171009b1c42SEd Schouten // is important for cache locality.
172009b1c42SEd Schouten
173009b1c42SEd Schouten // Do the comparison like this because NameStart isn't necessarily
174009b1c42SEd Schouten // null-terminated!
175009b1c42SEd Schouten char *ItemStr = (char *)BucketItem + ItemSize;
17659850d08SRoman Divacky if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
177009b1c42SEd Schouten // We found a match!
178009b1c42SEd Schouten return BucketNo;
179009b1c42SEd Schouten }
180009b1c42SEd Schouten }
181009b1c42SEd Schouten
182009b1c42SEd Schouten // Okay, we didn't find the item. Probe to the next bucket.
183145449b1SDimitry Andric BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
184009b1c42SEd Schouten
185009b1c42SEd Schouten // Use quadratic probing, it has fewer clumping artifacts than linear
186009b1c42SEd Schouten // probing and has good cache behavior in the common case.
187009b1c42SEd Schouten ++ProbeAmt;
188009b1c42SEd Schouten }
189009b1c42SEd Schouten }
190009b1c42SEd Schouten
191009b1c42SEd Schouten /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
192009b1c42SEd Schouten /// delete it. This aborts if the value isn't in the table.
RemoveKey(StringMapEntryBase * V)193009b1c42SEd Schouten void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
194009b1c42SEd Schouten const char *VStr = (char *)V + ItemSize;
19559850d08SRoman Divacky StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
196cf099d11SDimitry Andric (void)V2;
197009b1c42SEd Schouten assert(V == V2 && "Didn't find key?");
198009b1c42SEd Schouten }
199009b1c42SEd Schouten
200009b1c42SEd Schouten /// RemoveKey - Remove the StringMapEntry for the specified key from the
201009b1c42SEd Schouten /// table, returning it. If the key is not in the table, this returns null.
RemoveKey(StringRef Key)202907da171SRoman Divacky StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
20359850d08SRoman Divacky int Bucket = FindKey(Key);
204cfca06d7SDimitry Andric if (Bucket == -1)
205cfca06d7SDimitry Andric return nullptr;
206009b1c42SEd Schouten
20763faed5bSDimitry Andric StringMapEntryBase *Result = TheTable[Bucket];
20863faed5bSDimitry Andric TheTable[Bucket] = getTombstoneVal();
209009b1c42SEd Schouten --NumItems;
210009b1c42SEd Schouten ++NumTombstones;
2116b943ff3SDimitry Andric assert(NumItems + NumTombstones <= NumBuckets);
2126b943ff3SDimitry Andric
213009b1c42SEd Schouten return Result;
214009b1c42SEd Schouten }
215009b1c42SEd Schouten
216009b1c42SEd Schouten /// RehashTable - Grow the table, redistributing values into the buckets with
217009b1c42SEd Schouten /// the appropriate mod-of-hashtable-size.
RehashTable(unsigned BucketNo)2185ca98fd9SDimitry Andric unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
2196b943ff3SDimitry Andric unsigned NewSize;
2206b943ff3SDimitry Andric // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
2216b943ff3SDimitry Andric // the buckets are empty (meaning that many are filled with tombstones),
2226b943ff3SDimitry Andric // grow/rehash the table.
2235a5ac124SDimitry Andric if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
2246b943ff3SDimitry Andric NewSize = NumBuckets * 2;
2255a5ac124SDimitry Andric } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
2265a5ac124SDimitry Andric NumBuckets / 8)) {
2276b943ff3SDimitry Andric NewSize = NumBuckets;
2286b943ff3SDimitry Andric } else {
2295ca98fd9SDimitry Andric return BucketNo;
2306b943ff3SDimitry Andric }
2316b943ff3SDimitry Andric
2325ca98fd9SDimitry Andric unsigned NewBucketNo = BucketNo;
233145449b1SDimitry Andric auto **NewTableArray = createTable(NewSize);
234145449b1SDimitry Andric unsigned *NewHashArray = getHashTable(NewTableArray, NewSize);
235145449b1SDimitry Andric unsigned *HashTable = getHashTable(TheTable, NumBuckets);
236009b1c42SEd Schouten
237009b1c42SEd Schouten // Rehash all the items into their new buckets. Luckily :) we already have
238009b1c42SEd Schouten // the hash values available, so we don't have to rehash any strings.
23963faed5bSDimitry Andric for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
24063faed5bSDimitry Andric StringMapEntryBase *Bucket = TheTable[I];
24163faed5bSDimitry Andric if (Bucket && Bucket != getTombstoneVal()) {
242145449b1SDimitry Andric // If the bucket is not available, probe for a spot.
24363faed5bSDimitry Andric unsigned FullHash = HashTable[I];
244009b1c42SEd Schouten unsigned NewBucket = FullHash & (NewSize - 1);
245145449b1SDimitry Andric if (NewTableArray[NewBucket]) {
246009b1c42SEd Schouten unsigned ProbeSize = 1;
247009b1c42SEd Schouten do {
248009b1c42SEd Schouten NewBucket = (NewBucket + ProbeSize++) & (NewSize - 1);
24963faed5bSDimitry Andric } while (NewTableArray[NewBucket]);
250145449b1SDimitry Andric }
251009b1c42SEd Schouten
252009b1c42SEd Schouten // Finally found a slot. Fill it in.
25363faed5bSDimitry Andric NewTableArray[NewBucket] = Bucket;
25463faed5bSDimitry Andric NewHashArray[NewBucket] = FullHash;
2555ca98fd9SDimitry Andric if (I == BucketNo)
2565ca98fd9SDimitry Andric NewBucketNo = NewBucket;
257009b1c42SEd Schouten }
258009b1c42SEd Schouten }
259009b1c42SEd Schouten
260009b1c42SEd Schouten free(TheTable);
261009b1c42SEd Schouten
262009b1c42SEd Schouten TheTable = NewTableArray;
263009b1c42SEd Schouten NumBuckets = NewSize;
2646b943ff3SDimitry Andric NumTombstones = 0;
2655ca98fd9SDimitry Andric return NewBucketNo;
266009b1c42SEd Schouten }
267