StringMap.cpp 9.3 KB

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