StringMap.cpp 8.7 KB

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