StringMap.cpp 9.1 KB

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